angularjsauthenticationhttp-redirectmean-stackangular-fullstack

Redirect to original request after authentication, Angular-Fullstack?


I am using the angular-fullstack (https://github.com/DaftMonk/generator-angular-fullstack) from the yeoman generator for the MEAN stack. I am new to most of these technologies and am just beginning to wrap my head around how the pieces fit together.

I am trying to figure out how to redirect a freshly authenticated user to the URL that they originally requested before they logged in.

In

myproject/server/auth/auth.service.js

there is this function which appears to redirect back to '/' after an oAuth login:

/**
 * Set token cookie directly for oAuth strategies
 */
function setTokenCookie(req, res) {
  if (!req.user) return res.json(404, { message: 'Something went wrong, please try again.'});
  var token = signToken(req.user._id, req.user.role);
  res.cookie('token', JSON.stringify(token));
  res.redirect('/');
}

How would I go about remembering the original request for both oAuth AND local login and then were would I redirect the user appropriately after they log in? Thanks!!


Solution

  • I figured this out, finally! I had to make changes in 3 files. I made a gist to highlight the changes:

    https://gist.github.com/dcoffey3296/d27c141ef79bec3ff6a6

    
    
    // I figured it out, here are the steps I took to solve this problem.  For some reason, Stack Overflow isn't formatting my last 2 code blocks below.
    
    
    
    // 1. store the url to return to in a cookie within the '.run()' method of `client/app/app.js`
    
            .run(function ($rootScope, $location, Auth, $cookieStore) {
    
            // Redirect to login if route requires auth and you're not logged in
    
            $rootScope.$on('$stateChangeStart', function (event, next) {
    
              Auth.isLoggedInAsync(function(loggedIn) {
    
                if (next.authenticate && !loggedIn) {
    
        
    
                  // store the requested url if not logged in
    
                  if ($location.url() != '/login')
    
                  {
    
                    $cookieStore.put('returnUrl', $location.url());
    
                  }
    
                  $location.path('/login');
    
                }
    
              });
    
            });
    
          });
    
    
    
     // 2. for Oauth, check for this cookie and redirect if it exists in `server/auth/auth.service.js`
    
        function setTokenCookie(req, res) {
    
          if (!req.user) { 
    
            return res.json(404, { message: 'Something went wrong, please try again.'}); 
    
          }
    
          
    
          var token = signToken(req.user._id, req.user.role);
    
          res.cookie('token', JSON.stringify(token));
    
        
    
          // return the user to the request page (oAuth) or homepage
    
          if (typeof req.cookies.returnUrl != 'undefined')
    
          {
    
              res.redirect(req.cookies.returnUrl.replace(/"/g, "") || '/');
    
          }
    
          else
    
          {
    
            res.redirect('/');
    
          }
    
        }
    
    
    
     // 3. for local login, check for cookie in the `.then()` part of `$scope.login()`, file: `client/app/account/login/login.controller.js`
    
        .then( function() {
    
                  // Logged in, redirect to home
    
                  if (typeof $cookieStore.get('returnUrl') != 'undefined' && $cookieStore.get('returnUrl') != '')
    
                  {
    
                    $location.path($cookieStore.get('returnUrl'));
    
                    $cookieStore.remove('returnUrl');
    
                  }
    
                  else
    
                  {
    
                    $location.path('/');
    
                  }
    
                })