DEV Community

Casey πŸ’Ž
Casey πŸ’Ž

Posted on

16

Vue + Firebase + Google == Easy Authentication

Hey, DEV community!

I sometimes create youtube videos about things I'm working on, detailing how I did it - instead of a video, I'd like to share how you can use Firebase Auth to authenticate your users with Google!

Getting started, I'll assume that you probably already have a firebase project up and running, the only thing we need to do on the firebase console is active Google sign in:

Once that's done, we can get started on adding functionality to our view.

Here's a base template consisting of a button and the empty method googleSignIn

// @/views/SignUp.vue

<template>
  <div>
    <button @click="googleSignIn">
      Sign In with Google
    </button>
  </div>
</template>

<script>
import firebase from "firebase";
export default {
  name: "SignUp",
  methods: {
    googleSignIn: function() {
      // We'll create functionality here
    }
  },
};
</script>

Enter fullscreen mode Exit fullscreen mode

Now that we have the basic structure in place, we can figure out what we need.

First, we need to create an instance of our preferred provider:
let provider = new firebase.auth.GoogleAuthProvider();

Second, we decide what method we want to use - in our case we're going to use signInWithPopup()

Lastly, let's implement this into working code:

let provider = new firebase.auth.GoogleAuthProvider();
firebase
        .auth()
        .signInWithPopup(provider)
        .then((result) => {
          let token = result.credential.accessToken;
          let user = result.user;
            console.log(token) // Token
            console.log(user) // User that was authenticated
        })
        .catch((err) => {
          console.log(err); // This will give you all the information needed to further debug any errors
        });
Enter fullscreen mode Exit fullscreen mode

That's it! You can now authenticate users with Google in your firebase project - yay!

Note, if you don't know how to sign someone out, it's pretty easy:

firebase
        .auth()
        .signOut()
        .then(() => {
          alert("Successfully signed out.");
        });
Enter fullscreen mode Exit fullscreen mode

Let me know if you have any questions about the process and happy coding 😊

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (3)

Collapse
 
markokoh profile image
Mark Okoh β€’

Very nice! Super simple.

Collapse
 
itscasey profile image
Casey πŸ’Ž β€’

Yep! It’s a lot easier than it seems

Collapse
 
tarunjain3 profile image
tarunjain3 β€’

also need to import this files

import firebase from "firebase/compat/app"
import "firebase/compat/auth"
import "firebase/compat/firestore"

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay