Handling SignUp or SignIn with Google and Facebook using VueJS

Handling SignUp or SignIn with Google and Facebook using VueJS

signup_with_facebook_google_vue Handling SignUp or SignIn with Google and Facebook using Pure VueJS applications without any external package.

signup_with_facebook_google_vue

Handling SignUp or SignIn with Google and Facebook using Pure VueJS applications without any external package.

Project setup

npm install

Google Setup Client ID

import GoogleAuth from '@/config/google_oAuth.js'
const gauthOption = {
clientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com',
    scope: 'profile email',
    prompt: 'select_account'
}
Vue.use(GoogleAuth, gauthOption)

Options

Property Type Required Description
clientId String Required. The app's client ID, found and created in the Google Developers Console.
scope String Optional. Default value is profile email. Full list of scopes.
prompt String Optional. This value using for authCode. The possible values are select_account or consent. Default value is select_account. To get refresh token from auth code, use consent.
fetch_basic_profile Boolean Optional. If set to true, email profile openid will be automatically added as scope. Default value is true.

Methods

Property Description Type
GoogleAuth return of gapi.auth2.getAuthInstance() Object
isAuthorized Whether or not you have auth Boolean
isInit Whether or not api init Boolean
isLoaded Whether or not api init. will be deprecated. Function
signIn function for sign-in Function
getAuthCode function for getting authCode Function
signOut function for sign-out Function

Usage

We already initalized GoogleAuth and directly we can add click event for the login button as below,

loginWithGoogle () {
  this.$gAuth
  .signIn()
  .then(GoogleUser => {
    // on success do something
    console.log('GoogleUser', GoogleUser)
    console.log('getId', GoogleUser.getId())
    console.log('getBasicProfile', GoogleUser.getBasicProfile())
    console.log('getAuthResponse', GoogleUser.getAuthResponse())
  })
  .catch(error => {
    console.log('error', error)
  })
}

Facebook App ID Setup

Important: The Facebook SDK must first be loaded asynchronously for the plugin to work. Something like this will do:

export const initFbsdk = () => {
    return new Promise(resolve => {
        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'xxxxxxxxxxxxxxxxxxx',
                cookie     : true,  // enable cookies to allow the server to access the session
                xfbml      : true,  // parse social plugins on this page
                version    : 'v2.8' // use graph api version 2.8
            });
        };
        (function(d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) return;
            js = d.createElement(s); js.id = id;
            js.src = "//connect.facebook.net/en_US/sdk.js";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    })
}

If you're not in a modular environment, just include it as a <script>.

<script type="text/javascript" src="https://connect.facebook.net/en_US/sdk.js"></script>

Usage

Step 1: import and use the plugin if you're in a modular environment otherwise plugin will register itself.

import { initFbsdk } from '@/config/facebook_oAuth.js'

Step 2: Initialize the Facebook instance with the app id

mounted () {
   initFbsdk()
}

Step 3: Add the button click event

loginWithFacebook () {
  window.FB.login(response => {
    console.log('fb response', response)
  }, this.params)
}

Compiles and hot-reloads for development

npm run serve

Login Screen

Login

SignUp Screen

SignUp

Compiles and minifies for production

npm run build

Run your tests

npm run test

Lints and fixes files

npm run lint

Run your unit tests

npm run test:unit

Customize configuration

See Configuration Reference.

Download Details:

Author: Jebasuthan

Source Code: https://github.com/Jebasuthan/Vue-Facebook-Google-oAuth

vue vuejs javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

8 Popular Websites That Use The Vue.JS Framework

In this article, we are going to list out the most popular websites using Vue JS as their frontend framework. Vue JS is one of those elite progressive JavaScript frameworks that has huge demand in the web development industry. Many popular websites are developed using Vue in their frontend development because of its imperative features.

Vue Native is a framework to build cross platform native mobile apps using JavaScript

Vue Native is a framework to build cross platform native mobile apps using JavaScript. It is a wrapper around the APIs of React Native. So, with Vue Native, you can do everything that you can do with React Native. With Vue Native, you get

How to Make a Simple Vue Custom Select Component

In this article, you’ll learn how to build a Vue custom select component that can be easily be styled using your own CSS. In fact, it’s the same component that we use in production on Qvault, and you can see it in action on the playground.

Creating a Custom Tooltip Component in Vue

There are plenty of libraries out there that will have you up and running with a good tooltip solution in minutes. However, if you are like me, you are sick and tired of giant dependency trees that have the distinct possibility of breaking at any time.

Vue ShortKey plugin for Vue.js

Vue-ShortKey - The ultimate shortcut plugin to improve the UX .Vue-ShortKey - plugin for VueJS 2.x accepts shortcuts globaly and in a single listener.