Created
October 18, 2018 04:28
-
-
Save lukebyrne/accc8ed29765a5350e3f88be595d0b56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import decode from 'jwt-decode'; | |
import axios from 'axios'; | |
import auth0 from 'auth0-js'; | |
import Router from 'vue-router'; | |
import Auth0Lock from 'auth0-lock'; | |
const ID_TOKEN_KEY = 'id_token'; | |
const ACCESS_TOKEN_KEY = 'access_token'; | |
const CLIENT_ID = '{AUTH0_CLIENT_ID}'; | |
const CLIENT_DOMAIN = '{AUTH0_DOMAIN}'; | |
const REDIRECT = 'YOUR_CALLBACK_URL'; | |
const SCOPE = '{SCOPE}'; | |
const AUDIENCE = 'AUDIENCE_ATTRIBUTE'; | |
var auth = new auth0.WebAuth({ | |
clientID: CLIENT_ID, | |
domain: CLIENT_DOMAIN | |
}); | |
export function login() { | |
auth.authorize({ | |
responseType: 'token id_token', | |
redirectUri: REDIRECT, | |
audience: AUDIENCE, | |
scope: SCOPE | |
}); | |
} | |
var router = new Router({ | |
mode: 'history', | |
}); | |
export function logout() { | |
clearIdToken(); | |
clearAccessToken(); | |
router.go('/'); | |
} | |
export function requireAuth(to, from, next) { | |
if (!isLoggedIn()) { | |
next({ | |
path: '/', | |
query: { redirect: to.fullPath } | |
}); | |
} else { | |
next(); | |
} | |
} | |
export function getIdToken() { | |
return localStorage.getItem(ID_TOKEN_KEY); | |
} | |
export function getAccessToken() { | |
return localStorage.getItem(ACCESS_TOKEN_KEY); | |
} | |
function clearIdToken() { | |
localStorage.removeItem(ID_TOKEN_KEY); | |
} | |
function clearAccessToken() { | |
localStorage.removeItem(ACCESS_TOKEN_KEY); | |
} | |
// Helper function that will allow us to extract the access_token and id_token | |
function getParameterByName(name) { | |
let match = RegExp('[#&]' + name + '=([^&]*)').exec(window.location.hash); | |
return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); | |
} | |
// Get and store access_token in local storage | |
export function setAccessToken() { | |
let accessToken = getParameterByName('access_token'); | |
localStorage.setItem(ACCESS_TOKEN_KEY, accessToken); | |
} | |
// Get and store id_token in local storage | |
export function setIdToken() { | |
let idToken = getParameterByName('id_token'); | |
localStorage.setItem(ID_TOKEN_KEY, idToken); | |
} | |
export function isLoggedIn() { | |
const idToken = getIdToken(); | |
return !!idToken && !isTokenExpired(idToken); | |
} | |
function getTokenExpirationDate(encodedToken) { | |
const token = decode(encodedToken); | |
if (!token.exp) { return null; } | |
const date = new Date(0); | |
date.setUTCSeconds(token.exp); | |
return date; | |
} | |
function isTokenExpired(token) { | |
const expirationDate = getTokenExpirationDate(token); | |
return expirationDate < new Date(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment