Revize 06808454
Přidáno uživatelem Schwobik před téměř 2 roky(ů)
src/api/authservice.ts | ||
---|---|---|
12 | 12 |
|
13 | 13 |
export const isAuthRequest = async () => { |
14 | 14 |
return await axiosInstance.get( |
15 |
"/api/isauth" |
|
15 |
"/isauth" |
|
16 |
) |
|
17 |
} |
|
18 |
|
|
19 |
export const logoutRequest = async () => { |
|
20 |
return await axiosInstance.get( |
|
21 |
"/logout" |
|
16 | 22 |
) |
17 | 23 |
} |
18 | 24 |
|
src/pages/LoginPage.tsx | ||
---|---|---|
11 | 11 |
Text, |
12 | 12 |
KeyboardAvoidingView |
13 | 13 |
} from "native-base" |
14 |
import { useState } from "react" |
|
14 |
import { useCallback, useEffect, useState } from "react"
|
|
15 | 15 |
import { useDispatch, useSelector } from "react-redux" |
16 |
import { login } from "../stores/actions/userThunks" |
|
16 |
import { checkAuth, login } from "../stores/actions/userThunks"
|
|
17 | 17 |
import { AppDispatch, RootState } from "../stores/store" |
18 | 18 |
import { Platform } from "react-native" |
19 |
import { log } from "../logging/logger" |
|
20 |
import * as SplashScreen from "expo-splash-screen" |
|
21 |
import { ApplicationHeading } from "../components/reusables/ApplicationHeading" |
|
19 | 22 |
|
20 | 23 |
|
21 | 24 |
const LoginPage = () => { |
22 | 25 |
const [username, setUsername] = useState("") |
23 | 26 |
const [password, setPassword] = useState("") |
24 |
const status = useSelector((state: RootState) => state.user.loggedIn)
|
|
27 |
const lastError = useSelector((state: RootState) => state.user.lastError)
|
|
25 | 28 |
|
26 | 29 |
const dispatch = useDispatch<AppDispatch>() |
27 | 30 |
|
28 | 31 |
const loginUser = () => { |
32 |
log.debug("LoginPage", "loginUser", "dispatching login") |
|
29 | 33 |
dispatch(login({username, password})) |
30 | 34 |
//TODO - add error handling |
31 | 35 |
} |
32 | 36 |
|
37 |
|
|
38 |
useEffect(() => { |
|
39 |
dispatch(checkAuth()) |
|
40 |
}, []) |
|
41 |
|
|
33 | 42 |
return ( |
34 | 43 |
<KeyboardAvoidingView |
44 |
behavior={ Platform.OS === "ios" ? "padding" : "height" } |
|
45 |
flex={ 1 } |
|
46 |
justifyContent={ "center" } |
|
35 | 47 |
h={ { |
36 | 48 |
base: "400px", |
37 | 49 |
lg: "auto" |
38 | 50 |
} } |
39 |
behavior={ Platform.OS === "ios" ? "padding" : "height" } |
|
40 | 51 |
> |
41 | 52 |
<Center w="100%"> |
42 |
<Box |
|
43 |
safeArea |
|
53 |
<VStack |
|
44 | 54 |
p="2" |
45 |
py="8" |
|
46 |
w="90%" |
|
47 |
maxW="290" |
|
55 |
w="80%" |
|
56 |
justifyContent={ "center" } |
|
48 | 57 |
> |
49 |
<Heading |
|
50 |
size="2xl" |
|
51 |
color="primary.500" |
|
52 |
textAlign="center" |
|
53 |
_dark={ { |
|
54 |
color: "primary.100" |
|
55 |
} } |
|
56 |
> |
|
57 |
Inventaria Rudolphina |
|
58 |
</Heading> |
|
58 |
<ApplicationHeading /> |
|
59 | 59 |
<Heading |
60 | 60 |
mt="10" |
61 | 61 |
textAlign="center" |
... | ... | |
74 | 74 |
> |
75 | 75 |
Please log in to continue |
76 | 76 |
</Text> |
77 |
{ lastError && ( |
|
78 |
<Text |
|
79 |
mt="1" |
|
80 |
textAlign="center" |
|
81 |
color="error.500" |
|
82 |
> |
|
83 |
{ lastError } |
|
84 |
</Text> |
|
85 |
) } |
|
77 | 86 |
|
78 |
<VStack space={ 3 } mt="1">
|
|
87 |
<VStack space={ 3 } > |
|
79 | 88 |
<FormControl> |
80 | 89 |
<FormControl.Label>Username</FormControl.Label> |
81 | 90 |
<Input |
... | ... | |
106 | 115 |
</Button> |
107 | 116 |
</FormControl> |
108 | 117 |
</VStack> |
109 |
</Box>
|
|
118 |
</VStack>
|
|
110 | 119 |
</Center> |
111 | 120 |
</KeyboardAvoidingView> |
112 | 121 |
) |
src/pages/Logout.tsx | ||
---|---|---|
1 | 1 |
import { useDispatch } from "react-redux" |
2 | 2 |
import { useEffect } from "react" |
3 | 3 |
import { AppDispatch } from "../stores/store" |
4 |
import { Heading, HStack, Spinner } from "native-base" |
|
5 |
import { logout } from "../stores/reducers/userSlice"
|
|
4 |
import { Center, Heading, HStack, Spinner } from "native-base"
|
|
5 |
import { logout } from "../stores/actions/userThunks"
|
|
6 | 6 |
|
7 | 7 |
const Logout = () => { |
8 | 8 |
const dispatch = useDispatch<AppDispatch>(); |
9 | 9 |
|
10 | 10 |
useEffect(() => { |
11 | 11 |
dispatch(logout()) |
12 |
}, [dispatch])
|
|
12 |
}, []) |
|
13 | 13 |
|
14 | 14 |
return ( |
15 |
<> |
|
15 |
<Center justifyContent={"center"}>
|
|
16 | 16 |
<HStack space={2} justifyContent="center"> |
17 | 17 |
<Spinner |
18 | 18 |
accessibilityLabel="Loading logout" |
19 |
color="primary.50" |
|
19 |
color="primary.500"
|
|
20 | 20 |
/> |
21 |
<Heading color="primary.50" fontSize="md"> |
|
21 |
<Heading color="primary.500" fontSize="md">
|
|
22 | 22 |
Loading |
23 | 23 |
</Heading> |
24 | 24 |
</HStack> |
25 |
</> |
|
25 |
</Center>
|
|
26 | 26 |
) |
27 | 27 |
} |
28 | 28 |
|
src/stores/actions/userThunks.ts | ||
---|---|---|
1 | 1 |
import { createAsyncThunk } from "@reduxjs/toolkit" |
2 |
import { isAuthRequest, loginRequest } from "../../api/authservice" |
|
2 |
import { isAuthRequest, loginRequest, logoutRequest } from "../../api/authservice" |
|
3 |
import { log } from "../../logging/logger" |
|
3 | 4 |
|
4 | 5 |
export const login = createAsyncThunk( |
5 | 6 |
"user/login", |
6 | 7 |
async (payload: { username: string, password: string }) => { |
7 | 8 |
try { |
8 |
console.log("POST user/login");
|
|
9 |
log.debug("Logging in", payload)
|
|
9 | 10 |
const response = await loginRequest(payload.username, payload.password) |
11 |
log.debug("Login response", response) |
|
10 | 12 |
if (response.status === 200) { |
11 | 13 |
return { |
12 | 14 |
username: payload.username, |
... | ... | |
38 | 40 |
return Promise.reject(err.response.data) |
39 | 41 |
} |
40 | 42 |
} |
43 |
) |
|
44 |
|
|
45 |
export const logout = createAsyncThunk( |
|
46 |
"user/logout", |
|
47 |
async () => { |
|
48 |
try { |
|
49 |
const response = await logoutRequest() |
|
50 |
log.debug("Logout response", response) |
|
51 |
if (response.status === 200) { |
|
52 |
return Promise.resolve() |
|
53 |
} else { |
|
54 |
return Promise.reject(response.data ? response.data : "Logout failed") |
|
55 |
} |
|
56 |
} catch (err: any) { |
|
57 |
return Promise.reject(err.response.data) |
|
58 |
} |
|
59 |
} |
|
41 | 60 |
) |
src/stores/reducers/userSlice.ts | ||
---|---|---|
1 | 1 |
import { createSlice } from "@reduxjs/toolkit" |
2 |
import { checkAuth, login } from "../actions/userThunks" |
|
2 |
import { checkAuth, login, logout } from "../actions/userThunks"
|
|
3 | 3 |
|
4 | 4 |
export interface UserState { |
5 | 5 |
username: string |
6 | 6 |
loggedIn: boolean |
7 | 7 |
role: string |
8 | 8 |
lastError?: string |
9 |
checkedAuth: boolean |
|
9 | 10 |
} |
10 | 11 |
|
11 | 12 |
const initialState: UserState = { |
12 | 13 |
username: "", |
13 | 14 |
loggedIn: false, |
14 |
role: "" |
|
15 |
role: "", |
|
16 |
checkedAuth: false |
|
15 | 17 |
} |
16 | 18 |
|
17 | 19 |
export const userSlice = createSlice({ |
18 | 20 |
name: "user", |
19 | 21 |
initialState: initialState, |
20 | 22 |
reducers: { |
21 |
logout: () => initialState, |
|
23 |
resetState: (state) => { |
|
24 |
state = initialState |
|
25 |
} |
|
22 | 26 |
}, |
23 | 27 |
extraReducers: (builder) => { |
24 | 28 |
builder.addCase(login.fulfilled, (state, action) => { |
... | ... | |
33 | 37 |
builder.addCase(checkAuth.fulfilled, (state, action) => { |
34 | 38 |
state.loggedIn = action.payload.isLogged |
35 | 39 |
state.lastError = "" |
40 |
state.checkedAuth = true |
|
36 | 41 |
}) |
37 | 42 |
builder.addCase(checkAuth.rejected, (state, action) => { |
38 | 43 |
state.lastError = action.error.message |
44 |
state.checkedAuth = true |
|
39 | 45 |
}) |
46 |
builder.addCase(logout.fulfilled, (state, action) => initialState) |
|
40 | 47 |
} |
41 | 48 |
}) |
42 | 49 |
|
43 |
export const { logout } = userSlice.actions
|
|
50 |
export const { resetState } = userSlice.actions
|
|
44 | 51 |
|
45 | 52 |
export default userSlice.reducer |
Také k dispozici: Unified diff
Logout and Login fixed
re #10715