Created
October 5, 2017 12:35
-
-
Save mxmzb/103e10d199804da717c7b5de1e451d7b to your computer and use it in GitHub Desktop.
Auth0.com authentication
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 Auth0Lock from 'auth0-lock'; | |
// import auth0 from 'auth0-js'; | |
import decode from 'jwt-decode'; | |
// import { browserHistory } from 'react-router'; | |
import history from './history'; | |
export default class Auth { | |
lock = new Auth0Lock('GNx0zoCnEmuSz5T3JylON5hxEhMSe9Ki', 'artmarqet.auth0.com', { | |
oidcConformant: true, | |
autoclose: true, | |
allowedConnections: ['Username-Password-Authentication', 'facebook', 'linkedin'], | |
additionalSignUpFields: [{ | |
name: 'firstName', | |
placeholder: 'Your first name', | |
prefill: 'Barbara' | |
}, | |
{ | |
name: 'lastName', | |
placeholder: 'Your last name', | |
prefill: 'Wright' | |
}], | |
auth: { | |
redirectUrl: 'http://localhost:3000/callback', | |
responseType: 'token id_token', | |
audience: `https://artmarqet.auth0.com/userinfo`, | |
params: { | |
scope: 'email openid' | |
} | |
} | |
}); | |
constructor() { | |
this.handleAuthentication(); | |
// binds functions to keep this context | |
this.login = this.login.bind(this); | |
this.logout = this.logout.bind(this); | |
this.isAuthenticated = this.isAuthenticated.bind(this); | |
} | |
login() { | |
// Call the show method to display the widget. | |
this.lock.show(); | |
} | |
handleAuthentication() { | |
// Add a callback for Lock's `authenticated` event | |
this.lock.on('authenticated', this.setSession.bind(this)); | |
// Add a callback for Lock's `authorization_error` event | |
this.lock.on('authorization_error', (err) => { | |
console.log(err); | |
alert(`Error: ${err.error}. Check the console for further details.`); | |
history.replace('/home'); | |
}); | |
} | |
setSession(authResult) { | |
if (authResult && authResult.accessToken && authResult.idToken) { | |
// Set the time that the access token will expire at | |
let expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime()); | |
localStorage.setItem('access_token', authResult.accessToken); | |
localStorage.setItem('id_token', authResult.idToken); | |
localStorage.setItem('expires_at', expiresAt); | |
// navigate to the home route | |
history.replace('/home'); | |
} | |
} | |
logout() { | |
// Clear access token and ID token from local storage | |
localStorage.removeItem('access_token'); | |
localStorage.removeItem('id_token'); | |
localStorage.removeItem('expires_at'); | |
// navigate to the home route | |
history.replace('/home'); | |
} | |
isAuthenticated() { | |
// Check whether the current time is past the | |
// access token's expiry time | |
let expiresAt = JSON.parse(localStorage.getItem('expires_at')); | |
return new Date().getTime() < expiresAt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment