Created
September 22, 2015 14:33
-
-
Save JosephScript/de76fc12993b14ea2a19 to your computer and use it in GitHub Desktop.
A login controller for AngularJS. This relies on authService.js for token storage, and retrieval.
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
app.controller('LoginCtrl', ['$scope', '$http', '$location', 'authService', | |
function ($scope, $http, $location, authService) { | |
$scope.submit = function () { | |
$http.post('/authenticate', this.form) | |
.success(function (data, status, headers, config) { | |
// save json web token in session storage | |
authService.saveToken(data.token); | |
// passes the user to scope for display | |
$scope.user = authService.getUser(); | |
// redirect to home page | |
$location.path('/home'); | |
}).error(function () { | |
// wipe out the stored token | |
authService.logout(); | |
$scope.form.password = ''; | |
}) | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment