1

My app has api_token in localstorage, and I want to determine a user using api_token from my REST before app starts. How can I do that? I've used routeResolvers, but I want to determine a user before app starts instead determine a user when route starts.

Paul Androschuk
  • 788
  • 6
  • 9

2 Answers2

3

If you just need to run some code before bootstrapping Angular, then you can just... run some code outside of Angular and have that code bootstrap the app.

For example:

// main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

// Here, some code retrieving api_token from localStorage.

// Only call the line below once your code is done.
// You might have to place that line in a callback of some sort.
platformBrowserDynamic().bootstrapModule(AppModule);

But I suspect you want to run code AND pass the retrieved value to Angular. In that case, take a look at APP_INITIALIZER. See explanation + code sample.

AngularChef
  • 13,133
  • 8
  • 51
  • 69
1

I recently answered this with sample code at - How to call an rest api while bootstrapping angular 2 app.

That is exactly what you are look for.

Community
  • 1
  • 1
Santanu Biswas
  • 4,514
  • 2
  • 22
  • 21