0

Note: Before asking this question i went to lot of other answers and blogs. i also visited and understood. Make AJAX request before bootstrapping Angular2 application properly.. With introduction to bootstrapModule all the previous answers doesn't work. and all answers has APIs which are not available in angular2's stable release

Question: I am trying to solve very basic problem with login/authentication. If user copy the link http://domain.com/login and if he is already logged in I want to redirect him to home.

The way i tried is on route change I check authentication and then redirect. but that takes time as one has to wait till response comes back. what i want is even before the angular bootstrap happens i want to make an ajax call and get the current user from server. if user is not logged in that call will not give any user and that information should be available to Angular via some service let say authService. so at any place we can use authService.user to check if user is logged in or not.

not i can make ajax call with native JS xmlHttpRequest and store use in window object. and on bootstrap access that window object in authService that works fine. but i think using window object is not a good solution.

please guide me what would be the possible solution.

below is my main.js and i have to use bootstrapMoudle and not bootstrap(component);

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

const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule)
  .catch(err => console.error(err));
Community
  • 1
  • 1
gaurang171
  • 9,032
  • 4
  • 28
  • 30
  • Did you tried to use guards for children routes? – galvan Sep 23 '16 at 21:07
  • [APP_INITIALIZER](https://angular.io/docs/ts/latest/api/core/index/APP_INITIALIZER-let.html) is still available in Angular2 Final. You should be able to use it as describe in [this response](http://stackoverflow.com/questions/37611549/how-to-pass-parameters-rendered-from-backend-to-angular2-bootstrap-method). But you said that you have already looked in this topic. – Noémi Salaün Sep 23 '16 at 21:21
  • saving in window object should work for you as you mentioned, why do you think using window object is not good solution? we use windows object for all sort of things like jquery, momemt etc or any other globally loaded libraries, why not for this purpose? – Madhu Ranjan Sep 23 '16 at 21:23
  • @galvan I am using Guards but that doesn't re authenticate on reload automatically. – gaurang171 Sep 24 '16 at 00:49
  • @NoémiSalaün APP_INITIALIZER can be a solution but its experimental so i am afraid that its fate is going to be same like HTTP_PROVIDERS and others. will try to evaluate this options. thanks for suggetion – gaurang171 Sep 24 '16 at 00:51
  • @MadhuRanjan Yes window objects works perfectly fine for initial configuration values like my case. i think i will keep this approach and once the values are in in service will clear those value from windows. thank you. – gaurang171 Sep 24 '16 at 00:53
  • Using this appoach avoiding window the only thing is i am still writing XHR code. but that should be okay. https://github.com/angular/angular/issues/11195#issuecomment-248020928 The question marked as duplicate lead me to this answer – gaurang171 Sep 24 '16 at 01:08
  • If you want to avoid vanilla XHR, you can use [this](http://stackoverflow.com/q/39452451/2587435) – Paul Samsotha Sep 24 '16 at 01:29
  • You can include [@nglibs\config](https://github.com/nglibs/config) in your project, and config utility will provide you required config data from JSON or WebAPI. – Burak Tasci Feb 07 '17 at 10:53

0 Answers0