Skip to content

Commit 64fa32a

Browse files
authored
Merge pull request #32 from rezahedi/oauth-onetap
Google One Tap Login
2 parents 0265737 + 537ecdc commit 64fa32a

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useAuth } from "@/context/AuthContext";
2+
import { CredentialResponse, useGoogleOneTapLogin } from "@react-oauth/google";
3+
import { postData } from "@/util";
4+
5+
const GoogleOneTap = () => {
6+
const { login } = useAuth();
7+
8+
useGoogleOneTapLogin({
9+
onSuccess: async (response: CredentialResponse) => {
10+
if (response.credential) handleGoogleLogin(response.credential);
11+
},
12+
onError: async () => {
13+
console.log("Login Failed");
14+
},
15+
cancel_on_tap_outside: true,
16+
use_fedcm_for_prompt: true,
17+
});
18+
19+
const handleGoogleLogin = async (code: string) => {
20+
if (!code) return;
21+
22+
try {
23+
const userData = await postData("auth/login/google", { code }, () => {});
24+
if (userData) {
25+
await login(userData);
26+
}
27+
} catch (err: unknown) {
28+
let errorMessage = "";
29+
if (err instanceof Error) errorMessage = err.message;
30+
// TODO: a toast to show the error: setErrorMessage(`Error sending data to server: ${errorMessage}`);
31+
console.log(`Error sending data to server: ${errorMessage}`);
32+
}
33+
};
34+
35+
return null;
36+
};
37+
38+
export default GoogleOneTap;

src/Components/Header/Header.tsx

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import { Link } from "react-router-dom";
66
import { useAuthModal } from "@/context/AuthModalContext";
77
import HeaderActions from "./HeaderActions";
88
import Banner from "./Banner";
9-
import { CredentialResponse, useGoogleOneTapLogin } from "@react-oauth/google";
10-
import { postData } from "@/util";
119
import { useAuth } from "@/context/AuthContext";
10+
import GoogleOneTap from "./GoogleOneTap";
1211

1312
export const NAV_MENU = [
1413
{ text: "Home", link: "/" },
@@ -26,37 +25,11 @@ const Header = ({ withBanner = true }: { withBanner?: boolean }) => {
2625
closeForgotPassword,
2726
} = useAuthModal();
2827

29-
const { login } = useAuth();
30-
31-
useGoogleOneTapLogin({
32-
onSuccess: async (response: CredentialResponse) => {
33-
if (response.credential) handleGoogleLogin(response.credential);
34-
},
35-
onError: async () => {
36-
console.log("Login Failed");
37-
},
38-
cancel_on_tap_outside: true,
39-
use_fedcm_for_prompt: true,
40-
});
41-
42-
const handleGoogleLogin = async (code: string) => {
43-
if (!code) return;
44-
45-
try {
46-
const userData = await postData("auth/login/google", { code }, () => {});
47-
if (userData) {
48-
await login(userData);
49-
}
50-
} catch (err: unknown) {
51-
let errorMessage = "";
52-
if (err instanceof Error) errorMessage = err.message;
53-
// TODO: a toast to show the error: setErrorMessage(`Error sending data to server: ${errorMessage}`);
54-
console.log(`Error sending data to server: ${errorMessage}`);
55-
}
56-
};
28+
const { user } = useAuth();
5729

5830
return (
5931
<header>
32+
{!user && <GoogleOneTap />}
6033
<div className="bg-background flex items-center py-2">
6134
<Link
6235
to="/"

0 commit comments

Comments
 (0)