DEV Community

loading...
Cover image for Add sign in with google to react app using firebase

Add sign in with google to react app using firebase

Cody Snell
Software engineer based out of Tx
・5 min read

Firebase is a great way to set up authentication on your react applications, and with just a few steps it can be implemented very easy

Steps:

  • Create react app
  • Yarn add / npm install firebase
  • Create firebase project
  • Enable sign in with google in firebase console
  • Initialize firebase settings in react
  • Initialize state
  • Create sign in/out functions
  • Be a Pro

As with any project we're going to start in our terminal. Create a folder with any name you want, for this lesson I used firebase-practice.
Screen Shot 2021-05-25 at 3.30.12 PM

cd into your newly made folder and type the command
npx create-react-app .
This will create the react app inside the current folder.
Screen Shot 2021-05-25 at 3.32.05 PM

Now let's open it in VSCode.

You should have the basic React app setup.
Lets clean up that App.js file.
Delete everything inside the return except for the App div.Screen Shot 2021-05-25 at 4.30.19 PM

Open your built in terminal (control + `)

Now were going to add the firebase package.
For this you can use

  • npm install firebase
  • yarn add firebase

At this point we need firebase set up online so let's head there.

Firebase

After signing into Firebase click Go to console
Screen Shot 2021-05-25 at 3.36.30 PM

From the consoles main page you're going to want to add a new project Screen Shot 2021-05-25 at 3.36.41 PM

Give your project a name

Screen Shot 2021-05-25 at 4.40.23 PM

** Turn off analytics (this is optional)**

After your project is created you'll be redirected to the project's main page. Here we're going to add a web app. It is the </> symbol.
Screen Shot 2021-05-25 at 3.40.01 PM

Give your App a nickname and click Register App
Screen Shot 2021-05-25 at 4.46.49 PM

After the App is registered it will give you multiple scripts , we only need what's inside the second script.
Screen Shot 2021-05-25 at 3.40.48 PM

Copy what yours gives you as every project is different and you'll need your specific keys for this to work.

Last step here is to enable signing in with google for your app. Go back to the projects main page and select Authentication> Sign-in Method and then edit the option for google.
Screen Shot 2021-05-25 at 3.42.20 PM

Select Enable and enter your email address then hit Save.
Screen Shot 2021-05-25 at 3.42.40 PM

Congrats we're done in the firebase console!

Head back on over to your VSCode and react app.

Create a new file under the src folder named firebase.js

Inside firebase.js

  • on Line 1 import firebase from 'firebase'

  • on Line 3 paste your firebase configuration you just copied.
    Screen Shot 2021-05-25 at 4.53.40 PM

Now Were going to set up a few variables inside firebase.js

first initialize firebase

  • const firebaseApp=firebase.initializeApp(firebaseConfig)

Set database,auth, and provider

  • const db = firebaseApp.firestore()
  • const auth = firebase.auth()
  • const provider = new firebase.auth.GoogleAuthProvider()

With that were done setting up variables, time to export!

  • export { auth, provider }
  • export default db

firebase.js is now complete, don't forget to save!
Screen Shot 2021-05-25 at 3.52.07 PM

Back to App.js

Lets start in App.js with the imports were going to need

  • import { useState } from 'react'
  • import { auth, provider } from './firebase'

Under Function App(){ and above return( is where we're going to be working

First things first let's initialize state with useState().

  • const [user,setUser] = useState() Screen Shot 2021-05-25 at 4.08.16 PM

Then we'll create our sign in function.

  • const signIn = () => { auth .signInWithPopup(provider) .then((payload) => { setUser(payload.user.displayName) }) .catch((err) => console.log(err)) } Screen Shot 2021-05-25 at 4.10.02 PM

Here you can chain a .then((payload) =>{console.log(payload)) so you can view the object you get back from firebase when signing in. This is how we would know to use payload.user.displayName

Now for our sign out function.

  • const signOut = () => { auth.signOut().then(setUser(null)) } Screen Shot 2021-05-25 at 4.10.15 PM

That's it for our functions, we're on the home stretch!!

Inside of our

we're going to make two buttons. You guessed in, one for sign in, and one for sign out.
  • < button onClick={signIn}>Sign in
  • < button onClick={signOut}>Sign out Screen Shot 2021-05-25 at 4.10.32 PM

Under those buttons were going to use a ternary operator based on state that displays the user if you're signed in or "No user" if you're not.

  • {user ? < div> {user}< /div> : < div> No user< /div>} Screen Shot 2021-05-25 at 4.10.40 PM

And with that we're done!
Screen Shot 2021-05-25 at 4.11.09 PM

Save your files and npm start/yarn start in your terminal and you should see
Screen Shot 2021-05-25 at 4.11.23 PM

Click the sign in button and your App component will update and show the user's name.
Screen Shot 2021-05-25 at 4.11.29 PM

Congratulations you did it!!

Discussion (0)