Skip to content

Instantly share code, notes, and snippets.

@rrgarciach
Created August 12, 2016 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rrgarciach/142a5c6dbe367cf58296cbba00f8eaba to your computer and use it in GitHub Desktop.
Save rrgarciach/142a5c6dbe367cf58296cbba00f8eaba to your computer and use it in GitHub Desktop.
'use strict';
angular.module('components.auth')
.provider('AuthSystemRoles', function () {
var roles = {
system : 'System User',
admin : 'Admin User',
account : 'Account Owner'
};
var systemUserRoles = [
roles.admin,
roles.system
];
this.$get = function () {
return {
getSystemRoles: function() {
//@TODO: Add post to get roles from database
return roles;
},
getSystemUsersRoles: function() {
return systemUserRoles;
}
};
};
})
.factory('Auth', function Auth($location, $rootScope, $http, User, $cookies, $q, AuthSystemRoles, Redirections) {
var currentUser = {};
if($cookies.get('token')) {
currentUser = User.get({id:'me'});
}
return {
/**
* Authenticate user and save token
*
* @param {Object} user - login info
* @param {Function} callback - optional
* @return {Promise}
*/
login: function(user, callback) {
var self = this;
var cb = callback || angular.noop;
var deferred = $q.defer();
$http.post('/auth/local', {
email: user.email,
password: user.password
}).
success(function(data) {
if( data.url ){
Redirections.replace(data.url);
} else {
self.loadUser(data.token);
deferred.resolve(data);
return cb();
}
}).
error(function(err) {
this.logout();
deferred.reject(err);
return cb(err);
}.bind(this));
return deferred.promise;
},
loadUser: function(token){
$cookies.putObject('token', token);
currentUser = User.get({id:'me'});
return currentUser.$promise;
},
/**
* Delete access token and user info
*
* @param {Function}
*/
logout: function() {
$cookies.remove('token');
currentUser = {};
},
/**
* Create a new user
*
* @param {Object} user - user info
* @param {Function} callback - optional
* @return {Promise}
*/
createUser: function(user, callback) {
var cb = callback || angular.noop;
return User.save(user,
function(data) {
$cookies.put('token', data.token);
currentUser = User.get({id:'me'});
return cb(user);
},
function(err) {
this.logout();
return cb(err);
}.bind(this)).$promise;
},
/**
* Change password
*
* @param {String} oldPassword
* @param {String} newPassword
* @param {Function} callback - optional
* @return {Promise}
*/
changePassword: function(oldPassword, newPassword, callback) {
var cb = callback || angular.noop;
return User.changePassword({ id: currentUser._id }, {
oldPassword: oldPassword,
newPassword: newPassword
}, function(user) {
return cb(user);
}, function(err) {
return cb(err);
}).$promise;
},
/**
* Gets all available info on authenticated user
*
* @return {Object} user
*/
getCurrentUser: function() {
return currentUser;
},
/**
* Check if a user is logged in
*
* @return {Boolean}
*/
isLoggedIn: function() {
return currentUser.hasOwnProperty('Role');
},
/**
* Waits for currentUser to resolve before checking if user is logged in
*/
isLoggedInAsync: function(cb) {
if(currentUser.hasOwnProperty('$promise')) {
currentUser.$promise.then(function() {
cb(true);
}).catch(function() {
cb(false);
});
} else if(currentUser.hasOwnProperty('role')) {
cb(true);
} else {
cb(false);
}
},
/**
* Check if a user is an admin
*
* @return {Boolean}
*/
isAdmin: function() {
return currentUser.Role.name === AuthSystemRoles.getSystemRoles().admin;
},
isSystemUser: function() {
return AuthSystemRoles.getSystemUsersRoles().indexOf(currentUser.Role.name) > -1;
},
/**
* Get auth token
*/
getToken: function() {
return $cookies.get('token');
},
/**
* Get current user roles
*
* @param function {callback} function for success to retrieve currentUser
*/
getUserRole: function(cb) {
var currentUser = this.getCurrentUser();
if (currentUser.$promise) {
currentUser.$promise.then(function () {
cb(currentUser.Role);
}
);
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment