<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cody Snell</title>
    <description>The latest articles on DEV Community by Cody Snell (@decibullz).</description>
    <link>https://dev.to/decibullz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F606178%2F6c18b818-8808-4374-8561-4300c3f96c3d.jpeg</url>
      <title>DEV Community: Cody Snell</title>
      <link>https://dev.to/decibullz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/decibullz"/>
    <language>en</language>
    <item>
      <title>Add sign in with google to react app using firebase</title>
      <dc:creator>Cody Snell</dc:creator>
      <pubDate>Tue, 25 May 2021 22:35:15 +0000</pubDate>
      <link>https://dev.to/decibullz/add-sign-in-with-google-to-react-app-using-firebase-2c4p</link>
      <guid>https://dev.to/decibullz/add-sign-in-with-google-to-react-app-using-firebase-2c4p</guid>
      <description>&lt;p&gt;&lt;strong&gt;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&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create react app&lt;/li&gt;
&lt;li&gt;Yarn add / npm install firebase&lt;/li&gt;
&lt;li&gt;Create firebase project&lt;/li&gt;
&lt;li&gt;Enable sign in with google in firebase console&lt;/li&gt;
&lt;li&gt;Initialize firebase settings in react&lt;/li&gt;
&lt;li&gt;Initialize state&lt;/li&gt;
&lt;li&gt;Create sign in/out functions&lt;/li&gt;
&lt;li&gt;Be a Pro&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hlt69lwqw03jo13c7ai.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hlt69lwqw03jo13c7ai.png" alt="Screen Shot 2021-05-25 at 3.30.12 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;cd into your newly made folder and type the command&lt;br&gt;
 &lt;em&gt;npx create-react-app .&lt;/em&gt; &lt;br&gt;
This will create the react app inside the current folder. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmcbkudj0bi9bd05iete.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmcbkudj0bi9bd05iete.png" alt="Screen Shot 2021-05-25 at 3.32.05 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's open it in VSCode.&lt;/p&gt;

