I'm trying to understand the authentication process with Angular and .Net core which sends me an jwt Bearer token(Works). So my Problem is that I don't know what i should do in the guard and auth service to manage users (with roles later) properly. But I tried some stuff and now I don't know what I should do in my AuthService.
EDIT I will get a Token and expiration date from server after login post is done. so i want to store those and later also claims but idk how. and what i should return.
This is how my AuthService looks like:
@Injectable()
export class AuthService {
isLoggedIn = false;
constructor(private http: HttpClient) {}
login( email:string, password:string ) :Observable<boolean>{
this.http.post('/login', {email, password})
.subscribe(data =>
{
//TODO: check for Token ???
let userAuth = data;
if(userAuth) {
localStorage.setItem('currentUser', JSON.stringify(userAuth));
return true;
}else{
return false;
}
},
(err: HttpErrorResponse) => {
if(err.error instanceof Error){
console.log("error client side");
}else{
console.log("Server side error.");
}
});
//what should i return ????? and how
}
logout() {
localStorage.removeItem('currentUser');
}
}
Now i don't know how to check if the token is there and how to return boolean if its Observable. And After After login is done what is also important to check for future investigations?
And m AuthGuard looks like this:
@Injectable()
export class AuthGuardService implements CanActivate{
constructor(private router:Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
boolean | Observable<boolean> {
if(localStorage.getItem('currentUser')){
return true;
}
this.router.navigate(['auth/login'],
{queryParams: { returnUrl: state.url}});
}
}