-
-
Save mattvv/183e2a4073557b77ecb3 to your computer and use it in GitHub Desktop.
routesecurity.js for ui-router
This file contains 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
(function (angular) { | |
'use strict'; | |
angular.module('iwillApp') | |
.run(function ($injector, $location, $rootScope, loginRedirectPath) { | |
if ($injector.has('$state')) { | |
new RouteSecurityManager($location, $rootScope, $injector.get('$state'), loginRedirectPath); | |
} | |
}); | |
function RouteSecurityManager($location, $rootScope, $state, path) { | |
this._state = $state; | |
this._location = $location; | |
this._rootScope = $rootScope; | |
this._loginPath = path; | |
this._redirectTo = null; | |
this._authenticated = !!($rootScope.auth && $rootScope.auth.user); | |
this._init(); | |
} | |
RouteSecurityManager.prototype = { | |
_init: function () { | |
var self = this; | |
this._checkCurrent(); | |
// Set up a handler for all future route changes, so we can check | |
// if authentication is required. | |
self._rootScope.$on('$stateChangeStart', function (e, next) { | |
self._authRequiredRedirect(next, self._loginPath); | |
}); | |
self._rootScope.$on('$firebaseSimpleLogin:login', angular.bind(this, this._login)); | |
self._rootScope.$on('$firebaseSimpleLogin:logout', angular.bind(this, this._logout)); | |
self._rootScope.$on('$firebaseSimpleLogin:error', angular.bind(this, this._logout)); | |
}, | |
_checkCurrent: function () { | |
// Check if the current page requires authentication. | |
if (this._state.current) { | |
this._authRequiredRedirect(this._state.current, this._loginPath); | |
} | |
}, | |
_login: function () { | |
this._authenticated = true; | |
if (this._redirectTo) { | |
this._redirect(this._redirectTo); | |
this._redirectTo = null; | |
} | |
else if (this._location.path() === this._loginPath) { | |
this._location.replace(); | |
this._location.path('/'); | |
} | |
}, | |
_logout: function () { | |
this._authenticated = false; | |
this._checkCurrent(); | |
}, | |
_redirect: function (path) { | |
this._location.replace(); | |
this._location.path(path); | |
}, | |
// A function to check whether the current path requires authentication, | |
// and if so, whether a redirect to a login page is needed. | |
_authRequiredRedirect: function (state, path) { | |
if (state.authRequired && !this._authenticated) { | |
if (state.pathTo === undefined) { | |
this._redirectTo = this._location.path(); | |
} else { | |
this._redirectTo = state.pathTo === path ? '/' : state.pathTo; | |
} | |
this._redirect(path); | |
} | |
else if (this._authenticated && this._location.path() === this._loginPath) { | |
this._redirect('/'); | |
} | |
} | |
}; | |
})(angular); |
Hey Matt, will this only work if the application was scaffold using yeoman generator?? As I have scaffold my application Ionic and I have tried to used this but it doesn't work at all? What do I have to do to make it work?? Any suggestion or solution for this?? Thanks in advance
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks! Make sure to set
app.constant('loginRedirectPath', '/login')
so the module can be instantiated