Projekt

Obecné

Profil

Stáhnout (2.58 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
    username: 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
            }
63
            
64
            return userState
65
        } catch (err: any) {
66
            return Promise.reject(loginError)
67
        }
68
    }
69
)
70

    
71
export const register = createAsyncThunk(
72
    'user/register',
73
    async (userDto: UserRegister) => {
74
        try {
75
            console.log(userDto.passwords.password)
76
            console.log(userDto.passwords.confirmationPassword)
77
            const { data, status } = await axiosInstance.post('/users', userDto)
78
            if (status !== 200) {
79
                // TODO read API err
80
                return Promise.reject(loginError)
81
            }
82
            return status
83
        } catch (err: any) {
84
            return Promise.reject(loginError)
85
        }
86
    }
87
)
(7-7/7)