1
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
2
|
import axiosInstance from '../../api/api'
|
3
|
import { UserDto } from '../../swagger/data-contracts'
|
4
|
|
5
|
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
|
try {
|
13
|
// @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
|
|
28
|
dispatch({ type: 'user/refreshTokens', payload: { accessToken, refreshToken } })
|
29
|
} catch (err: any) {
|
30
|
dispatch({ type: 'user/setErr', payload: loginError })
|
31
|
}
|
32
|
}
|
33
|
)
|