1 |
930b05fd
|
Schwobik
|
import { createAsyncThunk } from "@reduxjs/toolkit"
|
2 |
|
|
import { axiosInstance } from "../../api/api"
|
3 |
5ed9692c
|
Schwobik
|
import { loginRequest } from "../../api/authservice"
|
4 |
930b05fd
|
Schwobik
|
|
5 |
|
|
export const login = createAsyncThunk(
|
6 |
|
|
"user/login",
|
7 |
5ed9692c
|
Schwobik
|
async (payload: { username: string, password: string }) => {
|
8 |
|
|
try {
|
9 |
|
|
const response = await loginRequest(payload.username, payload.password)
|
10 |
|
|
console.log(response)
|
11 |
|
|
if (response.status === 200) {
|
12 |
|
|
return {
|
13 |
|
|
username: payload.username,
|
14 |
|
|
role: response.data.role
|
15 |
|
|
}
|
16 |
|
|
} else {
|
17 |
|
|
return Promise.reject(response.data ? response.data : "Login failed")
|
18 |
930b05fd
|
Schwobik
|
}
|
19 |
5ed9692c
|
Schwobik
|
} catch (err: any) {
|
20 |
|
|
return Promise.reject(err.response.data)
|
21 |
930b05fd
|
Schwobik
|
}
|
22 |
|
|
}
|
23 |
|
|
)
|