2017-04-27 92 views
3

我想這是一個非常簡單的問題,但不幸的是我不知道如何處理它。將驗證服務與AuthGuard連接(簡單問題)

我正嘗試將UserAuthenticationService服務連接到ActivationGuard

UserAuthenticationService.ts

import {Injectable} from '@angular/core'; 
import {Http} from '@angular/http'; 

@Injectable() 
export class UserAuthenticationService { 
    isUserAuthenticated: boolean = false; 
    username: string; 

    constructor(private http: Http) { 
    } 

    authentication() { 
     this.http.get(`http://localhost/api/auth/isLogged/${this.username}`) 
      .subscribe(res => { //^^returns true or false, depending if the user is logged or not 
        this.isUserAuthenticated = res.json(); 
       }, 
       err => { 
        console.error('An error occured.' + err); 
       }); 
    } 


} 

ActivationGuard.ts

import {Injectable} from '@angular/core'; 
import {Router, RouterStateSnapshot, ActivatedRouteSnapshot} from '@angular/router'; 
import {Observable} from 'rxjs/Observable'; 
import {UserAuthenticationService} from './UserAuthenticationService'; 

interface CanActivate { 
    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean 
} 

@Injectable() 
export class WorksheetAccessGuard implements CanActivate { 

    constructor(private router: Router, private userService: UserAuthenticationService) { 
    } 

    public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { 

     if (this.userService) { 
      this.router.navigate(['/']); 
      return false; 
     } 
     return true; 
    } 
} 

它的偉大工程,如果我只是用localStorage來存儲信息,如果用戶登錄或不:

public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { 

    if (!localStorage.getItem('currentUser')) { 
     this.router.navigate(['/']); 
     return false; 
    } 
    return true; 
} 

但是,如何將服務與警衛連接?期待任何形式的幫助。先謝謝你。

如果您需要更多信息,請讓我知道,我將編輯我的帖子。無論是在構造或在ngOnit UserAuthenticationService的

+0

@羅伊現在好點了嗎? – Kaysh

+0

也許這只是我,但你已經注入驗證服務到WorksheetAccessGuard。那麼? (你是什麼意思「_但我怎麼能連接服務與guard_」) –

+0

@羅伊有什麼缺失。 – Kaysh

回答

0

呼叫認證()方法然後設置可變isUserAuthenticated和使用的是,在ActivationGuard.ts

UserAuthenticationService.ts:

import {Injectable} from '@angular/core'; 
import {Http} from '@angular/http'; 

@Injectable() 
export class UserAuthenticationService { 
    isUserAuthenticated: boolean = false; 
    username: string; 

    constructor(private http: Http) { 
    this.authentication(); 
    } 

    authentication() { 
     this.http.get(`http://localhost/api/auth/isLogged/${this.username}`) 
      .subscribe(res => { //^^returns true or false, depending if the user is logged or not 
        this.isUserAuthenticated = res.json(); 
       }, 
       err => { 
        console.error('An error occured.' + err); 
       }); 
    } 


} 

ActivationGuard.ts

@Injectable() 
export class WorksheetAccessGuard implements CanActivate { 

    constructor(private router: Router, private userService: UserAuthenticationService) { 
    } 

    public canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { 

     if (this.userService.isUserAuthenticated) { 
      this.router.navigate(['/']); 
      return false; 
     } 
     return true; 
    } 
} 
+0

每次都會返回'false',不幸的是...... – Kaysh

0

這不是正確的做法。每次調用服務時,它都會初始化一個新實例,因此您會得到一個錯誤信息。

你應該創建一個單獨的服務實例(通過您的應用程序的主模塊) - 它會包含您的應用程序狀態(內存/ localStorage的)

然後,當你會打電話給UserAuthenticationService - 你贏了」 t更新其owbn參數,但主要的一個(單身人士)。

我建議你使用BehaviourSubject(閱讀它,它就像一個主題,但它也會產生它的最後一個值,而不用等待手動發出一個值)。

從那時起,您的應用可以從任何地方看到用戶是否登錄。