I'm using Angular 8.0.3, keycloak 8.0.0 and the keycloak-service 7.0.1
I'm trying to make some public pages, so I'm using 'check-sso'. If I have understood correctly, if the user is not logged-in, the browser will be redirected back to the application and remain unauthenticated.
I have this routing for my app :
const routes: Routes = [
{
path: '',
loadChildren: () => import('src/app/pages/pages.module').then(m => m.PagesModule),
canLoad: [IsAuthenticateGuard]
},
{
path: 'login',
ponent: LoginComponent,
canLoad: [IsNotAuthenticateGuard]
},
{
path: '**',
redirectTo: ''
}
]
guards :
export class IsAuthenticateGuard implements CanLoad {
constructor(private keycloakService: KeycloakService, private router: Router) {}
canLoad(
route: Route,
segments: UrlSegment[]
): Observable<boolean>|Promise<boolean>|boolean {
return this.keycloakService.isLoggedIn().then(isLogged => {
if (!isLogged) {
this.router.navigateByUrl('/login');
}
return isLogged;
});
}
}
export class IsNotAuthenticateGuard implements CanActivate {
constructor(private keycloakService: KeycloakService,
private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.keycloakService.isLoggedIn().then(isLogged => {
if (isLogged) {
this.router.navigateByUrl('/home');
}
return !isLogged;
});
}
}
And I have this init config for my keycloak :
keycloakService.init({
config: keycloakConfig,
initOptions: {
onLoad: 'check-sso',
checkLoginIframe: false
},
bearerExcludedUrls: ['/login'] // (I tried also '.*')
})
but I'm redirected to :
http://localhost:4200/#error=login_required&state=0b36094c-92ac-4703-a800-77f27e4d206d
and nothing append...
I read this, but not find the solution :
.html#_javascript_adapter
I'm using Angular 8.0.3, keycloak 8.0.0 and the keycloak-service 7.0.1
I'm trying to make some public pages, so I'm using 'check-sso'. If I have understood correctly, if the user is not logged-in, the browser will be redirected back to the application and remain unauthenticated.
I have this routing for my app :
const routes: Routes = [
{
path: '',
loadChildren: () => import('src/app/pages/pages.module').then(m => m.PagesModule),
canLoad: [IsAuthenticateGuard]
},
{
path: 'login',
ponent: LoginComponent,
canLoad: [IsNotAuthenticateGuard]
},
{
path: '**',
redirectTo: ''
}
]
guards :
export class IsAuthenticateGuard implements CanLoad {
constructor(private keycloakService: KeycloakService, private router: Router) {}
canLoad(
route: Route,
segments: UrlSegment[]
): Observable<boolean>|Promise<boolean>|boolean {
return this.keycloakService.isLoggedIn().then(isLogged => {
if (!isLogged) {
this.router.navigateByUrl('/login');
}
return isLogged;
});
}
}
export class IsNotAuthenticateGuard implements CanActivate {
constructor(private keycloakService: KeycloakService,
private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.keycloakService.isLoggedIn().then(isLogged => {
if (isLogged) {
this.router.navigateByUrl('/home');
}
return !isLogged;
});
}
}
And I have this init config for my keycloak :
keycloakService.init({
config: keycloakConfig,
initOptions: {
onLoad: 'check-sso',
checkLoginIframe: false
},
bearerExcludedUrls: ['/login'] // (I tried also '.*')
})
but I'm redirected to :
http://localhost:4200/#error=login_required&state=0b36094c-92ac-4703-a800-77f27e4d206d
and nothing append...
I read this, but not find the solution :
https://github./mauriciovigolo/keycloak-angular https://www.keycloak/docs/latest/securing_apps/index.html#_javascript_adapter
Share Improve this question asked Dec 11, 2019 at 15:41 Ugo EvolaUgo Evola 6475 gold badges10 silver badges26 bronze badges1 Answer
Reset to default 4You are using onLoad: 'check-sso'
which only authenticates the client if the user if logged in.
You should use onLoad: 'login-required'
instead.