angular-auth-oidc-client icon indicating copy to clipboard operation
angular-auth-oidc-client copied to clipboard

[Question]: How to build a `PartialRoutesGuard` without auto-login

Open Ghostbird opened this issue 1 year ago • 0 comments

What Version of the library are you using? 18.0.0

Question

How can I make a custom guard, that works like the auto-login guard, except for the auto-login part. Instead I want to redirect the users to the app's internal login page. This is because we offer multiple different identity providers that the user can choose from.

I tried this documentation example but that doesn't work. I checked the library code, and oidcSecurityService.isAuthenticated$ is backed by private readonly authenticatedInternal$ = new BehaviorSubject<AuthenticatedResult>(DEFAULT_AUTHRESULT); from auth-state.service.ts. Where DEFAULT_AUTHRESULT.isAuthenticated is false.

The documentation for the property states:

Emits each time an authorization event occurs.

But that is clearly not true, as it always initially emits false.

Now you can see my problem. Once I get redirected back to my app, oidcSecurityService.isAuthenticated$.pipe(take(1)) always emits false exactly once, and therefore the route guard immediately concludes that you are not authenticated. That the AppComponent is still running checkAuth in the background, and will soon update the authentication state to say that you are authenticated is of no consequence.

Would it be possible to either:

  • Export more of the internals of the library? So that I can implement my own alternative to AutoLoginPartialRoutesGuard. Currently that's not possible, because the AutoLoginService, AuthStateService, and LoginService are not exported as part of this library's public API.
  • Make an extensible / configurable PartialRoutesGuard that makes the part that performs auto-login configurable?

I can probably workaround the route guard issue by putting a delay in my CallbackComponent before it routes. However,‌ I really like that the AutoLoginPartialRoutesGuard saves the target route and redirects back to the target route if you're logged in. In my opinion that is not related to automatic login at all. And I really want that feature. But to make that I would have to rewrite a lot of library internals simply because they're inaccessible.

Ghostbird avatar Jun 12 '24 11:06 Ghostbird