DEV Community

Cover image for Firebase Google Sign in with React

Firebase Google Sign in with React

Mary Gathoni on August 17, 2020

Introduction Firebase Authentication provides an easy way to log in users using their already existing social accounts. In this tutorial...
Collapse
 
thebenforce profile image
Ben Force

Very well written, thanks! I'm pretty new at React, so learning about useContext was an added bonus.

Collapse
 
gathoni profile image
Mary Gathoni

You're welcome 😊

Collapse
 
imabigfan profile image
imabigfan

Hi Mary,
I am trying to use your code for a few days. Keep getting the following message:
Unhandled Rejection (TypeError): Cannot destructure property 'displayName' of 'user' as it is null.
Getting pretty frustrated trying to solve it.

Can you please... help me?

Thread Thread
 
gavranha profile image
Jocimar Lopes

You can use a condition to check IF user is there. Something like this:

useEffect(() => {
    auth.onAuthStateChanged(async (user) => {
        if(user){
            const { displayName, email } = user;
            setUser({
                displayName,
                email,
            });
        }
    });
}, []);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gavranha profile image
Jocimar Lopes • Edited

Hello, Mary, I've been looking for a tuto like this for two days. Have found several pro devs guides and none of them managed to put things in such a clean way.

To be honest, I'm a newbie with React, have little experience with ES6 and some years as a Java developer. So, I was looking for something that I could not only copy/paste. I was looking for a guide in which I could understand the logic of hooks and Context Providers applied to Firebase authentication.

Your write up is awesome, because I can debug it, if needed. And it is short. And it is clean :)

Thank you very much and keep up with the good job.

Collapse
 
gavranha profile image
Jocimar Lopes

If you got this error:
Attempted import error: 'auth' is not exported from 'firebase/app'

try this import:
import firebase from "firebase/app";
require("firebase/auth");

more info here
github.com/firebase/firebaseui-web...

Collapse
 
kathcode profile image
Catalina Meneses

I you're using "firebase": "^9.0.2", read this firebase.google.com/docs/web/modul...

Collapse
 
sk8guerra profile image
Jorge Guerra

Believe me! This is the best react context tutorial I've found. Thanks for sharing. 🙏🏼

Collapse
 
1akshat profile image
Akki • Edited

What if I want a user to redirect to login page on doing logout?
Is this a good idea?

import { Redirect } from "react-router-dom";
export const logOut = () => {
auth
.signOut()
.then(() => {
console.log("logged out..");
return ;
})
.catch((error) => {
console.warn(error.message);
});
};

Collapse
 
abhishek150190 profile image
Abhishek150190

Its not Redirecting after logging in, can anybody tell

Collapse
 
abhishek150190 profile image
Abhishek150190

I think you have missed two things in the article one is while declaring the UserProvider.js "export default (props)"-props inside the brackets and secondly adding a return statement before redirect. Overall your article was pretty insightful .Keep up the good work buddy

Collapse
 
gathoni profile image
Mary Gathoni

I'll look into it and make changes. Thanks for pointing that out

Collapse
 
lethal254 profile image
lethal254

Thanks for this. I am an upcoming Kenyan developer too and am glad to see we are well represented.

Collapse
 
gramsco profile image
Antonin

Perfect, thanks !

Collapse
 
dewanshdt profile image
dewanshDT

This is exactly what I was looking for, thank you.

Collapse
 
restuta profile image
Anton Vynogradenko • Edited

I think:

 if (redirect) {
    <Redirect to={redirect}/>
  }
Enter fullscreen mode Exit fullscreen mode

should be:

 if (redirect) {
    return <Redirect to={redirect}/>
 }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kdfriedman profile image
Kevin

Brilliantly written, well done!

Collapse
 
curlyfriesplease profile image
curlyfriesplease

Thank you so much Mary, this is exactly what I needed :)
Really helpful!

Collapse
 
anupamdubey8823 profile image
Anupam Dubey

This is the first article I came across where actually every step end-to-end is well "articulated". Thanks for this!

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

Hey @Mary Gathoni if you look at Userprovider.js file there will be an error when the user gets logged out because when the user logged out, doesn't have properties like displayName and email.