Main.ts open-id-connect signin-oidc loop

Hey all
I’m back with a new question regarding aurelia and aurelia-open-id-connect.
I have a identity server running where my aurelia client connects to. Before a user can use the client he or she must be logged in first.
In my main.ts i check if the openIdConnect instance has a user and that the user is still valid.
If the user is not valid i call the openIdConnect.login() whichs redirects the user to the ID server. After login the user gets redirected back to the aurelia client.

Somehow when using the check in main.ts results in a signin-oidc loop. When using the same construction in my login.ts but with a different success routing, i have no loop and i am directly being send to the application.
do i need to do something differently in my main.ts?

main.ts

    return aurelia.start().then(() =>
    {
        let openIdConnect: OpenIdConnect = aurelia.container.get(OpenIdConnect);
        
        openIdConnect.userManager.getUser().then((user) =>
        {
            console.log(user);
            if (!user || !user.access_token || user.access_token && user.expired)
            {
                openIdConnect.login();
            }
            else
            {
                console.log('have user');
                console.log(user);
                let baseService: BaseService = aurelia.container.get(BaseService);
                baseService.setAccessToken(user);
                aurelia.setRoot(PLATFORM.moduleName("app"));
            }
        });
        
    });

login.ts

public login()
    {
        let openIdConnect: OpenIdConnect = Container.instance.get(OpenIdConnect);

        openIdConnect.userManager.getUser().then((user) =>
        {
            console.log(user);
            if (!user || !user.access_token || user.access_token && user.expired)
            {
                openIdConnect.login();
            }
            else
            {
                console.log('have user');
                console.log(user);
                let baseService: BaseService = Container.instance.get(BaseService);
                baseService.setAccessToken(user);
                this.router.navigateToRoute('dashboard');
            }
        });
    }
1 Like

Hi again :slight_smile:

aurelia-open-id-connect works with the router for the redirect loop to work, it creates a specific route for handling the oidc login callback and singing you in. The scenario where you have to be authenticated before loading the routes isn’t supported OOTB.
That being said, it’s quite trivial to write some code in order for this to work, see https://github.com/aurelia-contrib/aurelia-open-id-connect/issues/25#issuecomment-305321359

I used the code above in a project where I had the same requirements as you (need to be logged in in order to do anything in the app), give it a try and report back when you are still stuck

1 Like

@arnederuwe hi!
Thnx it worked! I did came across that solution but somehow i wasn’t really thinking that issue would resolve my issue.

Thnx again!

1 Like