Last active
August 29, 2015 14:15
-
-
Save bioball/29025680643786c4bd82 to your computer and use it in GitHub Desktop.
Angular AUTH example
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
angular.module('app') | |
.factory('authentication', [ | |
'user', | |
'$state', | |
function(user, $state){ | |
return { | |
authenticate: function(protect){ | |
return user.identify() | |
.then(function(){ | |
return true; | |
}) | |
.catch(function(){ | |
if (protect){ | |
$state.go('login'); | |
} | |
return false; | |
}); | |
} | |
}; | |
} | |
]); |
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
angular.module('app') | |
.controller('homeController', [ | |
'isLoggedIn', | |
'user' | |
function(isLoggedIn, user){ | |
// here, isLoggedIn is a boolean of whether the user is logged in or not. | |
} | |
]) |
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
angular.module('app') | |
.config([ | |
'$stateProvider', | |
function($stateProvider){ | |
$stateProvider | |
.state('home', { | |
url: '/', | |
controller: 'homeController', | |
// add this to any route that you'd like to protect. | |
resolve: { | |
isLoggedIn: [ | |
'authentication', | |
function(authentication){ | |
return authentication.authenticate(); | |
} | |
] | |
} | |
}) | |
.state('login', { | |
url: '/login' | |
}) | |
} | |
]) |
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
angular.module('app') | |
.factory('user', [ | |
'$http', | |
'$q', | |
function($http, $q){ | |
var User = function(){ | |
this.name = null; | |
}; | |
User.prototype.isLoggedIn = function(){ | |
// however this is done. You can a "loggedIn" variable in closure or something. | |
}; | |
User.prototype.identify = function(){ | |
if(this.isLoggedIn()){ | |
return $q.when(true) | |
} | |
return $http.get("/auth", cookie) // this can be a cookie, or whatever token is used | |
}; | |
return new User() | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out the demo of ngAuth module see if you can make it work for what you need.
https://github.com/appmux/adhesive.js