Skip to content
Snippets Groups Projects
Commit d8a32996 authored by Radek Puš's avatar Radek Puš
Browse files

logging in by cookies

parent 5829112d
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,8 @@ export class AuthGuard implements CanActivate {
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return true;
//return this.LoginService.IsLoggedIn;
//console.log("is logged in", this.LoginService.IsLoggedIn.source);
//return true;
return this.LoginService.IsLoggedIn;
}
}
......@@ -18,6 +18,12 @@ export class LoginService {
}
 
get IsLoggedIn(): Observable<boolean> {
if (this.LoginStatus)
this.LoginStatus.asObservable();
//user is maybe comeing back -> try to login by cookie
this.LoginByCookie();
return this.LoginStatus.asObservable();
}
 
......@@ -27,6 +33,24 @@ export class LoginService {
 
constructor(private HttpClient: HttpClient, private Router: Router) { }
 
private LoginByCookie(): boolean {
let loginstatus = localStorage.getItem('loginStatus');
if (!loginstatus || loginstatus !== '1')
return false;
let storedDate = localStorage.getItem('expiration');
if (!storedDate || /^\s*$/.test(storedDate))//blank / empty / undefined
return false;
let parsedStoredDate = new Date(storedDate);
if (!parsedStoredDate || new Date() > parsedStoredDate) //null or expired date
return false;
//JWT token will be checked with first api call
this.LoginStatus.next(true);
return true;
}
login(username: string, password: string) {
return this.HttpClient.post<any>('api/Login', { username, password }).pipe(
map(result => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment