Skip to content

Logging in when starting the application, keycloak-angular 19 #600

Closed
@Rezorl

Description

@Rezorl

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search for issues before submitting
- [ ] feature request

Versions.

"keycloak-angular": "19.0.1",
"keycloak-js": "25.0.5",
"@angular/core": "19.0.3",

Repro steps.

const isAccessAllowed = async (route: ActivatedRouteSnapshot, __: RouterStateSnapshot, authData: AuthGuardData): Promise<boolean | UrlTree> => {
  const { authenticated, grantedRoles } = authData;

  console.log(authData.keycloak.authenticated); //always false
  console.log(authenticated); //always false

   if (!authenticated) {
    const keycloak = inject(Keycloak);
    return await keycloak.login().then(() => true);
  }

  const requiredRole = route.data['role'];
  if (!requiredRole) {
    return false;
  }

  const hasRequiredRole = (role: string): boolean =>
    Object.values(grantedRoles.resourceRoles).some((roles) => roles.includes(role));

  if (authenticated && hasRequiredRole(requiredRole)) {
    return true;
  }

  return Promise.resolve(false);
}

export const keycloakAppGuard = createAuthGuard<CanActivateFn>(isAccessAllowed);




export const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    canActivate: [keycloakAppGuard],
    data: { role: 'read-app' }
  },
];




provideKeycloak({
  config: {
    clientId: 'test-client',
    realm: 'TestApp',
    url: 'http://localhost:8888',
  },
  initOptions: {
    onLoad: 'check-sso',
    silentCheckSsoRedirectUri:
      window.location.origin + '/assets/silent-check-sso.html'
  }
})

The log given by the failure.

Desired functionality.

When starting the application, I want to check whether the user is logged in:

If so, it enters the application.

If not, it is redirected to the login page.

  console.log(authData.keycloak.authenticated); //always false
  console.log(authenticated); //always false

Even after logging in to the keycloak website and being redirected to the application

Metadata

Metadata

Labels

bugThis issue/PR is a bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions