From 0185bf179a60f52b451b42564b663c49a184f1eb Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Fri, 31 Mar 2023 17:58:56 +0600 Subject: [PATCH] chore: google login implementation --- src/components/Login/LoginComponent.tsx | 50 +----------- src/components/Login/account-nav.tsx | 6 +- src/components/Login/google-login.tsx | 104 ++++++++++++++++++++++++ src/pages/signup.astro | 57 +++---------- 4 files changed, 118 insertions(+), 99 deletions(-) create mode 100644 src/components/Login/google-login.tsx diff --git a/src/components/Login/LoginComponent.tsx b/src/components/Login/LoginComponent.tsx index 41103577c..8445e1af4 100644 --- a/src/components/Login/LoginComponent.tsx +++ b/src/components/Login/LoginComponent.tsx @@ -1,5 +1,6 @@ import type { FunctionComponent } from 'preact'; import EmailLoginForm from './email-login-form'; +import GoogleLoginButton from './google-login'; export default function LoginComponent() { return ( @@ -66,52 +67,3 @@ export const GithubLoginButton: FunctionComponent<{ className?: string }> = ({ ); }; - -export const GoogleLoginButton: FunctionComponent<{ className?: string }> = ({ - className, -}) => { - return ( - - ); -}; - -function GoogleLogo() { - return ( - - - - - - - - - ); -} diff --git a/src/components/Login/account-nav.tsx b/src/components/Login/account-nav.tsx index 1e2650fc9..b9b3c82dd 100644 --- a/src/components/Login/account-nav.tsx +++ b/src/components/Login/account-nav.tsx @@ -12,7 +12,7 @@ export default function AccountNavigation() {
{isLoading ? (
- +
) : ( <> @@ -32,10 +32,10 @@ export default function AccountNavigation() { ); } -function Spinner() { +export function Spinner({className}: {className?: string}) { return ( (false); + + const handleRedirect = () => { + setIsLoading(true); + fetch('http://localhost:8080/v1-google-login', { + credentials: 'include', + }) + .then((res) => res.json()) + .then((data) => { + setIsLoading(false); + window.location.href = data.loginUrl; + }); + }; + + useEffect(() => { + // Get all query params and send them to v1-google-callback + const urlParams = new URLSearchParams(window.location.search); + const code = urlParams.get('code'); + const state = urlParams.get('state'); + const prompt = urlParams.get('prompt'); + + if (code && state && prompt) { + setIsLoading(true); + fetch( + `http://localhost:8080/v1-google-callback${window.location.search}`, + { + method: 'GET', + credentials: 'include', + } + ) + .then((res) => res.json()) + .then((data) => { + if (data.token) { + Cookies.set(TOKEN_COOKIE_NAME, data.token); + setIsLoading(false); + window.location.href = '/'; + } + }) + .catch((err) => { + console.log(err); + }); + } + }, []); + + return ( + Continue with Google + + )} + + ); +} + +function GoogleLogo() { + return ( + + + + + + + + + ); +} diff --git a/src/pages/signup.astro b/src/pages/signup.astro index cf551a7e5..676906ee2 100644 --- a/src/pages/signup.astro +++ b/src/pages/signup.astro @@ -1,12 +1,9 @@ --- import CaptchaFields from '../components/Captcha/CaptchaFields.astro'; import CaptchaScripts from '../components/Captcha/CaptchaScripts.astro'; -import { - Divider, - GithubLoginButton, - GoogleLoginButton, -} from '../components/Login/LoginComponent'; +import { Divider, GithubLoginButton } from '../components/Login/LoginComponent'; import EmailLoginForm from '../components/Login/email-login-form'; +import GoogleLoginButton from '../components/Login/google-login'; import BaseLayout from '../layouts/BaseLayout.astro'; --- @@ -32,12 +29,8 @@ import BaseLayout from '../layouts/BaseLayout.astro';

-
- - - - -
+ + @@ -49,40 +42,10 @@ import BaseLayout from '../layouts/BaseLayout.astro';