anthonyjgrove / react-google-login

A React Google Login Component
https://anthonyjgrove.github.io/react-google-login
MIT License
1.84k stars 427 forks source link

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

Open Kennyabby opened 1 year ago

Kennyabby commented 1 year ago

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 commented 1 year ago

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