1 |
37f6ff02
|
Vaclav Honzik
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
2 |
|
|
import axiosInstance from '../../api/api'
|
3 |
|
|
import { UserDto } from '../../swagger/data-contracts'
|
4 |
|
|
|
5 |
8370b6c1
|
Vaclav Honzik
|
const loginError =
|
6 |
|
|
'Server error occurred while logging in. Please contact help service to resolve this issue or try again later.'
|
7 |
|
|
|
8 |
|
|
|
9 |
|
|
export const logIn = createAsyncThunk(
|
10 |
|
|
'user/login',
|
11 |
|
|
async (userDto: UserDto, { dispatch, getState }) => {
|
12 |
37f6ff02
|
Vaclav Honzik
|
try {
|
13 |
8370b6c1
|
Vaclav Honzik
|
// @ts-ignore
|
14 |
|
|
// TODO fix
|
15 |
|
|
if (getState().user.isLoggedIn) {
|
16 |
|
|
return
|
17 |
|
|
}
|
18 |
|
|
console.log('Dispatching login thunk') // TODO remove
|
19 |
|
|
const { data, status } = await axiosInstance.post('/login', userDto)
|
20 |
|
|
const { accessToken, refreshToken } = data
|
21 |
|
|
console.log(data) // TODO remove
|
22 |
|
|
if (status !== 200) {
|
23 |
|
|
// TODO read API err
|
24 |
|
|
dispatch({ type: 'user/setErr', payload: loginError })
|
25 |
|
|
return
|
26 |
|
|
}
|
27 |
37f6ff02
|
Vaclav Honzik
|
|
28 |
8370b6c1
|
Vaclav Honzik
|
dispatch({ type: 'user/refreshTokens', payload: { accessToken, refreshToken } })
|
29 |
|
|
} catch (err: any) {
|
30 |
|
|
dispatch({ type: 'user/setErr', payload: loginError })
|
31 |
37f6ff02
|
Vaclav Honzik
|
}
|
32 |
|
|
}
|
33 |
|
|
)
|