Skip to content
This repository has been archived by the owner on Sep 21, 2023. It is now read-only.

OnFailure Method is silently called when page loads and when successfully logged in to Google. #551

Open
Kennyabby opened this issue Nov 19, 2022 · 1 comment

Comments

@Kennyabby
Copy link

I don't know if this issue as being raised here before, but i have searched and i haven't seen anything related.

This is my code.

`import { React, useState, useEffect, useRef } from 'react'
import { GoogleLogin } from 'react-google-login'

import usrImg from './usrImg.png'
import viewImg from './view.jpg'
import noViewImg from './noview.png'
import signinwall from './signin-wall.jpg'

const Signin = ({ showNavbar, showNavOpt, sendId, server }) => {

const [passValidated, setPassValidated] = useState(false)
const [signView, setSignView] = useState('Sign in')
const [error, setError] = useState('')

const handleGoogleSuccess = async (googleData) => {
setSignView('hold on...')
console.log('successful')
const res = await fetch('/api/v1/auth/google', {
method: 'POST',
body: JSON.stringify({
token: googleData.tokenId,
}),
headers: {
'Content-Type': 'application/json',
},
})
const data = await res.json()
console.log(data)
const user = data.user
console.log(user)
const res1 = await fetch('/isEmailPresent', {
method: 'POST',
body: JSON.stringify({
schoolEmail: user.email,
}),
headers: {
'Content-Type': 'application/json',
},
})
const resp = await res1.json()
const isPresent = resp.isPresent
if (isPresent) {
const user_id = resp.id
setSignView('Signin in...')
setFields((fields) => {
return { ...fields, id: user_id }
})
setPassValidated(true)
} else {
setPassValidated(false)
setError('This Email is not Authorized, Use Registered School Email!')
setTimeout(() => {
setError('')
}, 5000)
setSignView('Sign in')
}
}
const handleGoogleFailure = async () => {
setPassValidated(false)
setError('No Email To Validate!')
setTimeout(() => {
setError('')
}, 5000)
setSignView('Sign in')
}
return (
<>
<GoogleLogin
clientId='Google client id'
buttonText='Sign in with Google'
onSuccess={handleGoogleSuccess}
onFailure={handleGoogleFailure}
cookiePolicy={'single_host_origin'}
/>
</>
)
}

export default Signin
`

@fitimbytyqi
Copy link

@Kennyabby If you are testing locally be sure your url is localhost:{port}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants