Not authenticated in main.ts using open-id-connect

Hi, i’m trying to switch from aurelia-authentication to the aurelia-open-id-connect client.
Need to switch to a implicit client.

But i’m running into some problems.

Using Aurelia v1.

This is our old code in main.ts:

let authService = aurelia.container.get(AuthService);
aurelia.setRoot(authService.isAuthenticated() ? PLATFORM.moduleName(‘pages/app/app’) : PLATFORM.moduleName(‘pages/anonymous-pages/app’, ‘anon’));

Different root if authenticated vs not authenticated.

But with the new client:

let openIdConnect = aurelia.container.get(OpenIdConnect) as OpenIdConnect;
let user = await openIdConnect.userManager.getUser();
if (!user || user.expired) {
log.info(‘INIT MAIN: Not Autenticated’, user);
aurelia.setRoot(PLATFORM.moduleName(‘pages/anonymous-pages/app’));
}
else {
log.info(‘INIT MAIN: Autenticated!’, user);
aurelia.setRoot(PLATFORM.moduleName(‘pages/app/app’));
}

When returning back from identityserver 4 after a successful login, with a clean browser, then i’m not authenticated ?

If i do a F5 refresh, then i’m authenticated in main,ts / app.ts.

So not authenticated initially in main.ts, not in app.ts activate, when i hit a page in the app, then i’m authenticated.
Also in Preactivate step, then i’m also authenticated.

So how can i fix this ?
How can i check if i’m authenticated in main.ts so i can set different roots ?

Any tips ? Thanks…

Or:
How can I handle this without setting a new root for anonymous pages ?

The app has mostly secured pages.
And 4 anonymous pages like:
/message/{id}
/request/{id}

So when entering /message, no auth should be required.
If you are authenticated, then you should also be able to view the page.

Entering root /, then you have to login.
My login route is /login which is also anonymous.

Any examples that I can look into out there ?
Or any Tips ?

Should be a fairly common case ? But can’t find any useful info around this…

Maybe this part of documentation can help?
Aurelia router pipelines

Well multiple roots with the oidc client is a dead end.
Too much hassle to get it working. Authenticated state is just too late.

And we had an error in the initialization which caused alle pages to need authentication.
After switching to one root…
So that’s why anonymous pages did not work.

So after fixing this, anonymous pages works fine.
Much cleaner with one root. Just had to hide some stuff in app.html when anonymous.

Thanks.

1 Like