&lt;p&gt;You should have the basic React app setup.&lt;br&gt;
Lets clean up that App.js file. &lt;br&gt;
Delete everything inside the return except for the App div.&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96qxfy4emltdy5met5jz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96qxfy4emltdy5met5jz.png" alt="Screen Shot 2021-05-25 at 4.30.19 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open your built in terminal (control + `)&lt;/p&gt;

&lt;p&gt;Now were going to add the firebase package.&lt;br&gt;
For this you can use &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm install firebase &lt;/li&gt;
&lt;li&gt;yarn add firebase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point we need firebase set up online so let's head there. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://firebase.google.com/" rel="noopener noreferrer"&gt;Firebase&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After signing into Firebase click &lt;strong&gt;Go to console&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxljptp0qrus2cj1c8rju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxljptp0qrus2cj1c8rju.png" alt="Screen Shot 2021-05-25 at 3.36.30 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the consoles main page you're going to want to add a new project &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9kq5ad23c4jqukohkos.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9kq5ad23c4jqukohkos.png" alt="Screen Shot 2021-05-25 at 3.36.41 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give your project a name&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuzcjv6xe4dmysdn6betk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuzcjv6xe4dmysdn6betk.png" alt="Screen Shot 2021-05-25 at 4.40.23 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;** Turn off analytics (this is optional)** &lt;/p&gt;

&lt;p&gt;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 &amp;lt;/&amp;gt; symbol.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3hw4ik1qr6210pc1qtz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3hw4ik1qr6210pc1qtz.png" alt="Screen Shot 2021-05-25 at 3.40.01 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Give your App a nickname and click Register App&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5yowlj5sua6u38qupaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx5yowlj5sua6u38qupaw.png" alt="Screen Shot 2021-05-25 at 4.46.49 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the App is registered it will give you multiple scripts , we only need what's inside the second script.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosyzn6fa4ak8svodzfhr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fosyzn6fa4ak8svodzfhr.png" alt="Screen Shot 2021-05-25 at 3.40.48 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy what yours gives you as every project is different and you'll need your specific keys for this to work.&lt;/p&gt;

&lt;p&gt;Last step here is to enable signing in with google for your app. Go back to the projects main page and select Authentication&amp;gt; Sign-in Method and then edit the option for google.&lt;br&gt;
 &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fer1ovyisyg9j7xfrp6fw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fer1ovyisyg9j7xfrp6fw.png" alt="Screen Shot 2021-05-25 at 3.42.20 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select Enable and enter your email address then hit Save.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ocsl3m0o5rneds3gtqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ocsl3m0o5rneds3gtqk.png" alt="Screen Shot 2021-05-25 at 3.42.40 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Congrats we're done in the firebase console!
&lt;/h2&gt;

&lt;p&gt;Head back on over to your VSCode and react app.&lt;/p&gt;

&lt;p&gt;Create a new file under the src folder named firebase.js&lt;/p&gt;

&lt;p&gt;Inside firebase.js&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;on Line 1 import firebase from 'firebase'&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;on Line 3 paste your firebase configuration you just copied. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp0lh03rf7sijv378trwd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp0lh03rf7sijv378trwd.png" alt="Screen Shot 2021-05-25 at 4.53.40 PM"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now Were going to set up a few variables inside firebase.js&lt;/p&gt;

&lt;p&gt;first initialize firebase&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;const firebaseApp=firebase.initializeApp(firebaseConfig)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Set database,auth, and provider&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;const db = firebaseApp.firestore()&lt;/li&gt;
&lt;li&gt;const auth = firebase.auth()&lt;/li&gt;
&lt;li&gt;const provider = new firebase.auth.GoogleAuthProvider()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that were done setting up variables, time to export!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;export { auth, provider }&lt;/li&gt;
&lt;li&gt;export default db&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;firebase.js is now complete, don't forget to save!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4ipi58ojkt5lvbxj12p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4ipi58ojkt5lvbxj12p.png" alt="Screen Shot 2021-05-25 at 3.52.07 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Back to App.js&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets start in App.js with the imports were going to need&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;import { useState } from 'react'&lt;/li&gt;
&lt;li&gt;import { auth, provider } from './firebase'&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Under Function App(){ and above return( is where we're going to be working&lt;/p&gt;

&lt;p&gt;First things first let's initialize state with useState().&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;const [user,setUser] = useState()
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fogpopgste9idek4qxi5l.png" alt="Screen Shot 2021-05-25 at 4.08.16 PM"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then we'll create our sign in function. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  const signIn = () =&amp;gt; {
auth
  .signInWithPopup(provider)
  .then((payload) =&amp;gt; {
    setUser(payload.user.displayName)
  })
  .catch((err) =&amp;gt; console.log(err))
}
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2snm0ze324l6tnshihmz.png" alt="Screen Shot 2021-05-25 at 4.10.02 PM"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Here you can chain a .then((payload) =&amp;gt;{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&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now for our sign out function. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; const signOut = () =&amp;gt; {
auth.signOut().then(setUser(null))
}
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgvinaitj33k8t5w5s3qk.png" alt="Screen Shot 2021-05-25 at 4.10.15 PM"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it for our functions, we're on the home stretch!!&lt;/p&gt;

&lt;p&gt;Inside of our &lt;/p&gt; we're going to make two buttons. You guessed in, one for sign in, and one for sign out.

&lt;ul&gt;
&lt;li&gt;&amp;lt; button onClick={signIn}&amp;gt;Sign in&lt;/li&gt;
&lt;li&gt;&amp;lt; button onClick={signOut}&amp;gt;Sign out
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2b126jnhyg8uap6mwg57.png" alt="Screen Shot 2021-05-25 at 4.10.32 PM"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;{user ? &amp;lt; div&amp;gt; {user}&amp;lt; /div&amp;gt; : &amp;lt; div&amp;gt; No user&amp;lt; /div&amp;gt;}
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5vmykl89n06dk73af7b.png" alt="Screen Shot 2021-05-25 at 4.10.40 PM"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And with that we're done!&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrj3pl08k3u9ct6wf8qb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flrj3pl08k3u9ct6wf8qb.png" alt="Screen Shot 2021-05-25 at 4.11.09 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save your files and npm start/yarn start in your terminal and you should see &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wtus2d7s7keodj350ja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1wtus2d7s7keodj350ja.png" alt="Screen Shot 2021-05-25 at 4.11.23 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click the sign in button and your App component will update and show the user's name.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx87g5urz0r2hzkv0oy8s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx87g5urz0r2hzkv0oy8s.png" alt="Screen Shot 2021-05-25 at 4.11.29 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Congratulations you did it!!
&lt;/h1&gt;

</description>
      <category>firebase</category>
      <category>react</category>
      <category>authentication</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
