How to get current angular 4 user id?

I need to get the current user id in order to use it in different parts of my application.

login.component.ts:

onSignin(form: NgForm){
const UserName = form.value.UserName;
const Password = form.value.Password;
this.authService.signinUser(UserName,Password).map(res => res.json())
 .subscribe(
   data => {
     if (data.Id == 0){
     this.invalid=true;
    }
    else{
      localStorage.setItem('isLoggedin', 'true');
      this.router.navigate(['/calendar']);
      console.log('Ok');
   }})}

auth.service.ts:

signinUser(UserName,Password): Observable<any>{
  return this.http.get('http://api.##.com/api/security/signin?username='+UserName+'&password='+Password);
}
+4
source share
2 answers

, , redux, . ng2 Redux angular. https://www.npmjs.com/package/ng2-redux , . ,

sessionStorage.setItem('id', data.id);
retrieving from the session//
var data = sessionStorage.getItem('id');
console.log(data) to see the id in the console
+3

, , . , , .

.

  • - , . , , .
  • NGRX - , , .
  • - , . .

, NGRX , , .

+2
source

Source: https://habr.com/ru/post/1686812/


All Articles