Почему мой токен auth0 истекает при обновлении страницы или переходе по ссылке в моем приложении Angular? - PullRequest
1 голос
/ 24 марта 2019

Я настраиваю аутентификацию в моем Angular SPA.

Я использую auth0, и я просматривал учебное пособие на их странице: https://auth0.com/docs/quickstart/spa/angular2

Я сделал учебное пособие по входу.

import { Injectable } from '@angular/core';
import * as auth0 from 'auth0-js';
import { Router } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  userProfile: any;

  private _idToken: string;
  private _accessToken: string;
  private _expiresAt: number;

    auth0 = new auth0.WebAuth({
    clientID: '*****',
    domain: '****',
    responseType: 'token id_token',
    redirectUri: 'http://localhost:4200/callback',
    scope: 'openid profile'
  });

  constructor(public router: Router) {
    this._idToken = '';
    this._accessToken = '';
    this._expiresAt = 0;
  }

  public login(): void {
    this.auth0.authorize();
  }

  public handleAuthentication(): void {
    this.auth0.parseHash((err, authResult) => {
      if (authResult && authResult.accessToken && authResult.idToken) {
        window.location.hash = '';
        this.localLogin(authResult);
        this.router.navigate(['/']);
      } else if (err) {
        this.router.navigate(['/']);
        console.log(err);
      }
    });
  }

  public getProfile(cb): void {
    if (!this._accessToken) {
      throw new Error('Access Token must exist to fetch profile');
    }

    const self = this;
    this.auth0.client.userInfo(this._accessToken, (err, profile) => {
      if (profile) {
        self.userProfile = profile;
      }
      cb(err, profile);
    });
  }

  private localLogin(authResult): void {

    localStorage.setItem('isLoggedIn', 'true');

    const expiresAt = (authResult.expiresIn * 1000) + new Date().getTime();
    this._accessToken = authResult.accessToken;
    this._idToken = authResult.idToken;
    this._expiresAt = expiresAt;
  }

  public renewTokens(): void {
    this.auth0.checkSession({}, (err, authResult) => {
      if (authResult && authResult.accessToken && authResult.idToken) {
        this.localLogin(authResult);
      } else if (err) {
        alert(`Could not get a new token (${err.error}: ${err.error_description}).`);
        this.logout();
      }
    });
  }

  public logout(): void {
    this._accessToken = '';
    this._idToken = '';
    this._expiresAt = 0;

    localStorage.removeItem('isLoggedIn');
    this.router.navigate(['/']);
  }

  public isAuthenticated(): boolean {
    return new Date().getTime() < this._expiresAt;
  }

  get accessToken(): string {
    return this._accessToken;
  }

  get idToken(): string {
    return this._idToken;
  }


}

Я нажимаю войти в свой аккаунтстраница, я успешен, я получил токен и все в порядке.Но когда я обновляю страницу или нажимаю на ссылку, происходит выход из системы.Что мне делать, чтобы оставаться на странице?

1 Ответ

3 голосов
/ 24 марта 2019

В настоящее время вы сохраняете данные в переменной в памяти.Итак, при перезагрузке приложения переменная в памяти ( _expiresAt ) потеряла значение.Поэтому метод isAuthenticated () возвращает false .Вы можете сохранить данные в браузере localStorage .

 private localLogin(authResult): void {
      const 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);
  }

Затем в методе isAuthenticated () вы можете загрузить expiresAt из localStorage.

  public isAuthenticated(): boolean {
    const expiresAt = JSON.parse(localStorage.getItem("expires_at") || "{}");
    return new Date().getTime() < expiresAt;
  }

Во время выхода из системы вам необходимо очистить данные из localStorage.

public logout(): void {
    // Remove tokens and expiry time from localStorage
    localStorage.removeItem("access_token");
    localStorage.removeItem("id_token");
    localStorage.removeItem("expires_at");
    // Remove server SSO session
    this.auth0.logout({ returnTo: "http://localhost:4200" });

  }
...