Projekt

Obecné

Profil

Stáhnout (2.73 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
                title: `Auth err: ${err}`,
59
                duration: 10000
60
            })
61
            return Promise.reject(err.response ? err.response.data : "Something went wrong" + err)
62 0efa284e Fantič
        }
63
    }
64 06808454 Schwobik
)
65
66
export const logout = createAsyncThunk(
67
    "user/logout",
68
    async () => {
69
        try {
70
            const response = await logoutRequest()
71
            log.debug("Logout response", response)
72
            if (response.status === 200) {
73
                return Promise.resolve()
74
            } else {
75
                return Promise.reject(response.data ? response.data : "Logout failed")
76
            }
77
        } catch (err: any) {
78
            return Promise.reject(err.response.data)
79
        }
80
    }
81 930b05fd Schwobik
)