Skip to content

Instantly share code, notes, and snippets.

@magicleon94
Created July 14, 2020 14:46
Show Gist options
  • Save magicleon94/415d330fbea545e68ad509dc5b007944 to your computer and use it in GitHub Desktop.
Save magicleon94/415d330fbea545e68ad509dc5b007944 to your computer and use it in GitHub Desktop.
Auth0 manager - js side - for medium article
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