Skip to content

Commit 67e96d5

Browse files
committed
fix: #400 logout because token expired or corrupted
1 parent e345273 commit 67e96d5

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

frontend/src/hooks/useAuth.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ const isLoggedIn = () => {
1818
}
1919

2020
const useAuth = () => {
21-
const [error, setError] = useState<string | null>(null)
21+
const [errorSetter, setError] = useState<string | null>(null)
2222
const navigate = useNavigate()
2323
const showToast = useCustomToast()
2424
const queryClient = useQueryClient()
25-
const { data: user, isLoading } = useQuery<UserPublic | null, Error>({
25+
const { data: user, isLoading, error, errorUpdateCount } = useQuery<UserPublic | null, Error>({
2626
queryKey: ["currentUser"],
2727
queryFn: UsersService.readUserMe,
2828
enabled: isLoggedIn(),
@@ -92,7 +92,9 @@ const useAuth = () => {
9292
logout,
9393
user,
9494
isLoading,
95+
errorSetter,
9596
error,
97+
errorUpdateCount,
9698
resetError: () => setError(null),
9799
}
98100
}

frontend/src/routes/_layout.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useEffect } from 'react'
2+
13
import { Flex, Spinner } from "@chakra-ui/react"
24
import { Outlet, createFileRoute, redirect } from "@tanstack/react-router"
35

@@ -17,7 +19,13 @@ export const Route = createFileRoute("/_layout")({
1719
})
1820

1921
function Layout() {
20-
const { isLoading } = useAuth()
22+
const { isLoading, error, errorUpdateCount, logout } = useAuth()
23+
useEffect(() => {
24+
if (errorUpdateCount && error?.message === "Forbidden") {
25+
console.log("____Unauth____logout");
26+
logout();
27+
}
28+
}, [errorUpdateCount, error, logout]);
2129

2230
return (
2331
<Flex maxW="large" h="auto" position="relative">

frontend/src/routes/login.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const Route = createFileRoute("/login")({
3838

3939
function Login() {
4040
const [show, setShow] = useBoolean()
41-
const { loginMutation, error, resetError } = useAuth()
41+
const { loginMutation, errorSetter, resetError } = useAuth()
4242
const {
4343
register,
4444
handleSubmit,
@@ -84,7 +84,7 @@ function Login() {
8484
alignSelf="center"
8585
mb={4}
8686
/>
87-
<FormControl id="username" isInvalid={!!errors.username || !!error}>
87+
<FormControl id="username" isInvalid={!!errors.username || !!errorSetter}>
8888
<Input
8989
id="username"
9090
{...register("username", {
@@ -99,7 +99,7 @@ function Login() {
9999
<FormErrorMessage>{errors.username.message}</FormErrorMessage>
100100
)}
101101
</FormControl>
102-
<FormControl id="password" isInvalid={!!error}>
102+
<FormControl id="password" isInvalid={!!errorSetter}>
103103
<InputGroup>
104104
<Input
105105
{...register("password", {
@@ -124,7 +124,7 @@ function Login() {
124124
</Icon>
125125
</InputRightElement>
126126
</InputGroup>
127-
{error && <FormErrorMessage>{error}</FormErrorMessage>}
127+
{errorSetter && <FormErrorMessage>{errorSetter}</FormErrorMessage>}
128128
</FormControl>
129129
<Link as={RouterLink} to="/recover-password" color="blue.500">
130130
Forgot password?

0 commit comments

Comments
 (0)