Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- 'use strict';
- var app = angular.module('trainingPortal');
- app.factory('authService', ['$http', 'ngAuthSettings', '$q', 'localStorageService', function ($http, ngAuthSettings, $q, localStorageService) {
- var serviceBase = ngAuthSettings.apiServiceBaseUri;
- var authServiceFactory = {};
- var _authentication = {
- isAuth: false,
- userName: "",
- userRole: "",
- userApplicant: false,
- userTrainer: false,
- userTrainingOrganizer:false
- };
- var _saveRegistration = function (registration) {
- _logOut();
- return $http.post(serviceBase + 'api/account/register', registration).then(function (response) {
- return response;
- });
- };
- var _login = function (loginData) {
- var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
- var deferred = $q.defer();
- $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function (response) {
- localStorageService.set('authorizationData', { token: response.access_token, userName: loginData.userName, userRole: response.userRole });
- _authentication.isAuth = true;
- _authentication.userName = loginData.userName;
- _authentication.userRole = response.userRole;
- if (_authentication.userRole.indexOf("Trainer") != -1)
- _authentication.userTrainer = true;
- if (_authentication.userRole.indexOf("Training Organizer") != -1)
- _authentication.userTrainingOrganizer = true;
- if (!_authentication.userTrainer && !_authentication.userTrainingOrganizer)
- _authentication.userApplicant = true;
- deferred.resolve(response);
- }).error(function (err, status) {
- _logOut();
- deferred.reject(err);
- });
- return deferred.promise;
- };
- var _logOut = function () {
- localStorageService.remove('authorizationData');
- _authentication.isAuth = false;
- _authentication.userName = "";
- _authentication.userRole = "";
- _authentication.userApplicant = false;
- _authentication.userTrainer = false;
- _authentication.userTrainingOrganizer = false;
- };
- var _fillAuthData = function () {
- var authData = localStorageService.get('authorizationData');
- if (authData) {
- _authentication.isAuth = true;
- _authentication.userName = authData.userName;
- _authentication.userRole = authData.userRole;
- if (_authentication.userRole.indexOf("Trainer") != -1)
- _authentication.userTrainer = true;
- if (_authentication.userRole.indexOf("Training Organizer") != -1)
- _authentication.userTrainingOrganizer = true;
- if (!_authentication.userTrainer && !_authentication.userTrainingOrganizer)
- _authentication.userApplicant = true;
- }
- }
- authServiceFactory.saveRegistration = _saveRegistration;
- authServiceFactory.login = _login;
- authServiceFactory.logOut = _logOut;
- authServiceFactory.fillAuthData = _fillAuthData;
- authServiceFactory.authentication = _authentication;
- return authServiceFactory;
- }]);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement