-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.tsx
114 lines (100 loc) · 2.6 KB
/
authentication.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Spinner } from '@/components/Elements';
import {
login,
getAuthenticatedUserInfo,
register,
UserResponse,
LoginCredentialsDTO,
RegisterCredentialsDTO,
AuthUser,
} from '@/features/auth';
import storage from '@/utils/storage';
import { configureAuth } from 'react-query-auth';
import { useEffect, useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { AxiosError } from 'axios';
async function handleUserResponse(data: UserResponse) {
if(data.accessToken) storage.setToken(data.accessToken);
return data.user;
}
async function userFn() {
if (storage.getToken()) {
const data = await getAuthenticatedUserInfo();
const user = await handleUserResponse(data);
return user;
}
return null;
}
async function loginFn(data: LoginCredentialsDTO) {
const response = await login(data);
const user = await handleUserResponse(response);
return user;
}
async function registerFn(data: RegisterCredentialsDTO) {
const response = await register(data);
const user = await handleUserResponse(response);
return user;
}
async function logoutFn() {
storage.clearToken();
window.location.assign(window.location.origin as unknown as string);
}
const authConfig = {
userFn,
loginFn,
registerFn,
logoutFn,
LoaderComponent() {
return (
<div className="w-screen h-screen flex justify-center items-center">
<Spinner size="xl" />
</div>
);
},
};
export const { AuthLoader, useUser, useLogin, useLogout, useRegister } = configureAuth<
AuthUser | null,
unknown,
LoginCredentialsDTO,
RegisterCredentialsDTO
>(authConfig);
const initialState : AuthUser = {
id : "",
email : "",
name : "",
authenticated : false
}
export const useAuth = () => {
const queryClient = useQueryClient()
const { data , error, refetch, status, isLoading, fetchStatus, isFetching, isError, isFetched, remove, isStale } = useUser({
// I don't use initialData, useQuery always returns initialData on mount and gets the data only on tab focus!
...queryClient.getDefaultOptions().queries
});
const isAuthenticated = data !== null && data !== undefined && data?.email;
const [ auth, setAuth ] = useState<AuthUser>(initialState)
useEffect(()=>{
if(isAuthenticated){
setAuth({
id : data.id,
email : data.email,
name : data.name,
authenticated : true
})
}else{
setAuth(initialState);
}
}, [ data ])
return {
auth,
error,
refetch,
status,
fetchStatus,
isLoading,
isFetching,
isError,
isFetched ,
remove,
isStale
}
}