2017-02-01 9 views
-1

アングル2を学習しています。自分のユーザーにホームページが表示され、Auth0を使用してアプリケーションにログインするシナリオを作成しようとしていますユーザーは認証サービス内からダッシュボードページにリダイレクトされます。私はAuth0を使用していますアングル2アプリでサービスからリダイレクトを実行したい

import { Injectable }  from '@angular/core'; 
import { tokenNotExpired } from 'angular2-jwt'; 
import { myConfig }  from './auth.config'; 
import {Router} from "@angular/router"; 

// Avoid name not found warnings 
declare var Auth0Lock: any; 

@Injectable() 
export class Auth { 
    // Configure Auth0 
    lock = new Auth0Lock(myConfig.clientID, myConfig.domain, {}); 

    constructor(private router:Router) { 
    // Add callback for lock `authenticated` event 
    this.lock.on('authenticated', (authResult) => 
    { 
     this.lock.getProfile(authResult.idToken,function (error: any, profile: any) 
     { 
      if(error) 
      { 
      throw new Error; 
      } 
      localStorage.setItem('id_token', authResult.idToken); 
      localStorage.setItem('profile', JSON.stringify(profile)); 
      this.router.navigate(['/dashboard']); 
     }); 


    }); 
    } 

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

    public authenticated() { 
    // Check if there's an unexpired JWT 
    // It searches for an item in localStorage with key == 'id_token' 
    return tokenNotExpired(); 
    }; 

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

    this.router.navigate(['/login']); 
    }; 
} 

は、認証サービスのためのテンプレートを提供して、それから私は、ログイン機能でrouter.navigate機能を追加しましたが、私のユーザーがまだログインページが表示されます。

答えて

3

TypeScriptクラス内でfunctionを使用しないでください... thisコンテキストが混乱します。矢印機能の表記() => {}のみを使用してください。

this.lock.getProfile(authResult.idToken, (error: any, profile: any) => { //here 
     if(error) 
     { 
     throw new Error; 
     } 
     localStorage.setItem('id_token', authResult.idToken); 
     localStorage.setItem('profile', JSON.stringify(profile)); 
     this.router.navigate(['/dashboard']); 
}); 
+0

働いたことが、今では私は、サイドバーのようなページに持っている他の要素を表示せずに私に私のダッシュボードのルータだけ、出口部分を示しています。これは、クラスのそれにthisコンテキストを維持します。 – lagfvu

関連する問題