Skip to content

Commit 0941d76

Browse files
authored
Merge pull request #31 from rezahedi/oauth-onetap
Activate One Tap Login and Pass Credential to Backend API
2 parents 65bddd8 + 1049ae0 commit 0941d76

File tree

6 files changed

+54
-34
lines changed

6 files changed

+54
-34
lines changed

src/Components/Auth/GoogleLoginButton.tsx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
import React from "react";
2-
import { CodeResponse, useGoogleLogin } from "@react-oauth/google";
3-
import { Button } from "../ui/button";
4-
import { Google } from "@/ui/icons";
2+
import { CredentialResponse, GoogleLogin } from "@react-oauth/google";
53

64
const GoogleLoginButton = ({
75
onLogin,
86
}: {
97
onLogin: (code: string) => void;
108
}) => {
11-
const handleLogin = useGoogleLogin({
12-
flow: "auth-code",
13-
onSuccess: async (response: CodeResponse) => {
14-
onLogin(response.code);
15-
},
16-
onError: async (errorResponse) => {
17-
console.log("Login Failed", errorResponse);
18-
},
19-
});
20-
219
return (
22-
<Button
23-
onClick={handleLogin}
24-
variant="ghost"
25-
className="my-2 w-full p-5 font-normal text-lg bg-foreground text-background hover:bg-foreground hover:text-background active:scale-95"
26-
>
27-
<Google className="size-8" /> Sign in with Google
28-
</Button>
10+
<GoogleLogin
11+
onSuccess={(response: CredentialResponse) => {
12+
if (response.credential) onLogin(response.credential);
13+
}}
14+
text="continue_with"
15+
theme="filled_blue"
16+
/>
2917
);
3018
};
3119

src/Components/Auth/Login.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { useAuthModal } from "@/context/AuthModalContext";
55
import { User } from "@/types";
66
import { Dialog, DialogContent } from "@/Components/ui/dialog";
77
import { Button } from "@/Components/ui/button";
8-
import { GoogleOAuthProvider } from "@react-oauth/google";
98
import GoogleLoginButton from "./GoogleLoginButton";
109

1110
const LoginPage = ({
@@ -137,11 +136,7 @@ const LoginPage = ({
137136
{/* Left Section */}
138137
<div className="flex-1 md:flex-1/2 px-2 py-3 space-y-2">
139138
<h5 className="flex justify-center text-3xl">Sign In</h5>
140-
<GoogleOAuthProvider
141-
clientId={import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_ID}
142-
>
143-
<GoogleLoginButton onLogin={handleGoogleLogin} />
144-
</GoogleOAuthProvider>
139+
<GoogleLoginButton onLogin={handleGoogleLogin} />
145140
<div className="my-4 text-foreground/60 text-center">OR</div>
146141
<label className="block">
147142
Email:

src/Components/Auth/Register.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { postData } from "@/util";
55
import { useAuth } from "@/context/AuthContext";
66
import { useAuthModal } from "@/context/AuthModalContext";
77
import { User } from "@/types";
8-
import { GoogleOAuthProvider } from "@react-oauth/google";
98
import GoogleLoginButton from "./GoogleLoginButton";
109

1110
const RegisterPage = ({
@@ -159,11 +158,7 @@ const RegisterPage = ({
159158
<h5 className="flex justify-center text-3xl">
160159
Create Your Account
161160
</h5>
162-
<GoogleOAuthProvider
163-
clientId={import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_ID}
164-
>
165-
<GoogleLoginButton onLogin={handleGoogleLogin} />
166-
</GoogleOAuthProvider>
161+
<GoogleLoginButton onLogin={handleGoogleLogin} />
167162
<div className="my-4 text-foreground/60 text-center">OR</div>
168163
<label className="block">
169164
Name:

src/Components/Header/Header.tsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ 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";
11+
import { useAuth } from "@/context/AuthContext";
912

1013
export const NAV_MENU = [
1114
{ text: "Home", link: "/" },
@@ -23,6 +26,35 @@ const Header = ({ withBanner = true }: { withBanner?: boolean }) => {
2326
closeForgotPassword,
2427
} = useAuthModal();
2528

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+
};
57+
2658
return (
2759
<header>
2860
<div className="bg-background flex items-center py-2">

src/Components/Layout/MainLayout.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import React, { useEffect } from "react";
22
import { Outlet, useLocation } from "react-router-dom";
33
import Header from "@/Components/Header";
44
import Footer from "@/Components/Footer";
5+
import { GoogleOAuthProvider } from "@react-oauth/google";
56

67
export default function MainLayout() {
78
return (
89
<>
910
<ScrollToTop />
1011
<div className="relative mx-auto px-2 sm:px-3 w-full max-w-[1300px] box-border">
11-
<Header />
12+
<GoogleOAuthProvider
13+
clientId={import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_ID}
14+
>
15+
<Header />
16+
</GoogleOAuthProvider>
1217
</div>
1318

1419
<div className="my-5 mx-auto px-2 sm:px-3 w-full max-w-[1300px] box-border">

src/Components/Layout/MapLayout.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from "react";
22
import { Outlet } from "react-router-dom";
33
import Header from "@/Components/Header";
44
import { APIProvider } from "@vis.gl/react-google-maps";
5+
import { GoogleOAuthProvider } from "@react-oauth/google";
56

67
const GOOGLE_MAPS_API_KEY = import.meta.env.VITE_GOOGLE_MAP_API_KEY;
78

@@ -14,7 +15,11 @@ function MapLayout() {
1415
return (
1516
<div className="flex flex-col h-screen">
1617
<div className="relative mx-auto px-2 sm:px-3 w-full max-w-[1300px] box-border">
17-
<Header withBanner={false} />
18+
<GoogleOAuthProvider
19+
clientId={import.meta.env.VITE_GOOGLE_OAUTH_CLIENT_ID}
20+
>
21+
<Header withBanner={false} />
22+
</GoogleOAuthProvider>
1823
</div>
1924

2025
<div className="flex-1">

0 commit comments

Comments
 (0)