Projekt

Obecné

Profil

Stáhnout (2.93 KB) Statistiky
| Větev: | Tag: | Revize:
1 930b05fd Schwobik
import { createAsyncThunk } from "@reduxjs/toolkit"
2 06808454 Schwobik
import { isAuthRequest, loginRequest, logoutRequest } from "../../api/authservice"
3
import { log } from "../../logging/logger"
4 9fa2b717 Michal Schwob
import {Toast} from "native-base"
5 930b05fd Schwobik
6
export const login = createAsyncThunk(
7
    "user/login",
8 5ed9692c Schwobik
    async (payload: { username: string, password: string }) => {
9
        try {
10 06808454 Schwobik
            log.debug("Logging in", payload)
11 5ed9692c Schwobik
            const response = await loginRequest(payload.username, payload.password)
12 06808454 Schwobik
            log.debug("Login response", response)
13 5ed9692c Schwobik
            if (response.status === 200) {
14
                return {
15
                    username: payload.username,
16
                    role: response.data.role
17
                }
18
            } else {
19 9fa2b717 Michal Schwob
                Toast.show({
20
                    title: `Login not 200 (${response.status}): ${response}`,
21
                    duration: 10000
22
                })
23 5ed9692c Schwobik
                return Promise.reject(response.data ? response.data : "Login failed")
24 930b05fd Schwobik
            }
25 5ed9692c Schwobik
        } catch (err: any) {
26 9fa2b717 Michal Schwob
            Toast.show({
27
                title: `Login err: ${err}`,
28
                duration: 10000
29
            })
30
            return Promise.reject(err.response ? err.response.data : "Something went wrong" + err)
31 930b05fd Schwobik
        }
32
    }
33 0efa284e Fantič
)
34
35
export const checkAuth = createAsyncThunk(
36
    "user/isauth",
37 84b0fbc3 Fantič
    async () => {
38 0efa284e Fantič
        try {
39
            const response = await isAuthRequest()
40
            console.log(response)
41
            if (response.status === 200) {
42 9fa2b717 Michal Schwob
                Toast.show({
43
                    title: `Auth ok: ${response.data}`,
44
                    duration: 10000
45
                })
46 0efa284e Fantič
                return {
47 84b0fbc3 Fantič
                    isLogged: response.data.isauth
48 0efa284e Fantič
                }
49
            } else {
50 9fa2b717 Michal Schwob
                Toast.show({
51
                    title: `Auth err: ${response.data ? response.data : "Check authentication failed"}`,
52
                    duration: 10000
53
                })
54 0efa284e Fantič
                return Promise.reject(response.data ? response.data : "Check authentication failed")
55
            }
56
        } catch (err: any) {
57 9fa2b717 Michal Schwob
            Toast.show({
58 e92ebc5c Michal Schwob
                title: `Auth config: ${err.config}`,
59 9fa2b717 Michal Schwob
                duration: 10000
60
            })
61 e92ebc5c Michal Schwob
62
            Toast.show({
63
                title: `Auth message: ${err.message}`,
64
                duration: 10000
65
            })
66
            return Promise.reject(`response: ${JSON.stringify(err.response?.data)}, request: ${JSON.stringify(err.request)}, config: ${JSON.stringify(err.config)}` )
67 0efa284e Fantič
        }
68
    }
69 06808454 Schwobik
)
70
71
export const logout = createAsyncThunk(
72
    "user/logout",
73
    async () => {
74
        try {
75
            const response = await logoutRequest()
76
            log.debug("Logout response", response)
77
            if (response.status === 200) {
78
                return Promise.resolve()
79
            } else {
80
                return Promise.reject(response.data ? response.data : "Logout failed")
81
            }
82
        } catch (err: any) {
83
            return Promise.reject(err.response.data)
84
        }
85
    }
86 930b05fd Schwobik
)