Skip to content

Instantly share code, notes, and snippets.

@dcoffey3296
Last active September 17, 2019 15:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcoffey3296/d27c141ef79bec3ff6a6 to your computer and use it in GitHub Desktop.
Save dcoffey3296/d27c141ef79bec3ff6a6 to your computer and use it in GitHub Desktop.
Authentication redirect for angular-fullstack yeoman generator
// 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('/');
}
})
@SaintPeter
Copy link

Very helpful, thanks!

@TNowalk
Copy link

TNowalk commented Dec 28, 2015

Looks like you also need to add the same cookie logic to the authInterceptor to redirect after getting a 401. There were cases where I didn't have my UI routes requiring authentication and so the routes would load then be redirected after encountering a 401.

client/app/app.js

      // Intercept 401s and redirect you to login
      responseError: function(response) {
        if(response.status === 401) {
          // Create cookie with location info before redirecting
          if ($location.url() !== '/login') {
            $cookieStore.put('returnUrl', $location.url());
          }

          $location.path('/login');
          // remove any stale tokens
          $cookieStore.remove('token');
          return $q.reject(response);
        }
        else {
          return $q.reject(response);
        }
      }

@yeaske
Copy link

yeaske commented Sep 17, 2019

An option to use state instead of path/url
Before login:
$cookieStore.put('redirectState', next.name);

After login:
this.$state.go(this.$cookieStore.get('redirectState'));
this.$cookieStore.remove('redirectState');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment