-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogin.tsx
37 lines (35 loc) · 886 Bytes
/
Login.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
import { CredentialResponse, GoogleLogin } from "@react-oauth/google";
import axios from "axios";
import jwt_decode from "jwt-decode";
import useAuthStore from "../store/authStore";
import { BASE_URL } from "../utils/constants";
const Login = () => {
const { addUser } = useAuthStore();
const loginUser = async (res: CredentialResponse) => {
const decoded: {
name: string;
picture: string;
sub: string;
email: string;
} = jwt_decode(`${res.credential}`);
const { name, picture, sub, email } = decoded;
const userDoc = {
_type: "user",
_id: sub,
avatar: picture,
userName: name,
email,
};
const { data } = await axios.post(`${BASE_URL}/api/auth`, userDoc);
addUser(data);
};
return (
<GoogleLogin
onSuccess={loginUser}
auto_select={true}
onError={() => console.log("Login Failed")}
useOneTap
/>
);
};
export default Login;