0

I have an angular2 project,

I blocked some route components by using 'canActivate' Guard

For example: The guard check if the user is logged in and block components for registered users only.

But, I have a problem with the timing of the guard call.

When app load - I call to AutoLogin request (login with token). The guard called before the request finished.

So, If the user refreshed page with guard, the guard block the page before the request finished.

Is there any way to call to the guard when the request finished?

----UPDATE----

My code structure:

user.service include:

  1. Autologin call - return Observable
  2. EventEmitter for observe the user changes when changes happen (login, logout, update etc...)
  3. Function for get the current saved user

app.component include:

  1. The constructor call to autologin request - for load the updated user when enter to the site

loggedin.guard contain:

  1. check if user loggedin right now (not only if the token saved)

I want to check the guard again after the user changed after the autologin finished the request in the app.component

zeevblu
  • 1,059
  • 2
  • 11
  • 26

1 Answers1

0

The guard should be on pages where the user is not anonymous, so at Login page there shouldn't be guard.
Guard will fire in canActivate cycle of page, it will check if user exists if it is then it will return true, if not it will return false.
When user not exists you can check if session token ( saved on the browser / cookie ) exists, if yes then call authenticate method.

Haddar Macdasi
  • 3,477
  • 8
  • 37
  • 59
  • Thanks, but my app save the token in cookies to call to autologin request with the saved token. The login page doesn't contain guard. My problem is when I refreshed the other page with guard directly, the app call to the login request and the page blocked by the guard before request finished. – zeevblu Mar 26 '17 at 19:08
  • I've outlined the way guard can be implemented, the autologin part on app load is something I don't understand. – Haddar Macdasi Mar 26 '17 at 19:13