Last active
December 19, 2015 00:19
-
-
Save christiansmith/5868086 to your computer and use it in GitHub Desktop.
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', ['authentication']) | |
.config(function ($stateProvider, $locationProvider) { | |
// ... | |
}) | |
.run(function (CurrentUser) { | |
CurrentUser.session(); | |
}) |
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
'use strict'; | |
angular.module('authentication') | |
.factory('CurrentUser', function ($http, $location) { | |
function CurrentUser () {} | |
CurrentUser.isAuthenticated = false; | |
CurrentUser.requiresAuthentication = false; | |
/** | |
* | |
*/ | |
CurrentUser.prototype.requireAuthentication = function () { | |
CurrentUser.requiresAuthentication = true; | |
}; | |
/** | |
* We need to pass a function to $scope.watch | |
*/ | |
CurrentUser.prototype.requiresAuthentication = function () { | |
return CurrentUser.requiresAuthentication; | |
} | |
CurrentUser.prototype.isAuthenticated = function(user) { | |
if (user) { | |
var key; | |
for (key in user) { this[key] = user[key]; } | |
CurrentUser.isAuthenticated = true; | |
CurrentUser.requiresAuthentication = false; | |
return CurrentUser; | |
} else { | |
return CurrentUser.isAuthenticated; | |
} | |
}; | |
/** | |
* Signup | |
*/ | |
CurrentUser.prototype.signup = function(registration) { | |
var self = this; | |
return $http.post('/signup', registration).then(function (response) { | |
self.isAuthenticated(response.data.user); | |
$location.path('/') | |
}); | |
}; | |
/** | |
* Login | |
*/ | |
CurrentUser.prototype.login = function(credentials) { | |
var self = this; | |
return $http.post('/login', credentials).then(function (response) { | |
self.isAuthenticated(response.data.user); | |
$location.path('/') | |
}); | |
}; | |
/** | |
* Reset | |
*/ | |
CurrentUser.prototype.reset = function() { | |
var key; | |
for (key in this) { delete this[key]; } | |
CurrentUser.isAuthenticated = false; | |
CurrentUser.requiresAuthentication = false; | |
}; | |
/** | |
* Logout | |
*/ | |
CurrentUser.prototype.logout = function() { | |
var self = this; | |
$http.post('/logout').then(function () { | |
self.reset(); | |
$location.path('/') | |
}); | |
}; | |
/** | |
* Session | |
*/ | |
CurrentUser.prototype.session = function() { | |
var self = this; | |
return $http.get('/session').then(function (response) { | |
if (response.data.authenticated) { | |
self.isAuthenticated(response.data.user); | |
} | |
}); | |
}; | |
/** | |
* The service is an instance of CurrentUser | |
*/ | |
return new CurrentUser(); | |
}) |
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('LoginCtrl', ['$scope', '$location', 'CurrentUser', | |
($scope, $location, user) -> | |
$scope.showLogin = false | |
$scope.$watch user.requiresAuthentication, (value) -> | |
$scope.showLogin = value | |
$scope.reset = -> | |
$scope.credentials = {} | |
$scope.message = "Enter your credentials" | |
$scope.messageType = "alert-warning" | |
$scope.sendLogin = -> | |
success = -> | |
$scope.reset() | |
error = (response) -> | |
$scope.message = response.data.error | |
$scope.messageType = "alert-error" | |
user.login($scope.credentials).then success, error | |
$scope.cancelLogin = -> | |
$scope.reset() | |
user.constructor.requiresAuthentication = false | |
$location.path '/' | |
# Initialize the form | |
$scope.reset() | |
]) |
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').directive('loginFocus', ['$timeout', 'CurrentUser', ($timeout, user) -> | |
(scope, element, attrs) -> | |
scope.$watch user.requiresAuthentication, (n,o) -> | |
focus = -> element.focus() | |
if n is true and o is false | |
$timeout focus, 300 | |
]) |
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('UserMenuCtrl', function ($scope, CurrentUser) { | |
$scope.currentUser = CurrentUser; | |
$scope.logout = function () { | |
CurrentUser.logout(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment