I'm able to get the value for the token after logging in because I'm passing in the values for the username and password from a function. On the next page, I'm not passing in those values. Which makes sense as to why I'm getting null. So is the most efficient way for getting that value is passing the username and password from page to page and calling that function?
Auth.ts
login(userName: string, password: string, route: string = null): any {
this.logout(false);
this.doLogin(userName, password)
.subscribe(response => {
let token:string = JSON.stringify(response.access_token);
this.storage.set('token', token);
console.log(token, 'this is the token here')
});
}
getToken(){
this.storage.get('token').then((val) => {
this.accessToken = val;
})
if(this.accessToken == undefined || this.accessToken == null){
return '0'
}
else{
return "Bearer " + this.accessToken.replace(/["]+/g, '')
}
}
component.ts
login(){
this.authService.login(this.account.username, this.account.password)
}
Interceptor.ts
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let access = this.auth.getToken()
const authReq = req.clone({
setHeaders: {
Authorization: access
}
});
return next.handle(authReq)
}
Thanks!