UNPKG

zettapi_client

Version:

Client side CRUD operations in angular to use with zettapi_server rest api to get started quickly in any CMS project

194 lines (175 loc) 8.01 kB
app.factory('$auth', function ($http, $crypto, blockUI, $location, anonymousPages, $rootScope, Idle, zapi, $translate) { var service = { currentUser: null, login: function (username, password, callback) { $translate('api.services.auth.loginLoad', function (text) { blockUI.start(text); }, function (translationId) { blockUI.start(translationId); }); return $http.post('/api/session/login/', { username: username, password: $crypto.md5(password) }).then(function (response) { if (zapi.idle) Idle.watch(); service.currentUser = response.data.user; callback(); }).catch(function (response) { $translate(['api.services.auth.loginErrorTitle', 'api.services.auth.loginErrorContent']).then(function (translations) { swal(translations['api.services.auth.loginErrorTitle'], translations['api.services.auth.loginErrorContent'], "error"); //swal(loginErrorTitle, response.data, "danger"); }, function (translationsId) { swal(translationsId.api_service_auth_loginerrortitle, translationsId.api_service_auth_loginerrorcontent, "error"); }); callback(true); }).finally(function () { blockUI.stop(); }); }, logout: function () { $http.get('/api/session/logout').then(function (response) { if (zapi.idle) Idle.unwatch(); service.currentUser = null; $rootScope.login = {}; $location.path('/'); }); }, requestCurrentUser: function (callback) { if (service.isAuthenticated()) return callback(null, service.currentUser); $http.get('/api/session/currentuser').then(function (response) { service.currentUser = response.data.user; callback(null, service.currentUser); }).catch(function (response) { service.currentUser = null; callback(response); }); }, isAuthenticated: function () { return !!service.currentUser; }, activateAccount: function (rawCode, callback) { if (!rawCode) return callback(true); var codes = rawCode.split('&'); if (codes.length !== 2) return callback(true); $translate('api.services.auth.activateAccountLoad', function (text) { blockUI.start(text); }, function (translationId) { blockUI.start(translationId); }); $http.post('/api/user/activate/', { email: codes[0], code: codes[1] }).then(function (response) { callback(null, response.data); }).catch(function (response) { $translate(['api.services.auth.activateAccountErrorTitle', 'api.services.auth.activateAccountErrorContent']).then(function (translations) { swal({ title: translations['api.services.auth.activateAccountErrorTitle'], text: translations['api.services.auth.activateAccountErrorContent'], type: "info", confirmButtonText: "OK" }); }, function (translationsId) { swal({ title: translationsId.api_service_auth_activateaccounterrortitle, text: translationsId.api_service_auth_activateaccounterrorcontent, type: "info", confirmButtonText: "OK" }); }); callback(true); }).finally(function () { blockUI.stop(); }); }, resetPassword: function (email, username, callback) { $translate('api.services.auth.resetPasswordLoad', function (text) { blockUI.start(text); }, function (translationId) { blockUI.start(translationId); }); return $http.post('/api/user/resetpassword/', { email: email, username: username }).then(function (response) { $translate(['api.services.auth.resetPasswordSuccessTitle', 'api.services.auth.resetPasswordSuccessContent']).then(function (translations) { swal(translations['api.services.auth.resetPasswordSuccessTitle'], translations['api.services.auth.resetPasswordSuccessContent'], "success"); }, function (translationsId) { swal(translationsId.api_service_auth_resetpasswordsuccesstitle, translationsId.api_service_auth_resetpasswordsuccesscontent, "success"); }); callback(); }).catch(function (response) { $translate(['api.services.auth.resetPasswordErrorTitle', 'api.services.auth.resetPasswordErrorContent']).then(function (translations) { swal(translations['api.services.auth.resetPasswordErrorTitle'], translations['api.services.auth.resetPasswordErrorContent'], "error"); }, function (translationsId) { swal(translationsId.api_service_auth_resetpassworderrortitle, translationsId.api_service_auth_resetpassworderrorcontent, "error"); }); callback(true); }).finally(function () { blockUI.stop(); }); }, changePassword: function (user, newPassword1, newPassword2, callback) { if (!newPassword1) return; if (newPassword1 !== newPassword2) { $translate(['api.services.auth.changepasswordWrongPasswordTitle', 'api.services.auth.changepasswordWrongPasswordContent']).then(function (translations) { swal(translations['api.services.auth.changepasswordWrongPasswordTitle'], translations['api.services.auth.changepasswordWrongPasswordContent'], "error"); }, function (translationsId) { swal(translationsId.api_service_auth_changepasswordwrongpasswordtitle, translationsId.api_service_auth_changepasswordwrongpasswordcontent, "error"); }); return; } user.newPassword = $crypto.md5(newPassword1); $translate('api.services.auth.changepasswordLoad', function (text) { blockUI.start(text); }, function (translationId) { blockUI.start(translationId); }); $http.post('/api/user/changepassword/', user).then(function (response) { $translate(['api.services.auth.changepasswordSuccessTitle', 'api.services.auth.changepasswordSuccessContent', 'api.services.auth.changepasswordSuccessBtnOk']).then(function (translations) { swal({ title: translations['api.services.auth.changepasswordSuccessTitle'], text: translations['api.services.auth.changepasswordSuccessContent'], type: "info", confirmButtonText: translations['api.services.auth.changepasswordSuccessBtnOk'] }); }, function (translationsId) { swal({ title: translationsId.api_service_auth_changepasswordsuccesstitle, text: translationsId.api_service_auth_changepasswordsuccesscontent, type: "info", confirmButtonText: translationsId.api_service_auth_changepasswordsuccessbtnok }); }); callback(); }).catch(function (response) { $translate(['api.services.auth.changepasswordErrorTitle', 'api.services.auth.changepasswordErrorContent']).then(function (translations) { swal(translations['api.services.auth.changepasswordErrorTitle'], translations['api.services.auth.changepasswordErrorContent'], "error"); }, function (translationsId) { swal(translationsId.api_service_auth_changepassworderrortitle, translationsId.api_service_auth_changepassworderrorcontent, "error"); }); callback(true); }).finally(function () { blockUI.stop(); }); }, isPageAnonymous: function () { var urlPaths = $location.path().split('/'); return anonymousPages.some(function (anonymousPage) { return anonymousPage === urlPaths[1]; }); }, isAnonymous: function () { return !$auth.isLoggedIn(); }, isLoggedIn: function () { if (!service.currentUser) return false; return !!service.currentUser._id; }, isAdmin: function () { if (service.isLoggedIn()) return service.currentUser.role.admin; return false; } }; return service; });