Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import React, { useState } from 'react'; import { GoogleLogin, GoogleLogout } from 'react-google-login'; const clientId = "Your-Client-Id"; function Login() { const [showloginButton, setShowloginButton] = useState(true); const [showlogoutButton, setShowlogoutButton] = useState(false); const onLoginSuccess = (res) => { console.log('Login Success:', res.profileObj); setShowloginButton(false); setShowlogoutButton(true); }; const onLoginFailure = (res) => { console.log('Login Failed:', res); }; const onSignoutSuccess = () => { alert("You have been logged out successfully"); console.clear(); setShowloginButton(true); setShowlogoutButton(false); }; return ( <div> { showloginButton ? <GoogleLogin clientId={clientId} buttonText="Sign In" onSuccess={onLoginSuccess} onFailure={onLoginFailure} cookiePolicy={'single_host_origin'} isSignedIn={true} /> : null} { showlogoutButton ? <GoogleLogout clientId={clientId} buttonText="Sign Out" onLogoutSuccess={onSignoutSuccess} > </GoogleLogout> : null } </div> ); } export default Login; |
This is the App
component where we have the simple google login
button for this you need to install the below library using the npm
command as shown below
npm i react-google-login
And then it will also have the logout
button and it will show the profile
of the user in the dashboard page as shown below