vue-authenticate is easily configurable solution for Vue.js that provides local login/registration as well as Social login using Github, Facebook, Google and other OAuth providers.

Overview

[WARNING]: README file is currently in process of rewrite and will be released soon.

vue-authenticate

Join the chat at https://gitter.im/vuejs-auth/vue-authenticate

vue-authenticate is easily configurable solution for Vue.js that provides local login/registration as well as Social login using Github, Facebook, Google and other OAuth providers.

The best part about this library is that it is not strictly coupled to one request handling library like vue-axios. You will be able to use it with different libraries.

For now it is tested to work with vue-resource and axios (using vue-axios wrapper).

WARNING: From version 1.3.0 default request library is axios using vue-axios wrapper plugin.

This library was inspired by well known authentication library for Angular called Satellizer developed by Sahat Yalkabov. They share almost identical configuration and API so you can easily switch from Angular to Vue.js project.

Supported OAuth providers and configurations

  1. Facebook (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L21)
  2. Google (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L34)
  3. Github (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L49)
  4. Instagram (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L61)
  5. Twitter (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L72)
  6. Bitbucket (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L81)
  7. LinkedIn (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L93)
  8. Microsoft Live (https://github.com/dgrubelic/vue-authenticate/blob/master/src/options.js#L106)

Installation

npm install vue-authenticate

Usage

import Vue from 'vue'
import VueAxios from 'vue-axios'
import VueAuthenticate from 'vue-authenticate'
import axios from 'axios';

Vue.use(VueAxios, axios)
Vue.use(VueAuthenticate, {
  baseUrl: 'http://localhost:3000', // Your API domain
  
  providers: {
    github: {
      clientId: '',
      redirectUri: 'http://localhost:8080/auth/callback' // Your client app URL
    }
  }
})

Email & password login and registration

new Vue({
  methods: {
    login: function () {
      this.$auth.login({ email, password }).then(function () {
        // Execute application logic after successful login
      })
    },

    register: function () {
      this.$auth.register({ name, email, password }).then(function () {
        // Execute application logic after successful registration
      })
    }
  }
})
Login ">
<button @click="login()">Loginbutton>
<button @click="register()">Registerbutton>

Social account authentication

new Vue({
  methods: {
    authenticate: function (provider) {
      this.$auth.authenticate(provider).then(function () {
        // Execute application logic after successful social authentication
      })
    }
  }
})
auth Github ">
<button @click="authenticate('github')">auth Githubbutton>
<button @click="authenticate('facebook')">auth Facebookbutton>
<button @click="authenticate('google')">auth Googlebutton>
<button @click="authenticate('twitter')">auth Twitterbutton>

Vuex authentication

Import and initialize all required libraries

// ES6 example
import Vue from 'vue'
import Vuex from 'vuex'
import VueAxios from 'vue-axios'
import { VueAuthenticate } from 'vue-authenticate'
import axios from 'axios';

Vue.use(Vuex)
Vue.use(VueAxios, axios)

const vueAuth = new VueAuthenticate(Vue.prototype.$http, {
  baseUrl: 'http://localhost:4000'
})
// CommonJS example
var Vue = require('vue')
var Vuex = require('vuex')
var VueAxios = require('vue-axios')
var VueAuthenticate = require('vue-authenticate')
var axios = require('axios');

Vue.use(Vuex)
Vue.use(VueAxios, axios)

// ES5, CommonJS example
var vueAuth = VueAuthenticate.factory(Vue.prototype.$http, {
  baseUrl: 'http://localhost:4000'
})

Once you have created VueAuthenticate instance, you can use it in Vuex store like this:

export default new Vuex.Store({
  
  // You can use it as state property
  state{
    isAuthenticated: false
  },

  // You can use it as a state getter function (probably the best solution)
  getters: {
    isAuthenticated () {
      return vueAuth.isAuthenticated()
    }
  },

  // Mutation for when you use it as state property
  mutations: {
    isAuthenticated (state, payload) {
      state.isAuthenticated = payload.isAuthenticated
    }
  },

  actions: {

    // Perform VueAuthenticate login using Vuex actions
    login (context, payload) {

      vueAuth.login(payload.user, payload.requestOptions).then((response) => {
        context.commit('isAuthenticated', {
          isAuthenticated: vueAuth.isAuthenticated()
        })
      })

    }
  }
})

Later in Vue component, you can dispatch Vuex state action like this

// You define your store logic here
import store from './store.js'

new Vue({
  store,

  computed: {
    isAuthenticated: function () {
      return this.$store.getters.isAuthenticated()
    }
  },

  methods: {
    login () {
      this.$store.dispatch('login', { user, requestOptions })
    }
  }
})

Custom request and response interceptors

You can easily setup custom request and response interceptors if you use different request handling library.

Important: You must set both request and response interceptors at all times.

/**
* This is example for request and response interceptors for axios library
*/

Vue.use(VueAuthenticate, {
  bindRequestInterceptor: function () {
    this.$http.interceptors.request.use((config) => {
      if (this.isAuthenticated()) {
        config.headers['Authorization'] = [
          this.options.tokenType, this.getToken()
        ].join(' ')
      } else {
        delete config.headers['Authorization']
      }
      return config
    })
  },

  bindResponseInterceptor: function () {
    this.$http.interceptors.response.use((response) => {
      this.setToken(response)
      return response
    })
  }
})

License

The MIT License (MIT)

Copyright (c) 2017 Davor Grubelić

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

You might also like...
A Vue.js Vuex module for authentication using Amazon AWS Cognito

vue-auth-cognito This small library serves as a wrapper of Amazon Cognito for Vuex. As pointed out in #65 the aws-amplify/amplify-js project officiall

A sample application with authentication setup using firebase.

vue-auth (currently, firebase) A sample application with authentication setup using firebase. Project setup Create a firebase account Enable Google SS

A example application using nuxt-auth.
A example application using nuxt-auth.

Nuxt Auth Example This is an example project using nuxt-auth, the Nuxt module providing authentication and sessions via NextAuth.js. nuxt-auth wraps N

Modular security for Vue, Vuex, Vue-Router and Nuxt

vue-kindergarten Introduction vue-kindergarten is a plugin for VueJS 2.0 that integrates kindergarten into your VueJS applications. It helps you to au

 Vue 3 Authentication with JWT, Vuex and Vue Router
Vue 3 Authentication with JWT, Vuex and Vue Router

Vue 3 Authentication and Authorization with JWT, Vuex, Axios, Vue Router, VeeValidate

Vue 3 JWT Refresh Token with Axios Interceptors, Vuex and Vue Router example
Vue 3 JWT Refresh Token with Axios Interceptors, Vuex and Vue Router example

Vue 3 JWT Refresh Token with Axios example For instruction, please visit: Vue 3 JWT Refresh Token with Axios example Vue 3 Authentication & Authorizat

Sing App Vue Dashboard - free and open source admin dashboard template
Sing App Vue Dashboard - free and open source admin dashboard template

Sing App Vue Dashboard - free and open source admin dashboard template Including responsive layout and high quality UI built with Bootstrap 4.5. View

An Axios adapter for Vue Admin authentication and authorization

Auth Axios Adapter for Vue Admin Introduction This package is one of the adapters that can be used with Vue Admin. It provides very simple and minimal

Simple OAuth2 library for Vue.js and Electron clients.

XAuth2 - Electron OAuth2 Client A Fork of vue-authenticate that (hopefully) works in Electron. XAuth2 is easily configurable solution for Vue.js that

Owner
Altafino Ltd
Software Engineering & Consulting (Go,JS,Vue,React,SQL,NoSQL)
Altafino Ltd
vue-google-api - A wrapper for easy integration of the Google's APIs client in any vue.js 2 project, based on promises, with built-in Oauth2 "Sign In with Google" facilities.

vue-google-api This project is outdated and no longer maintained, I can't find time to do what should be done here. Really sorry, if someone wants to

Stéphane Souron 37 Nov 3, 2022
DEPRECATED - Vue wrapper to facebook account kit: https://www.accountkit.com/

DEPRECATED AccountKit Facebook Account Kit is being deprecated: read more https://developers.facebook.com/docs/accountkit/ A Vue.js Plugin Installatio

Alessandro Biessek 13 Sep 11, 2020
A simple plugin to include a custom Facebook sign-in button into your web app.

vue-facebook-signin-button A simple Vue plugin to include a custom Facebook sign-in button into your web app. Install This plugin can be installed as

Phan An 135 May 25, 2022
Mindsaver 14 Oct 17, 2022
An Identity Manager that uses Handshake Login and works with all supported strategies.

hTools Identity Manager An Identity Manager that uses handshake-login and works with all supported strategies. Features Supports all strategies: Local

Handshake Tools 9 Jun 7, 2022
Vue plugin for easily managing your app's auth state

vue-auth Vue plugin for easily managing your app's auth state This is a work in progress and the API may change without

Wade Urry 15 Feb 7, 2018
Handling Google sign-in and sign-out

vue-google-authenticator Handling Google sign-in and sign-out for Vue.js applications clone of simmatrix/vue-google-auth. Installation npm install vue

Gilson Fernandes Batista de Souza 0 Jul 9, 2018
🔐 A simple Vue plugin to include a Google sign-in button into your web app.

vue-google-signin-button A simple Vue plugin to include a Google sign-in button into your web app. Install This plugin can be installed as a module $

Phan An 288 Oct 17, 2022
A simple authentication and authorization library for Vue.js using typescript

?? Vue Auth Plugin A simple authentication and authorization library for Vue.js using typescript Inspired in @websanova/vue-auth Pull requests and iss

Bekee Franklin 0 Jul 7, 2019
Vue plugin for using Microsoft Authentication Library (MSAL)

vue-msal Wrapper of MSAL.js (Microsoft Authentication Library) for usage in Vue. The vue-msal library enables client-side vue applications, running in

null 112 Dec 17, 2022