Created
July 14, 2020 14:46
-
-
Save magicleon94/415d330fbea545e68ad509dc5b007944 to your computer and use it in GitHub Desktop.
Auth0 manager - js side - for medium article
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
function createAuth0Manager(clientParameters, onAuthenticated, onAuthError) { | |
return new Auth0Manager(clientParameters, onAuthenticated, onAuthError); | |
} | |
class Auth0Manager { | |
constructor(clientParameters, onAuthenticated, onAuthError) { | |
this.clientParameters = clientParameters; | |
this.onAuthenticated = onAuthenticated; | |
this.onAuthError = onAuthError; | |
this.auth0Client = new auth0.WebAuth({ | |
domain: clientParameters['domain'], | |
clientID: clientParameters['clientID'], | |
redirectUri: clientParameters['redirectUri'], | |
responseType: clientParameters['responseType'], | |
scope: clientParameters['scope'], | |
}); | |
} | |
login() { | |
var that = this; | |
this.auth0Client.authorize( | |
{ redirectUri: this.clientParameters['redirectUri'] }, | |
function (err, res) { | |
that.handleAuthenticationResult(that, err, res); | |
} | |
); | |
} | |
handleAuthenticationResult(that, authError, _) { | |
if (authError) { | |
that.onAuthError(authError); | |
} else { | |
that.auth0Client.parseHash( | |
(err, result) => { | |
if (result && result.accessToken && result.idToken) { | |
that.onAuthenticated(result); | |
} else if (err) { | |
that.onAuthError(err) | |
} | |
} | |
); | |
} | |
} | |
logout(callback) { | |
this.auth0Client.logout({ | |
returnTo: window.location.origin, | |
clientID: this.clientParameters['clientID'], | |
}); | |
callback(); | |
} | |
getUserInfo(accessToken, onUserInfo) { | |
this.auth0Client.client.userInfo(accessToken, function (err, info) { | |
onUserInfo(err, info); | |
}); | |
} | |
refreshLogin() { | |
var that = this; | |
this.auth0.checkSession({}, | |
(err, authResult) => { | |
if (authResult && authResult.accessToken && authResult.idToken) { | |
that.onAuthenticated(authResult); | |
} else if (err) { | |
that.onAuthError(err); | |
} | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment