Created
May 26, 2018 19:36
-
-
Save bfillmer/d7358b56c04a4f5a84b82116f6305b44 to your computer and use it in GitHub Desktop.
This file contains 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
// Basic auth0 lock functionality written functionally rather than class based. | |
import auth0 from 'auth0-js' | |
const config = { | |
domain: '', | |
clientID: '', | |
redirectUri: '', | |
audience: '', | |
responseType: '', | |
scope: '' | |
} | |
const auth = new auth0.WebAuth(config) | |
export function login () { | |
return auth.authorize() | |
} | |
function setAuthentication (result) { | |
const expiresAt = JSON.stringify((result.expiresIn * 1000) + new Date().getTime()) | |
window.localStorage.setItem('access_token', result.accessToken) | |
window.localStorage.setItem('id_token', result.idToken) | |
window.localStorage.setItem('expires_at', expiresAt) | |
} | |
export function parseCallback () { | |
return new Promise((resolve, reject) => { | |
auth.parseHash((error, authResult) => { | |
if (authResult && authResult.accessToken && authResult.idToken) { | |
setAuthentication(authResult) | |
resolve(authResult) | |
} else { | |
reject(error) | |
} | |
}) | |
}) | |
} | |
export function logout () { | |
// Clear Access Token and ID Token from local storage | |
window.localStorage.removeItem('access_token') | |
window.localStorage.removeItem('id_token') | |
window.localStorage.removeItem('expires_at') | |
} | |
export function isAuthenticated () { | |
const expires = JSON.parse(window.localStorage.getItem('expires_at')) | |
return (new Date().getTime() < expires) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment