1 |
c7d2ced1
|
Václav Honzík
|
import axios from "../../api/axios"
|
2 |
|
|
import { UserDto } from "../../swagger/data-contracts"
|
3 |
|
|
import ApiCallError from "../../utils/ApiCallError"
|
4 |
|
|
|
5 |
|
|
export const getAccessToken = () => localStorage.getItem('accessToken') as string | null
|
6 |
|
|
export const getRefreshToken = () => localStorage.getItem('refreshToken') as string | null
|
7 |
|
|
|
8 |
|
|
export const setAccessToken = (accessToken: string) => localStorage.setItem('accessToken', accessToken)
|
9 |
|
|
export const setRefreshToken = (refreshToken: string) => localStorage.setItem('refreshToken', refreshToken)
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
export const sendRegisterRequest = (userDto: UserDto) => {
|
13 |
|
|
|
14 |
|
|
}
|
15 |
|
|
|
16 |
|
|
export const sendLoginRequest = async (username: string, password: string, setLoggedInState: (loggedIn: boolean) => void) => {
|
17 |
|
|
try {
|
18 |
|
|
const { data } = await axios.post('/login', {username, password})
|
19 |
|
|
|
20 |
|
|
if (!data) {
|
21 |
|
|
throw new ApiCallError("An authentication error has occurred. Please try again later")
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
// TODO - set state as logged in
|
25 |
|
|
const { accessToken, refreshToken } = data
|
26 |
|
|
setAccessToken(accessToken)
|
27 |
|
|
setRefreshToken(refreshToken)
|
28 |
|
|
setLoggedInState(true)
|
29 |
|
|
}
|
30 |
|
|
catch (err) {
|
31 |
|
|
|
32 |
|
|
}
|
33 |
|
|
}
|