Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- 'use strict';
- class loginService {
- /*@ngInject*/
- constructor($resource, $q, BASE_URL, REST_PATH, LOGIN_PATH, LOGOUT_PATH, IS_LOGGED_IN_PATH, $httpParamSerializerJQLike, messages) {
- this.BASE_URL = BASE_URL;
- this.REST_PATH = REST_PATH;
- this.$q = $q;
- this.$resource = $resource;
- this.LOGIN_PATH = LOGIN_PATH;
- this.LOGOUT_PATH = LOGOUT_PATH;
- this.IS_LOGGED_IN_PATH = IS_LOGGED_IN_PATH;
- this.$httpParamSerializerJQLike = $httpParamSerializerJQLike;
- this.messages = messages;
- this.state = {};
- this.userStatus = { authorized: false };
- }
- initLoginResource() {
- const { $resource, BASE_URL, REST_PATH, LOGIN_PATH, $httpParamSerializerJQLike } = this;
- return $resource(BASE_URL + REST_PATH + LOGIN_PATH, null, {
- doLogin: {
- method: 'POST',
- params: {},
- transformRequest: (data) =>{
- return $httpParamSerializerJQLike(data);
- },
- transformResponse: (data, headersGetter, status)=> {
- return {data: data, status: status};
- },
- headers: {'Content-Type': 'application/x-www-form-urlencoded'}
- }
- });
- }
- initIsLoggedInResource() {
- const { $resource, BASE_URL, REST_PATH , IS_LOGGED_IN_PATH } = this;
- return $resource(BASE_URL + REST_PATH + IS_LOGGED_IN_PATH, null, {
- isLoggedIn: {
- method: 'GET'
- }
- });
- }
- initLogoutResource() {
- const { $resource, BASE_URL, REST_PATH , LOGOUT_PATH } = this;
- return $resource(BASE_URL + REST_PATH + LOGOUT_PATH, null, {
- doLogout: {
- method: 'POST'
- }
- });
- }
- isUserAuthorized() {
- return this.$q((resolve, reject) => {
- this.initIsLoggedInResource().isLoggedIn().$promise
- .then( (data)=> {
- this.userStatus.authorized = true;
- resolve(true);
- }, (data) => {
- this.userStatus.authorized = false;
- reject(false);
- });
- });
- }
- performLogin(name, password) {
- return this.$q( (resolve, reject) => {
- this.initLoginResource().doLogin({}, {
- username: name,
- password: password
- }).$promise.then( (data) => {
- this.userStatus.authorized = true;
- return resolve(data);
- }, (data) => {
- this.userStatus.authorized = false;
- return reject(data.data);
- });
- });
- }
- logOut() {
- return this.$q( (resolve, reject) => {
- this.initLogoutResource().doLogout().$promise
- .then( (data) => {
- this.userStatus.authorized = false;
- return resolve(data);
- }, (data) => {
- this.userStatus.authorized = false;
- return resolve(data); // resolve request anyway
- });
- });
- }
- }
- angular.module('app.core')
- .factory('loginService',
- ['$resource', '$q', 'BASE_URL', 'REST_PATH', 'LOGIN_PATH', 'LOGOUT_PATH', 'IS_LOGGED_IN_PATH', '$httpParamSerializerJQLike', 'messages',
- ($resource, $q, BASE_URL, REST_PATH, LOGIN_PATH, LOGOUT_PATH, IS_LOGGED_IN_PATH, $httpParamSerializerJQLike, messages) =>
- new loginService($resource, $q, BASE_URL, REST_PATH, LOGIN_PATH, LOGOUT_PATH, IS_LOGGED_IN_PATH, $httpParamSerializerJQLike, messages)
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement