Skip to content

Instantly share code, notes, and snippets.

@nathanbarrett
Created April 25, 2016 18:59
Show Gist options
  • Select an option

  • Save nathanbarrett/6e09ace8b8b5923155701548de9af959 to your computer and use it in GitHub Desktop.

Select an option

Save nathanbarrett/6e09ace8b8b5923155701548de9af959 to your computer and use it in GitHub Desktop.
Basic Auth Service for Angular
/*
* Angular Service for authentication
*
* @params none
*
* @returns none
*/
AuthService.$inject = ['$location', 'ExceptionService'];
function AuthService($location, ExceptionService) {
var self = this;
/**** Private Variables ****/
var user = null;
/**** Initializations ****/
authCheck();
/**** Public Functions ****/
/*
* Logs in the user
*
* @params email (string|required), pwd (string|required), remember (bool|required), redirect (string|optional)
*
* @returns $http promise
*/
self.login = function (email, pwd, remember, redirect) {
return $http.post('/login', {
email: email,
password: pwd,
remember: remember
})
.then(function (response) {
if(response.data.success){
if(redirect){
$location.url(redirect);
}
user = response.data.user;
}
}, function (error) {
ExceptionService.errorResponse(error);
})
};
self.logout = function () {
$location.url('/logout');
};
self.register = function () {
//TODO fill out
};
self.inviteNewUser = function () {
//TODO fill out
};
self.getUser = function () {
return user;
};
self.isLoggedIn = function () {
return user !== null;
};
/**** Private Functions ****/
function authCheck() {
return $http.post('/authCheck')
.then(function (response) {
user = response.data.user;
}, function (error) {
ExceptionService.errorResponse(error);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment