forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.tsx
More file actions
43 lines (35 loc) · 1.09 KB
/
auth.tsx
File metadata and controls
43 lines (35 loc) · 1.09 KB
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
import { configureAuth } from 'react-query-auth';
import { getUser } from '../api/get-user';
import { LoginInput, loginWithEmailAndPassword } from '../api/login';
import { logout } from '../api/logout';
import { RegisterInput, registerWithEmailAndPassword } from '../api/register';
import { UserResponse } from '../types';
async function handleUserResponse(data: UserResponse) {
const { user } = data;
return user;
}
async function userFn() {
return getUser();
}
async function loginFn(data: LoginInput) {
const response = await loginWithEmailAndPassword(data);
const user = await handleUserResponse(response);
return user;
}
async function registerFn(data: RegisterInput) {
const response = await registerWithEmailAndPassword(data);
const user = await handleUserResponse(response);
return user;
}
async function logoutFn() {
await logout();
// window.location.assign(window.location.origin as unknown as string);
}
const authConfig = {
userFn,
loginFn,
registerFn,
logoutFn,
};
export const { useUser, useLogin, useLogout, useRegister, AuthLoader } =
configureAuth(authConfig);