Skip to content

Instantly share code, notes, and snippets.

@JosephScript
Created September 22, 2015 14:33
Show Gist options
  • Save JosephScript/de76fc12993b14ea2a19 to your computer and use it in GitHub Desktop.
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.
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