0

I am trying to call an http GET method to check if the user has a session with my back end NodeJS API, but I can't see the request being made.

I have an observable that is set when the user logs in so my AuthGuard will check this first, if this is set to false then call the back end to see if there is an existing session.

auth.guard.ts

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {

  this.authService.loggedIn.subscribe(currentState => {
    this.loggedIn = currentState;
  });

  if (this.loggedIn) {
    return true;
  } else {
    console.log('check for login');
    this.http.get(environment.apiURL + '/checklogin'
    , { withCredentials : true }
      ).map(user => {
          const resp = user.json();
          // login successful if there's a user
            if (resp.isLoggedIn) {
              this.authService.obsLoggedIn.next(true);
              return true;
            } else {
              this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
              console.log('about to return false for authguard');
              return false;
            }
        });
      }
   }

app.js

app.get('/checklogin', function(req, res) {
console.log('get from login ' + req.body);
req.session.user ? res.status(200).send({isLoggedIn: true}) : res.status(200).send({isLoggedIn: false});
});

I know there is nothing wrong with my apiURL since going to the login page and logging in works fine, and I can see back-end logging.

Can anyone see why my http request would not work?

rustgamer3434
  • 43
  • 1
  • 6
  • So you're having trouble with the http get call towards `/checklogin`? Also, which one of angulars Http service are you currently using? `Http` or `HttpClient`? In case of the `HttpClient` the mapping to json is not necessary anymore, instead you define the type of the calls result and it is automatically parsed. The problem that I see is that you don't subscribe to the `/checklogin` call, you just define what happens when it's done. In order to execute it, I think you have to subscribe to it first. – Benedikt Schmidt May 07 '18 at 11:33
  • Correct. I have trouble with the get call to /checklogin. And I am using Http at the moment. I know I am suppose to change to HttpClient. Would that be an issue? – rustgamer3434 May 07 '18 at 11:45
  • Yes you are right, I changed from map to subscribe. Thanks! – rustgamer3434 May 07 '18 at 12:06
  • It probably is not a problem right now, but it is deprecated and surely will be removed in a later version of angular. So if you want to stay up to date with the version, I highly recommend switching to the new HttpClient – Benedikt Schmidt May 07 '18 at 14:47

0 Answers0