Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 0efa284e

Přidáno uživatelem Fantič před asi 2 roky(ů)

#re 10343 Check authentication request added

Zobrazit rozdíly:

src/api/authservice.ts
8 8
            password: password
9 9
        }
10 10
    )
11
}
11
}
12

  
13
export const isAuthRequest = async () => {
14
    return await axiosInstance.get(
15
        "/api/isauth"
16
    )
17
}
18

  
19

  
src/api/itemservice.ts
1
import { axiosInstance } from "./api"
src/stores/actions/userThunks.ts
1 1
import { createAsyncThunk } from "@reduxjs/toolkit"
2
import { axiosInstance } from "../../api/api"
3
import { loginRequest } from "../../api/authservice"
2
import { isAuthRequest, loginRequest } from "../../api/authservice"
4 3

  
5 4
export const login = createAsyncThunk(
6 5
    "user/login",
......
20 19
            return Promise.reject(err.response.data)
21 20
        }
22 21
    }
22
)
23

  
24
export const checkAuth = createAsyncThunk(
25
    "user/isauth",
26
    async (payload: {
27
        isauth: boolean
28
    }) => {
29
        try {
30
            const response = await isAuthRequest()
31
            console.log(response)
32
            if (response.status === 200) {
33
                return {
34
                    isLogged: payload.isauth
35
                }
36
            } else {
37
                return Promise.reject(response.data ? response.data : "Check authentication failed")
38
            }
39
        } catch (err: any) {
40
            return Promise.reject(err.response.data)
41
        }
42
    }
23 43
)
src/stores/reducers/userSlice.ts
1 1
import { createSlice } from "@reduxjs/toolkit"
2
import { login } from "../actions/userThunks"
2
import { checkAuth, login } from "../actions/userThunks"
3 3

  
4 4
export interface UserState {
5 5
    username: string
......
30 30
        builder.addCase(login.rejected, (state, action) => {
31 31
            state.lastError = action.error.message
32 32
        })
33
        builder.addCase(checkAuth.fulfilled, (state, action) => {
34
            state.loggedIn = action.payload.isLogged
35
            state.lastError = ""
36
        })
37
        builder.addCase(checkAuth.rejected, (state, action) => {
38
            state.lastError = action.error.message
39
        })
33 40
    }
34 41
})
35 42

  

Také k dispozici: Unified diff