0

所以我正在使用 Angular 6 构建一个 Web 应用程序,并使用 SocialLogin 库实现了谷歌登录。这是我的代码:

public socialSignIn(socialPlatform : string) {
      let socialPlatformProvider;
      if(socialPlatform == "facebook"){
        socialPlatformProvider = FacebookLoginProvider.PROVIDER_ID;
      }else if(socialPlatform == "google"){
        socialPlatformProvider = GoogleLoginProvider.PROVIDER_ID;
      }

      this.socialAuthService.signIn(socialPlatformProvider).then(
        (userData) => {

        let user: User = new User();
        let newuserrequest: NewUserRequest = new NewUserRequest();

        user.email = userData.email;
        var username;
        username = userData.name;
        var uuid = this.newGuid();
        newuserrequest.requestId = uuid;
        newuserrequest.user = user;
        newuserrequest.accountId = userData.id;
        newuserrequest.accountIdToken = userData.idToken;
        this.newuserrequest = Array<NewUserRequest>(newuserrequest);
        this.personname2 = userData.name;
        this.personname = this.personname2.toString();
        console.log('Personname: ' + this.personname);
        this.div = document.getElementById("name");
        this.div.textContent = this.personname;
        var token;
        token = userData.idToken;
        this.saveValueUser(username, user.email);

        this.islogedin = true;

        this.saveValue(token);
        this.communicator = new CommunicatorService(this.httpClient);
        var url;
        url = "myurl/newuser";
        this.communicator.getDataFromServer(this.newuserrequest, url)
        .subscribe(
          (data:any) => {
            console.log(data);
          }
        );
        }
      );
    }

这段代码工作正常,但只有 1 小时。使用 JWT-helper-service 我创建了一个方法,让我知道我的令牌是否仍然有效,以及它的有效期有多长。这是方法:

  private tokenHandler(){
    this.helper = new JwtHelperService();
    this.isExpired = this.helper.isTokenExpired(this.token);
    if(this.isExpired != true) {
      this.expirationDate = this.helper.getTokenExpirationDate(this.token);
    }
    console.log("Is expired: " + this.isExpired);
    console.log("Expiration date: " + this.expirationDate);
    if(this.isExpired == true){
      this.signOut();
    }
  }

如何在令牌过期之前刷新令牌?

4

1 回答 1

0

您可以在第一次获取设置倒数计时器的令牌并在到期前的特定时间触发续订之后在代码中实现调度调用。

查看此链接https://auth0.com/docs/quickstart/spa/angular2/05-token-renewal以获取示例

这是特定于 AuthO 库的,但您可以轻松地将其调整为您的令牌检索库。

此外,该示例面向 Angular 2,因此请记住 rxjs 进行了一些更改(例如,现在我们仅使用 of() 等代替 observable.of()

如果您需要更多帮助,请告诉我。

于 2018-08-27T17:11:05.953 回答