Last active
April 22, 2018 17:53
-
-
Save ShankarSumanth/97b132249b89036cabe79919a3d6e8a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Observable } from 'rxjs/Observable'; | |
import { KeycloakApiService } from './keycloak/keycloak-api.service'; | |
import { IsLoggedInCheck } from './keycloak/keycloak.actions'; | |
import { selectKeycloakModel } from './keycloak/keycloak.reducer'; | |
import { AppState } from './reducers'; | |
import { Store } from '@ngrx/store'; | |
import { Injectable } from '@angular/core'; | |
import { | |
CanActivate, Router, | |
ActivatedRouteSnapshot, | |
RouterStateSnapshot | |
} from '@angular/router'; | |
@Injectable() | |
export class AuthGuard implements CanActivate { | |
constructor( | |
private router: Router, | |
private store: Store<AppState> | |
) { } | |
canActivate( | |
route: ActivatedRouteSnapshot, | |
state: RouterStateSnapshot | |
): Observable<boolean> { | |
let url: string = state.url; | |
return this.checkLogin(url); | |
} | |
checkLogin(url: string): Observable<boolean> { | |
return this.store.select(selectKeycloakModel) | |
.do(keycloak => { | |
if (!keycloak.isLoggedIn) { | |
window.localStorage.setItem('postLoginRedirect', url); | |
this.router.navigate(['/']); | |
} else { | |
const redirectUrl = window.localStorage.getItem('postLoginRedirect'); | |
window.localStorage.removeItem('postLoginRedirect'); | |
if (redirectUrl) { | |
console.log('Redirecting to ' + redirectUrl); | |
this.router.navigate([redirectUrl]); | |
} | |
} | |
}) | |
.map(keycloak => keycloak.isLoggedIn); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment