diff --git a/app/(full-page)/auth/google/page.tsx b/app/(full-page)/auth/google/page.tsx index c6136b6..6bd952f 100644 --- a/app/(full-page)/auth/google/page.tsx +++ b/app/(full-page)/auth/google/page.tsx @@ -4,19 +4,22 @@ import { useSearchParams } from 'next/navigation' import { useCookies} from 'react-cookie' import { redirect } from 'next/navigation' import { GoogleSigninService } from '@/service/GoogleSignInService' -import { useEffect, useState } from 'react' +import { useContext , useEffect, useState } from 'react' import { IS_LOGIN } from '../../../../lib/constants' +import { LayoutContext } from '../../../../layout/context/layoutcontext'; +import { classNames } from 'primereact/utils'; +import { Button } from 'primereact/button'; const GoogleAuthPage = () => { + const { layoutConfig } = useContext(LayoutContext); const searchParams = useSearchParams() - - const state = searchParams.get("state"); - const authCode = searchParams.get("code"); const [cookies, setCookies, removeCookie] = useCookies(['state', 'code_verifier']); const clientState = cookies.state; const codeVerifier = cookies.code_verifier; const [isDenyAccess, setIsDenyAccess] = useState(false); const [isLogin, setIsLogin] = useState(false); + const state = searchParams.get("state"); + const authCode = searchParams.get("code"); useEffect(()=>{ if(isDenyAccess) @@ -30,30 +33,79 @@ const GoogleAuthPage = () => { //console.debug(clientState != state); //console.debug("authorisation code", authCode); //console.debug("code verifier", codeVerifier); + if(state !== null || authCode !== null){ + if(clientState != state || !authCode){ + console.log('redirection'); + setIsDenyAccess(true); + } - if(clientState != state || !authCode){ - console.log('redirection'); - setIsDenyAccess(true); + GoogleSigninService.getAuthenticated(codeVerifier, authCode) + .then((data) => { + console.log(data); + //clear cookies + removeCookie('state'); + removeCookie('code_verifier'); + localStorage.setItem(IS_LOGIN, "true" ); + setIsLogin(true); + }) + .catch((err) => { + console.log("getAccessToken", err); + setIsDenyAccess(true); + }) } - - GoogleSigninService.getAuthenticated(codeVerifier, authCode) - .then((data) => { - console.log(data); - //clear cookies - removeCookie('state'); - removeCookie('code_verifier'); - localStorage.setItem(IS_LOGIN, "true" ); - setIsLogin(true); - }) - .catch((err) => { - console.log("getAccessToken", err); - setIsDenyAccess(true); - }) },[]) + async function signIn() { + var state = GoogleSigninService.generateRandomBytes(); + + await GoogleSigninService.generateCodeChallenge() + .then((result:any)=>{ + console.log(result); + setCookies('code_verifier', result.codeVerifier, { + httpOnly: false, + secure: false, + maxAge: 60, //60 secs + path: '/' + }); + setCookies('state', state, { + httpOnly: false, + secure: false, + maxAge: 60, //60 secs + path: '/' + }); + GoogleSigninService.signIn(state, result.codeChallenge); + }); + } + const containerClassName = classNames('surface-ground flex align-items-center justify-content-center min-h-screen min-w-screen overflow-hidden', { 'p-input-filled': layoutConfig.inputStyle === 'filled' }); - return (
Loading
-