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 😊

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

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"

nextjs tutorial video

Youtube Tutorial Series πŸ“Ί

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series πŸ‘€

Watch the Youtube series