如何在角度中设置会话超时或过期令牌?

时间:2018-09-27 03:57:42

标签: angular local-storage angular-directive

当前我正在使用角度工具,我想设置自动注销或要在角度帮助中使自动会话过期?

authguard.guard.ts

export class AuthguardGuard implements CanActivate {

constructor (private user : SupportService,private router : Router) {}
canActivate(
  next: ActivatedRouteSnapshot,
  state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
   if(localStorage.getItem('username')){  
      return true;
   }else{
      this.router.navigate(['/login']);
      return false;
    }
  }
}

login-form.component.ts

export class LoginFormComponent implements OnInit {

userObj : User = new User();
constructor(private router : Router, private user : SupportService, private route : ActivatedRoute) { }
loginUser(e){
const formData = new FormData();
formData.append('username',this.userObj.username);
formData.append('password', this.userObj.password);
this.user.getUserPass(formData).subscribe(
  (data) => {
    if(data != undefined && data.payload != undefined && data.status == 1){
      e.preventDefault();
      this.router.navigate(['/base/dashboard']);    
      localStorage.setItem('userid',data.payload[0].userid);
      localStorage.setItem('username',data.payload[0].username);
    }else{
      alert('Please Enter Valid UserName OR Password');
     }    
    }  
   )
  }
}

1 个答案:

答案 0 :(得分:0)

销毁cookie后,您可以将cookie用作无效令牌的角,然后检查不具有此条件的cookie数据是否也作为令牌删除并获取新令牌