//Auth.js
angular.module('authService', [])
  .factory('Auth', function($http, AuthToken, $rootScope) {
    var authFactory = {};
    authFactory.login = function(username, password) {
      return $http.post('api/v1/authenticate', { // get the token from back-end
        username: username,
        password: password
      }).success(function(data) {
        if (data.token)
          AuthToken.setToken(data.token); // store the token using AuthToken service
        return data;
      }).error(function(data, status) {
        alert('Something went wrong. Please try again later');
        return data;
      });
    };
    authFactory.logout = function() {
      AuthToken.setToken(); // if logout set the token is empty
    };
    authFactory.isLoggedIn = function() {
      if (AuthToken.getToken())
        return true;
      else
        return false;
    };
    return authFactory;
  });

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js