'How to fetch logged in use details before initializing the application using JWT token from Cookies

I have an angular application where I am generating JWT token using SSO login. Now, I want to fetch logged-in user details before initializing the application and store the data on run time. I also want to use user data in Route guards to validate if the user belongs to any specific group.



Solution 1:[1]

You should use APP_INITIALIZER in your app.module, something along the lines of:

const initConfig =
    srv: UserService) =>
    () => {
        const loadConfigPromise = srv.initialize();
        loadConfigPromise.then(() => {
            ...
        });
        return loadConfigPromise;
    };

@NgModule({
            providers: [
                {
                    provide: APP_INITIALIZER,
                    useFactory: initConfig,
                    multi: true,
                    deps: [UserService],
                },
            ],
        imports: [
         ...
        ],
    })
    export class AppModule {
    }
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dopoto