github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/static/app/services/auth.service.js (about)

     1  (function(){
     2      'use strict';
     3  
     4      angular
     5          .module('shipyard.services')
     6          .factory('AuthService', AuthService);
     7  
     8      AuthService.$inject = ['$http', '$state'];
     9      function AuthService($http, $state) {
    10          return {
    11              login: function(credentials) {
    12                  return $http
    13                      .post('/auth/login', credentials)
    14                      .success(function(data, status, headers, config) {
    15                          localStorage.setItem('X-Access-Token', credentials.username + ':' + data.auth_token);
    16                      })
    17                      .error(function(data, status, headers, config) {
    18                          localStorage.removeItem('X-Access-Token');
    19                      })
    20                      .then(function(response) {
    21                          return response.data;
    22                      });
    23              },
    24              logout: function() {
    25                  localStorage.removeItem('X-Access-Token');
    26              },
    27              isLoggedIn: function() {
    28                  return localStorage.getItem('X-Access-Token') != null;
    29              },
    30              getUsername: function() {
    31                  var token = localStorage.getItem('X-Access-Token');
    32                  if(token == null) {
    33                      return "";
    34                  }
    35                  return token.split(':')[0];
    36              }
    37          };
    38      }
    39  })();
    40