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 |
930b05fd
|
Schwobik
|
|
5 |
|
|
export const login = createAsyncThunk(
|
6 |
|
|
"user/login",
|
7 |
5ed9692c
|
Schwobik
|
async (payload: { username: string, password: string }) => {
|
8 |
|
|
try {
|
9 |
06808454
|
Schwobik
|
log.debug("Logging in", payload)
|
10 |
5ed9692c
|
Schwobik
|
const response = await loginRequest(payload.username, payload.password)
|
11 |
06808454
|
Schwobik
|
log.debug("Login response", response)
|
12 |
5ed9692c
|
Schwobik
|
if (response.status === 200) {
|
13 |
|
|
return {
|
14 |
|
|
username: payload.username,
|
15 |
b56c1e7d
|
Fantič
|
role: response.data.role,
|
16 |
|
|
userId: response.data.uuid
|
17 |
5ed9692c
|
Schwobik
|
}
|
18 |
|
|
} else {
|
19 |
|
|
return Promise.reject(response.data ? response.data : "Login failed")
|
20 |
930b05fd
|
Schwobik
|
}
|
21 |
5ed9692c
|
Schwobik
|
} catch (err: any) {
|
22 |
d963a25c
|
Michal Schwob
|
return Promise.reject(err.response ? err.response.data : "Something went wrong: " + err)
|
23 |
930b05fd
|
Schwobik
|
}
|
24 |
|
|
}
|
25 |
0efa284e
|
Fantič
|
)
|
26 |
|
|
|
27 |
|
|
export const checkAuth = createAsyncThunk(
|
28 |
|
|
"user/isauth",
|
29 |
84b0fbc3
|
Fantič
|
async () => {
|
30 |
0efa284e
|
Fantič
|
try {
|
31 |
|
|
const response = await isAuthRequest()
|
32 |
|
|
console.log(response)
|
33 |
|
|
if (response.status === 200) {
|
34 |
|
|
return {
|
35 |
b56c1e7d
|
Fantič
|
isLogged: response.data.isauth,
|
36 |
|
|
userId: response.data.uuid
|
37 |
0efa284e
|
Fantič
|
}
|
38 |
|
|
} else {
|
39 |
|
|
return Promise.reject(response.data ? response.data : "Check authentication failed")
|
40 |
|
|
}
|
41 |
|
|
} catch (err: any) {
|
42 |
d963a25c
|
Michal Schwob
|
return Promise.reject(err.response ? err.response.data : "Something went wrong: " + err)
|
43 |
0efa284e
|
Fantič
|
}
|
44 |
|
|
}
|
45 |
06808454
|
Schwobik
|
)
|
46 |
|
|
|
47 |
|
|
export const logout = createAsyncThunk(
|
48 |
|
|
"user/logout",
|
49 |
|
|
async () => {
|
50 |
|
|
try {
|
51 |
|
|
const response = await logoutRequest()
|
52 |
|
|
log.debug("Logout response", response)
|
53 |
|
|
if (response.status === 200) {
|
54 |
|
|
return Promise.resolve()
|
55 |
|
|
} else {
|
56 |
|
|
return Promise.reject(response.data ? response.data : "Logout failed")
|
57 |
|
|
}
|
58 |
|
|
} catch (err: any) {
|
59 |
|
|
return Promise.reject(err.response.data)
|
60 |
|
|
}
|
61 |
|
|
}
|
62 |
930b05fd
|
Schwobik
|
)
|