简体   繁体   English

auth0与angular2-如何获取令牌

[英]auth0 with angular2 - how to get token

I'm using auth0 and downloaded the quickstart sample app from here . 我正在使用auth0,并从此处下载了quickstart示例应用程序。 I'm trying to get the id_token so I can get the user's profile. 我正在尝试获取id_token,以便获取用户的个人资料。 However, I'm not able to, as the auth.service is running after my other pages load. 但是,我无法这样做,因为auth.service在其他页面加载后正在运行。

So, I have a home.component like this: 所以,我有一个像这样的home.component:

import { Component, OnInit } from '@angular/core';
import {Auth} from './auth.service';

@Component({
  selector: 'home',
  template: `
    <h1>Testing</h1>
    `,
  providers: [Auth],
})

export class HomeComponent implements OnInit {

 constructor(private auth: Auth) {}

 ngOnInit(){
     var token = localStorage.getItem('id_token');
     var profile = this.auth.lock.getProfile(token, function(error, profile) {
     if (error) {
       console.log("there was an ERROR" + error + token);
       return;
     }});    
   }
}

and my auth.service class that looks like this: 和我的auth.service类看起来像这样:

declare var Auth0Lock: any;

@Injectable()
export class Auth {
// Configure Auth0
lock = new Auth0Lock(mycreds, mycreds, {});

 constructor() {
    // Add callback for lock `authenticated` event
    this.lock.on("authenticated", (authResult) => {
    console.log("IN AUTH.SERVICE");      
    localStorage.setItem('id_token', authResult.idToken);                   
  });          
}

public login() {
  // Call the show method to display the widget.
  this.lock.show();        
};

public authenticated() {
// Check if there's an unexpired JWT
// This searches for an item in localStorage with key == 'id_token'

return tokenNotExpired();
};

  public logout() {
    // Remove token from localStorage
    localStorage.removeItem('id_token');
  };
}

the issue is that my home.component class loads before my auth.service, so when I try to get the user token, it's not there yet. 问题是我的home.component类在我的auth.service之前加载,因此当我尝试获取用户令牌时,它还不存在。 I'm pretty new to angular so I'm not sure how to make one page load before another or how to solve this problem. 我对angular还是很陌生,所以我不确定如何在加载页面之前先加载页面或如何解决此问题。

The service is instantiated and the constructor run way before your HomeComponents OnInit event is raised. 在引发HomeComponents OnInit事件之前,将实例化服务并构造方法运行。 The thing is, your service is waiting for a response (auth) before it sets the token. 问题是,您的服务在设置令牌之前正在等待响应(auth)。

That said - you can't expect there to be a token in your init on the HomeComponent. 就是说-您不能期望HomeComponent的init中有令牌。 You're not getting the token untill the user actively logged in through the Auth0Lock component. 在用户通过Auth0Lock组件主动登录之前,您不会获得令牌。 All you can do in your HomeComponent is call auth.login(). 您在HomeComponent中可以做的就是调用auth.login()。 Since your user on the callback might have been authenticated, the token might be there - you could check in your HomeComponent if the token is there (auth.authenticated()) before you request the profile data. 由于您的回调中的用户可能已通过身份验证,因此令牌可能在那里-您可以在请求配置文件数据之前在HomeComponent中检查令牌是否在其中(auth.authenticated())。

A couple questions for you: 1. Did you check your console if you're getting errors? 给您几个问题:1.您是否在检查控制台时遇到错误? 2. Are you ever getting the "IN AUTH.SERVICE" message from your auth callback? 2.您是否从授权回调中收到“ IN AUTH.SERVICE”消息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM