Advertisement
Guest User

Untitled

a guest
Oct 9th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. 'use strict';
  2.  
  3. angular.
  4. module('core.auth').
  5. factory('AuthService', [ '$http', '$rootScope', 'ConfigService', '$cookies',
  6. function AuthService($http, $rootScope, ConfigService, $cookies) {
  7. const service = {};
  8.  
  9. service.Login = Login;
  10. service.SetCredentials = SetCredentials;
  11. service.ClearCredentials = ClearCredentials;
  12.  
  13. return service;
  14.  
  15. function Login(username, password, callback) {
  16. $http.post(ConfigService.LOGIN_API, { username: username, password: password })
  17. .success(function (response) {
  18. callback(response);
  19. });
  20. }
  21.  
  22. function SetCredentials(username) {
  23. $rootScope.globals = {
  24. currentUser: {
  25. username: username
  26. }
  27. };
  28.  
  29. $cookies.put('globals', $rootScope.globals);
  30. }
  31.  
  32. function ClearCredentials() {
  33. $rootScope.globals = {};
  34. $cookies.remove('globals');
  35. }
  36. }
  37. ]);
  38.  
  39. 'use strict';
  40.  
  41. angular.
  42. module('login').
  43. component('login', {
  44. templateUrl: 'dist/components/login/login.template.html',
  45. controller: ['$location', 'AuthService', '$log',
  46. function LoginController($location, AuthService, $log) {
  47.  
  48. (function initController() {
  49. // reset login status
  50. AuthService.ClearCredentials();
  51. }());
  52.  
  53. this.login = () => {
  54. AuthService.Login(this.username, this.password, (response) => {
  55. if (response.success) {
  56. $log.log(response);
  57. $log.log('Login successful', true);
  58. AuthService.SetCredentials(this.username);
  59. $location.path('#!/products');
  60. } else {
  61. $log.log(response)
  62. }
  63. })
  64. }
  65.  
  66. }
  67. ],
  68.  
  69. controllerAs: 'vm'
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement