Forked from dcoffey3296/angular-fullstack-redirect.js
Last active
August 29, 2015 14:27
-
-
Save SaintPeter/1bb1670e65aad22958bb to your computer and use it in GitHub Desktop.
Authentication redirect for angular-fullstack yeoman generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Yeoman angular-fullstack generator allows for authentication to be required | |
// when a user goes to a given route. To enable this behavior, add | |
authenticate: true | |
// to your /client/app/<routename>/<routename>.js file, in the .when routing block. | |
// like this: | |
angular.module('meanApp') | |
.config(function ($routeProvider) { | |
$routeProvider | |
.when('/', { | |
templateUrl: 'app/main/main.html', | |
controller: 'MainCtrl', | |
authenticate: true // Require Authentication for this route | |
}); | |
}); | |
// This works well enough, but after authentication you are redirected back to the root | |
// of your site. If you want to store the intended url and redirect to it, follow the | |
// steps beloow. | |
// Note: For Angular v1.4 and greater you may need to replace all instances of | |
// $cookieStore with $cookies, depending on your version of ngCookies | |
// 1. store the url to return to in a cookie within the '.run()' method of `client/app/app.js` | |
// Note: You must add $cookieStore to the .run function parameter list | |
.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, edit `client/app/account/login/login.controller.js` | |
// Add $cookieStore to the .controller function parameters: | |
.controller('LoginCtrl', function ($scope, Auth, $location, $window, $cookieStore) { | |
// 4. Then check for cookie in the `.then()` part of `$scope.login()`, file: | |
.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('/'); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment