简体   繁体   English

如何使用angular2和Typescript对页面进行身份验证

[英]How to do authentication for pages using angular2 and typescript

i have a login page and 3 more pages along with that. 我有一个登录页面以及另外3个页面。 Now i need to do authentication function. 现在我需要执行身份验证功能。 If the authentication is undefined then it must redirected to login page, if authentication is true it must go to checkLogin function. 如果身份验证未定义,则必须重定向到登录页面;如果身份验证为真,则必须转到checkLogin函数。 I am not getting how to do this but i had tried in this way,in one of the pages. 我没有得到如何执行此操作,但我在其中一页中以这种方式尝试过。

ts: ts:

import { Router, CanActivate } from '@angular/router';

@Injectable()
export class PageComponent implements CanActivate {

    constructor(private router: Router) { }

    canActivate() {
      if (localStorage.getItem('authToken') == undefined ) {
        this.router.navigate(['/login']);
      }
      else {
            /*Checklogin*/    
        this.ApiService
            .checklogin()
            .subscribe(
              user  => {}
      }
        }

but i get an error: 但我得到一个错误:

Class 'PageComponent' incorrectly implements interface 'CanActivate'.
  Types of property 'canActivate' are incompatible.
    Type '() => void' is not assignable to type '(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => boolean | Observable<boolean> | Pr...'.
      Type 'void' is not assignable to type 'boolean | Observable<boolean> | Promise<boolean>'.

I dont know what i am doing is correct or not, can anyone guide me 我不知道我在做什么正确与否,谁能指导我

You have to return boolean or Observable<boolean> or Promise<boolean> from canActivate 您必须从canActivate返回boolean or Observable<boolean> or Promise<boolean>

Also

canActivate should be something like this canActivate应该是这样的

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
    //Your logic
}

Example

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){
    if (localStorage.getItem('authToken') == undefined ) {
        return false; 
    }
  else {    
    return new Observable<boolean>((observer)=>{
        this.ApiService
        .checklogin()
        .subscribe((res)=>{
           //Check user
           if(condition){
              observer.next(true);
           }
           else{
             observer.next(false);
           }
           observer.complete();
        });
   });
  }

}

CanActivate route guard expect a boolean , Observable or a Promise to be returned. CanActivate路由守卫期望返回booleanObservablePromise Based on the value returned the page is authenticated. 根据返回的值对页面进行身份验证。

In your case you can probably do something like this. 在您的情况下,您可能可以执行以下操作。

canActivate() {
  if (localStorage.getItem('authToken') == undefined ) {
    return false;
  }
  else {  
         return this.ApiService.checklogin()
      }
    }

In the implementation of checklogin you resolve/reject you promise/observable. checklogin的实现中,您解决/拒绝您的诺言/可观察的。

You can go through following article: https://ryanchenkie.com/angular-authentication-using-route-guards 您可以阅读以下文章: https : //ryanchenkie.com/angular-authentication-using-route-guards

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

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