3

In our angular application we use angular-oauth2-oidc for authenication (implicitFlow).

app.component.html:

<dt-menu></dt-menu>
<dt-header></dt-header>
<div class="content">
    <router-outlet></router-outlet>
</div>

app-routing.module.ts:

{ path: '', redirectTo: '/rooster', pathMatch: 'full' }, // default route
{ path: 'oauth', component: AuthResponseComponent },
{
     matcher: roosterRouteMatcher,
     component: RoosterComponent,
     canActivate: [AuthGuard],
     data: { subtitle: 'Rooster' }
  },
  { path: '**', component: PageNotFoundComponent }

For authentication we redirect to an external page/server. (login works).

The problem: When a user navigates to our application (s)he is not logged in and as such is redirected to the login page. What this means is that dt-menu and dt-header are drawn on the screen, a redirect happens, (user logs in), dt-menu and dt-header are drawn on the screen, the main component (rooster) is drawn.

This means that the user (with slower network speed) sees what looks like a flashing website (dt-menu + dt-headers -> login page -> dt-menu + dt-headers + dt-rooster)

I'm looking for a way to do a redirect to the login page before dt-menu + dt-headers are drawn.

What I have tried: I have tried to do a login (via angular-oauth2-oidc->initImplicitFlow) directly from the constructor of AppModule (app.module.ts), but inside initImplicitFlow Promises are used which means that dt-menu + dt-header will also be drawn before the redirect to the login page happens.

I also tried to add a component-less path (that has the canActive guard) with all of the other routes as its children. But with the same result.

Ajmal Sha
  • 906
  • 4
  • 17
  • 36
Pieter
  • 31
  • 3
  • 1
    Add `*ngIf` on your menu/header components. ` ` - update the variable `isUserLoggedIn` through some shared service which emits a `BehaviorSubject` after a user logs in/out which you can subscribe to in the AppComponent. This answer will help you: https://stackoverflow.com/a/46049546/1791913 – FAISAL Jan 31 '18 at 12:09
  • 1
    I had to adapt it a little bit but this idea works. Thanks! – Pieter Jan 31 '18 at 13:05

0 Answers0