如何返回当前用户?

时间:2018-07-27 09:34:27

标签: jwt angular5

我正在使用JWT进行角度5的身份验证,我想让当前用户稍后再使用它。 但是我做不到! 这是我的login.ts

     @Component({
     selector: 'app-login',
    templateUrl: './login.component.html',
    styleUrls: ['./login.component.css']
  })
   export class LoginComponent implements OnInit {
   mode:number=0;

  constructor(private authService:AuthenticationService, private router:Router) 
   { }

 ngOnInit() {
  }
   onLogin(user){
   this.authService.login(user).subscribe(resp=>{
  let jwt = resp.headers.get("Authorization");
  this.authService.saveToken(jwt);
  this.router.navigateByUrl("/users");

},
err=>{
  this.mode=1;
})
   }

  }

这是我的服务:

   @Injectable()
  export class AuthenticationService {

   private host:string = "http://localhost:8080";
   private jwtToken = null;
  name ;
  private roles:Array<any>;
 userco;
 constructor(private http:HttpClient) { }

 login(user){
return  this.http.post(this.host+"/login",user,{observe: 'response'});

  }

   saveToken(jwt:string){
this.jwtToken = jwt;
localStorage.setItem('token',jwt);
let jwtHelper = new JwtHelper();
this.roles = jwtHelper.decodeToken(this.jwtToken).roles;

  }
   loadToken(){
this.jwtToken = localStorage.getItem('token');
   }

一切顺利,但我不知道如何显示当前用户!

0 个答案:

没有答案