Commit f638aae1 authored by Alex Segers's avatar Alex Segers

[AFP-111] ️ Refactor custom hook 'useGoogleAuth' with types & service call (@asegers)

parent 0db4d364
import React from 'react'
import { useGoogleLogin, useGoogleLogout } from 'react-google-login'
import { createContext, useContext, useEffect, useState, FC } from 'react'
import { GoogleLoginResponse, useGoogleLogin, useGoogleLogout } from 'react-google-login'
import { useHistory } from 'react-router-dom'
import { Manager } from 'Manager'
const GoogleAuthContext = React.createContext<any>({});
import { ManagerService } from 'services'
import { tokenStorage } from 'utils'
export const useGoogleAuth = () => React.useContext(GoogleAuthContext);
export default useGoogleAuth;
export const GoogleAuthProvider: React.FC = ({ children }) => {
const {push: historyPush} = useHistory();
const [isSignedIn, setIsSignedIn] = React.useState(false)
const [staySignedIn, setStaySignedIn] = React.useState(true)
const [manager, setManager] = React.useState<Manager | null>()
const [authError, setAuthError] = React.useState<String | null>()
const GoogleAuthContext = createContext<any>({});
const { signIn, loaded, } = useGoogleLogin({
export const useGoogleAuth = () => useContext(GoogleAuthContext);
export default useGoogleAuth;
export const GoogleAuthProvider: FC = ({ children }) => {
const { push: historyPush } = useHistory();
const [isSignedIn, setIsSignedIn] = useState(false)
const [staySignedIn, setStaySignedIn] = useState(true)
const [manager, setManager] = useState<Manager | null>()
const [authError, setAuthError] = useState<string | null>()
const [accessToken, setAccessToken] = useState<string | null>()
const { signIn, loaded } = useGoogleLogin({
clientId: (process.env.REACT_APP_GOOGLE_CLIENT_ID as string),
onSuccess: (res: any) => {
const {
email,
familyName: lastName,
givenName:firstName,
googleId,
imageUrl
} = res.profileObj;
setManager(({
profileObj: {
email,
familyName: lastName,
givenName: firstName,
googleId,
imageUrl
},
tokenId: token
} = res;
const managerData: Manager = {
email,
firstName,
lastName,
googleId,
imageUrl
}))
}
setManager(managerData)
setAccessToken(token)
console.log(token)
// ManagerService.existByEmail(email) // GET api/managers/[email]
// .then((exists: boolean) => {
// if (exists) {
// ManagerService.isLoggedin(email)
// } else {
// ManagerService.create([managerData])
// }
// }).finally(() => {
setIsSignedIn(true);
historyPush("/orders");
// })
ManagerService.authenticate(token)
.then(() => {
setIsSignedIn(true)
historyPush("/orders")
})
},
onFailure: (err) => {
console.error("GOOGLE AUTH SIGN-IN ERROR: ", err)
setAuthError(`Ooops. Something went wrong: ${err?.message}`);
setAuthError(`Ooops. Something went wrong: ${err?.message}`)
},
isSignedIn: staySignedIn,
cookiePolicy: 'single_host_origin'
......@@ -58,16 +64,21 @@ export const GoogleAuthProvider: React.FC = ({ children }) => {
const { signOut } = useGoogleLogout({
clientId: (process.env.REACT_APP_GOOGLE_CLIENT_ID as string),
onLogoutSuccess: () => {
setIsSignedIn(false);
setIsSignedIn(false)
historyPush("/")
},
onFailure: () => {
console.error("A GOOGLE AUTH SIGN-OUT ERROR OCCURED")
setAuthError("Ooops. Something went wrong");
setAuthError("Ooops. Something went wrong")
},
})
useEffect(() => {
if (accessToken) tokenStorage.set(accessToken);
}, [accessToken])
const authContextValues = {
accessToken,
authError,
isSignedIn,
loaded,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment