Reputation: 847
i create a login in angular6 and aspcore .
this is my loginService.ts :
LoginService(login:Login){
return this.http.post<Login>(`${this.appConfig.apiEndpoint}${this.appConfig.loginPath}`,login,{headers:this.headers})
.pipe(map(respose => {
if(!respose)
{
this.authStatusSource.next(false);
}
if (respose) {
// store user details and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify(respose));
this.authStatusSource.next(true);
}
return respose;
}));
}
and server return this :
{
"sub": "3002",
"unique_name": "[email protected]",
"jti": "597b383b-b5bc-46f1-939f-022b60d7de33",
"iat": "12/12/2018 7:27:21 AM",
"nbf": 1544599641,
"exp": 1549783641,
"iss": "dotnetthoughts.net",
"aud": "dotnetthoughts.net"
}
and this Login.ts :
loginform(user:Login){
this.loginService.LoginService(user).subscribe((data)=>{
this.router.navigate([this.returnUrl])
});
}
now i need to get sub
for find info of user . how can i do this ?
Edit
when i debug loginService it show me this :
C:\Program Files\nodejs\node.exe --inspect-brk=17368 dist\out-tsc\src\app\Services\login.service.js
Debugger listening on ws://127.0.0.1:17368/00cf837a-4eb0-428d-8320-b468d3ccdc47
Debugger attached.
e:\MyProject\StoreAp\dist\out-tsc\src\app\Services\login.service.js:13
import { Injectable, Inject } from '@angular/core';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
Upvotes: 0
Views: 268
Reputation: 10707
Try this:
this.loginService.LoginService(user).subscribe((data)=>{
if(data)
{
console.log(data.sub);
var userId = data.sub; // use userId to store, etc.
}
this.router.navigate([this.returnUrl])
});
Retrieving values from LocalStorage:
localStorage.setItem('user', JSON.stringify(user)); // to set values
var user = JSON.parse(localStorage.getItem('user')); // to get values from localStorage
console.log(user.sub); // tested in HTML code
Upvotes: 1
Reputation: 450
Try this :
this.LoginService(login).subscribe((data)=>{
localStorate.setItem('currentUser', JSON.stringify(data));
});
When you want to use the localStorage
use this code below :
let userInfo = JSON.parse(localStorage.getItem('currentUser'));
console.log('userInfo : ', userInfo);
console.log('userId : ', userInfo.sub);
Upvotes: 2
Reputation: 1206
Once you have set the item in local Storage memory, you can get the item value by this way,
JSON.parse(localStorage.getItem("currentUser")).sub
Upvotes: 1