0

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.

  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 11 '22 at 10:30

1 Answers1

0

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 {
    }
})
dopoto
  • 1,124
  • 1
  • 10
  • 20
  • Thank you for your answer. But is it fine to get logged in user details in APP_INITIALIZER? As APP_INITIALIZER will be executed all the time whether the user is logged in or not? – ayush sachdeva Feb 02 '22 at 16:01
  • Yes, I don't see why not. When I enter gmail.com in my browser, it will use a similar process to determine that I have logged in recently and it will just show me my Inbox, without asking me to log in again. – dopoto Feb 03 '22 at 04:57