1

A similar-ish question here (but not applicable).

In my SPA I'm using PassportJS to handle authentication. I have many routes which require the user to be logged in. I route like this:

// ... other routing stuff
when('/insights', {
    templateUrl: 'partials/insights',
    controller: 'insightsCtrl',
    resolve: { 
        loggedin: checkLoggedin 
    }
})

var checkLoggedin = function($q, $timeout, $http, $location, $rootScope){ 
  var deferred = $q.defer(); 
  $http.get('/loggedin').success(function(user){ 
    if (user !== '0') {
        $rootScope.user = user;
        deferred.resolve(); 
    }
    else { 
      deferred.reject(); 
      $location.url('/login'); 
    } 
  }); 
return deferred.promise; 
};

My route to check if logged in:

app.get('/loggedin', function(req, res) {
    res.send(req.isAuthenticated() ? req.user : '0');
});

If they're not logged in, they're redirected to /login. Once that is successfully done though, I'd like to go back to the original page they tried to request.

Is there a built-in way to store the route the user is trying to access so that I can redirect to it once they're logged in? I can do this with a cookie I suppose, but it feels a little clunky to do so...

Community
  • 1
  • 1
JVG
  • 20,198
  • 47
  • 132
  • 210

2 Answers2

2

There is no built in approach besides using the custom callback that PassportJS provides, but if you are writing this for a SPA, you could leverage $cacheFactory and create a service/factory that stores the last visit page of that user.

Limitations for $cacheFactory are the same as local storage which are that they are domain specific, so if you change domains, your local storage will not be the same cross-domain.

Sean Larkin
  • 6,290
  • 1
  • 28
  • 43
1

You can use a custom callback as explained in passportjs documentation. With custom callback, you can send the current query string to your server, and get a redirect to same query string after successful login.

Another possibility is to use localStorage to save current state and take it after login. You have many modules to use localStorage with angular like angular-local-storage.

Ygalbel
  • 5,214
  • 1
  • 24
  • 32
  • Great. When you say I can send a query to my server, how would this be achieved? Would rather not use `angular-local-storage` / any extra modules if I can help it. – JVG Dec 02 '15 at 20:14
  • Based on PassportJs documentation you can return a redirect action to your angular client. Like this: return res.redirect('/users/' + user.username). I can explain longer on chat. – Ygalbel Dec 02 '15 at 20:23
  • Sorry, I'm asking how to let Passport know which Angular route was originally requested (that is, from within Angular). – JVG Dec 02 '15 at 20:24
  • 1
    You can the route as a parameter as query string to you express server. loggedin?current=#myRoute#. You can access it in server side at req.query.current. – Ygalbel Dec 02 '15 at 20:29
  • Ah, stupid me, forgot I could pass params to `req` like that! Awesome, thank you. – JVG Dec 02 '15 at 20:30