Projekt

Obecné

Profil

Stáhnout (2.62 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { createAsyncThunk } from '@reduxjs/toolkit'
2
import axiosInstance from '../../api/api'
3
import {  UserState } from './userSlice'
4
import jwt from 'jwt-decode' 
5

    
6
const loginError =
7
    'Server error occurred while logging in. Please contact help service to resolve this issue or try again later.'
8

    
9
const registerError =
10
    'Server error occurred while registering. Please contact help service to resolve this issue or try again later.'
11

    
12
// This is not present in the swagger since spring generates
13
export interface UserLogin {
14
    username: string,
15
    password: string
16
}
17

    
18
export interface UserRegister {
19
    name: string,
20
    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
export const logIn = createAsyncThunk(
33
    'user/login',
34
    async (userDto: UserLogin, { getState }) => {
35
        try {
36
            // @ts-ignore
37
            if (getState().user.isLoggedIn) {
38
                return Promise.reject(undefined)
39
            }
40

    
41
            const { data, status } = await axiosInstance.post('/login', userDto)
42
            const [ accessToken, refreshToken ] = [data.access_token, data.refresh_token]
43
            if (status !== 200) {
44
                // TODO read API err
45
                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
            }
54

    
55
            const userState: UserState = {
56
                accessToken,
57
                refreshToken,
58
                username: sub,
59
                roles: authorities,
60
                isLoggingIn: false,
61
                isLoggedIn: true,
62
                isRegistered: false
63
            }
64
            
65
            return userState
66
        } catch (err: any) {
67
            return Promise.reject(loginError)
68
        }
69
    }
70
)
71

    
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
                return Promise.reject(registerError)
82
            }
83
            return status
84
        } catch (err: any) {
85
            return Promise.reject(registerError)
86
        }
87
    }
88
)
(7-7/7)