1 |
37f6ff02
|
Vaclav Honzik
|
import { createAsyncThunk } from '@reduxjs/toolkit'
|
2 |
|
|
import axiosInstance from '../../api/api'
|
3 |
39d74503
|
Vaclav Honzik
|
import { UserState } from './userSlice'
|
4 |
fc79a8cb
|
Vaclav Honzik
|
import jwt from 'jwt-decode'
|
5 |
37f6ff02
|
Vaclav Honzik
|
|
6 |
8370b6c1
|
Vaclav Honzik
|
const loginError =
|
7 |
|
|
'Server error occurred while logging in. Please contact help service to resolve this issue or try again later.'
|
8 |
|
|
|
9 |
b523c74d
|
Schwobik
|
const registerError =
|
10 |
|
|
'Server error occurred while registering. Please contact help service to resolve this issue or try again later.'
|
11 |
|
|
|
12 |
fc79a8cb
|
Vaclav Honzik
|
// This is not present in the swagger since spring generates
|
13 |
|
|
export interface UserLogin {
|
14 |
|
|
username: string,
|
15 |
|
|
password: string
|
16 |
|
|
}
|
17 |
8370b6c1
|
Vaclav Honzik
|
|
18 |
b523c74d
|
Schwobik
|
export interface UserRegister {
|
19 |
627f4d82
|
Schwobik
|
name: string,
|
20 |
b523c74d
|
Schwobik
|
passwords: {
|
21 |
|
|
password: string,
|
22 |
|
|
confirmationPassword: string,
|
23 |
|
|
},
|
24 |
|
|
permissions: {
|
25 |
|
|
canRead: boolean,
|
26 |
|
|
canWrite: boolean,
|
27 |
|
|
canDelete: boolean,
|
28 |
|
|
},
|
29 |
|
|
email: string,
|
30 |
|
|
}
|
31 |
|
|
|
32 |
8370b6c1
|
Vaclav Honzik
|
export const logIn = createAsyncThunk(
|
33 |
|
|
'user/login',
|
34 |
39d74503
|
Vaclav Honzik
|
async (userDto: UserLogin, { getState }) => {
|
35 |
37f6ff02
|
Vaclav Honzik
|
try {
|
36 |
8370b6c1
|
Vaclav Honzik
|
// @ts-ignore
|
37 |
|
|
if (getState().user.isLoggedIn) {
|
38 |
fc79a8cb
|
Vaclav Honzik
|
return Promise.reject(undefined)
|
39 |
8370b6c1
|
Vaclav Honzik
|
}
|
40 |
fc79a8cb
|
Vaclav Honzik
|
|
41 |
8370b6c1
|
Vaclav Honzik
|
const { data, status } = await axiosInstance.post('/login', userDto)
|
42 |
fc79a8cb
|
Vaclav Honzik
|
const [ accessToken, refreshToken ] = [data.access_token, data.refresh_token]
|
43 |
8370b6c1
|
Vaclav Honzik
|
if (status !== 200) {
|
44 |
|
|
// TODO read API err
|
45 |
fc79a8cb
|
Vaclav Honzik
|
return Promise.reject(loginError)
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
// Strip bearer from access token
|
49 |
|
|
const userInfo = jwt(accessToken.replace('Bearer ', '')) as any
|
50 |
|
|
const { sub, authorities } = userInfo
|
51 |
|
|
if (!sub || !authorities) {
|
52 |
|
|
return Promise.reject(loginError)
|
53 |
8370b6c1
|
Vaclav Honzik
|
}
|
54 |
37f6ff02
|
Vaclav Honzik
|
|
55 |
fc79a8cb
|
Vaclav Honzik
|
const userState: UserState = {
|
56 |
|
|
accessToken,
|
57 |
|
|
refreshToken,
|
58 |
|
|
username: sub,
|
59 |
|
|
roles: authorities,
|
60 |
6129910f
|
Vaclav Honzik
|
isLoggingIn: false,
|
61 |
627f4d82
|
Schwobik
|
isLoggedIn: true,
|
62 |
|
|
isRegistered: false
|
63 |
fc79a8cb
|
Vaclav Honzik
|
}
|
64 |
|
|
|
65 |
|
|
return userState
|
66 |
8370b6c1
|
Vaclav Honzik
|
} catch (err: any) {
|
67 |
fc79a8cb
|
Vaclav Honzik
|
return Promise.reject(loginError)
|
68 |
37f6ff02
|
Vaclav Honzik
|
}
|
69 |
|
|
}
|
70 |
|
|
)
|
71 |
b523c74d
|
Schwobik
|
|
72 |
|
|
export const register = createAsyncThunk(
|
73 |
|
|
'user/register',
|
74 |
|
|
async (userDto: UserRegister) => {
|
75 |
|
|
try {
|
76 |
|
|
console.log(userDto.passwords.password)
|
77 |
|
|
console.log(userDto.passwords.confirmationPassword)
|
78 |
|
|
const { data, status } = await axiosInstance.post('/users', userDto)
|
79 |
|
|
if (status !== 200) {
|
80 |
|
|
// TODO read API err
|
81 |
627f4d82
|
Schwobik
|
return Promise.reject(registerError)
|
82 |
b523c74d
|
Schwobik
|
}
|
83 |
|
|
return status
|
84 |
|
|
} catch (err: any) {
|
85 |
627f4d82
|
Schwobik
|
return Promise.reject(registerError)
|
86 |
b523c74d
|
Schwobik
|
}
|
87 |
|
|
}
|
88 |
|
|
)
|