Guest User

Auth0.js - my custom flavour

a guest
May 2nd, 2019
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export default class Auth {
  2.  
  3.   //parameters of Auth-object
  4.   accessToken;
  5.   idToken;
  6.   expiresAt;
  7.   userProfile;
  8.   auth0 = new auth0.WebAuth({
  9.     domain: AUTH_CONFIG.domain,
  10.     clientID: AUTH_CONFIG.clientId,
  11.     redirectUri: AUTH_CONFIG.callbackUrl,
  12.     responseType: AUTH_CONFIG.responseType,
  13.     scope: AUTH_CONFIG.scope
  14.   });
  15.  
  16. constructor() {
  17.     if (localStorage.getItem("accessToken") && localStorage.getItem("idToken") && localStorage.getItem("expiresAt")) {
  18.       console.log("Auth.js.constructor() IF - Data found in the localStorage");
  19.       this.accessToken = localStorage.getItem("accessToken");
  20.       this.idToken = localStorage.getItem("idToken");
  21.       this.expiresAt = localStorage.getItem("expiresAt");
  22.     }
  23.     else {
  24.       console.log("Auth.js.constructor() ELSE - No data found in the localstorage ELSE");
  25.     }
  26.  
  27.     this.login = this.login.bind(this);
  28.     this.logout = this.logout.bind(this);
  29.     this.handleAuthentication = this.handleAuthentication.bind(this);
  30.     this.isAuthenticated = this.isAuthenticated.bind(this);
  31.     this.getAccessToken = this.getAccessToken.bind(this);
  32.     this.getIdToken = this.getIdToken.bind(this);
  33.     this.renewSession = this.renewSession.bind(this);
  34.     this.getProfile = this.getProfile.bind(this);
  35.  
  36.   }
  37.  
  38. setSession(authResult) {
  39.     console.log("Auth.js.setSession() called with : authResult ->", authResult);
  40.  
  41.     // Set the time that the access token will expire at
  42.     let expiresAt = (authResult.expiresIn * 1000) + new Date().getTime();
  43.     this.accessToken = authResult.accessToken;
  44.     this.idToken = authResult.idToken;
  45.     this.expiresAt = expiresAt;
  46.  
  47.  
  48.  
  49.     localStorage.setItem("accessToken", authResult.accessToken);
  50.     localStorage.setItem("idToken", authResult.idToken);
  51.     localStorage.setItem("expiresAt", (new Date().getTime() / 1000) + authResult.expiresIn);
  52.     localStorage.setItem("isLoggedIn", true);
  53.  
  54.  
  55.     // navigate to the home route
  56.     history.replace('/dashboard');
  57.   }
  58.  
  59. }
Add Comment
Please, Sign In to add comment