Advertisement
Guest User

Untitled

a guest
May 7th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. app.controller('AuthController', function($auth, $state,$http,$rootScope, $scope) {
  2.  
  3. $scope.email='';
  4. $scope.password='';
  5. $scope.newUser={};
  6. $scope.loginError=false;
  7. $scope.loginErrorText='';
  8.  
  9. $scope.login = function() {
  10.  
  11. var credentials = {
  12. email: $scope.email,
  13. password: $scope.password
  14. }
  15.  
  16. $auth.login(credentials).then(function() {
  17.  
  18. return $http.get('api/authenticate/user');
  19.  
  20. }, function(error) {
  21. $scope.loginError = true;
  22. $scope.loginErrorText = error.data.error;
  23.  
  24. }).then(function(response) {
  25.  
  26. // var user = JSON.stringify(response.data.user);
  27.  
  28. // localStorage.setItem('user', user);
  29. $rootScope.authenticated = true;
  30. $rootScope.currentUser = response.data.user;
  31. $scope.loginError = false;
  32. $scope.loginErrorText = '';
  33.  
  34. $state.go('todo');
  35. });
  36. }
  37.  
  38. $scope.register = function () {
  39.  
  40. $http.post('/api/register',$scope.newUser)
  41. .success(function(data){
  42. $scope.email=$scope.newUser.email;
  43. $scope.password=$scope.newUser.password;
  44. $scope.login();
  45. })
  46.  
  47. };
  48.  
  49.  
  50. });
  51.  
  52. angular.module('apiFromApp')
  53. .factory('AuthServices', AuthServices);
  54.  
  55. AuthServices.inject = ['$http','$q', '$sanitize', '$auth', '$rootScope', '$state', 'toastr', 'CONFIG'];
  56.  
  57. function AuthServices($http, $q, $sanitize, $auth, $rootScope, $state, toastr, CONFIG) {
  58. var service = {};
  59.  
  60. // Service logic
  61. var sanitizeCredentials = function (credentials) {
  62. return {
  63. email: $sanitize(credentials.email),
  64. password: $sanitize(credentials.password)
  65. };
  66. };
  67.  
  68. service.login = function (data) {
  69. data = sanitizeCredentials(data);
  70. $auth.login(data).then(function() {
  71.  
  72. return $http.get(CONFIG.APIURL + 'api/authenticate/user').then(function(response) {
  73.  
  74.  
  75. var user = JSON.stringify(response.data.user);
  76.  
  77.  
  78. localStorage.setItem('user', user);
  79.  
  80.  
  81. $rootScope.authenticated = true;
  82.  
  83.  
  84. $rootScope.currentUser = response.data.user;
  85.  
  86.  
  87. $state.go('main');
  88. });
  89.  
  90. // Handle errors
  91. }, function(error) {
  92. toastr.error(error.data.error, error.statusText);
  93. });
  94.  
  95.  
  96. }
  97.  
  98. var sanitizeDatesRegister = function (newUser) {
  99. return {
  100. username: $sanitize(newUser.username),
  101. email: $sanitize(newUser.email),
  102. password: $sanitize(newUser.password)
  103. };
  104. };
  105.  
  106. /**
  107. * [register description]
  108. * @param {[type]} data [description]
  109. * @return {[type]} [description]
  110. */
  111. service.register = function (data) {
  112. var userNew = sanitizeDatesRegister(data);
  113. $http.post(CONFIG.APIURL + 'api/authenticate/sinup',userNew)
  114. .success(function(data){
  115.  
  116. }).error(function (err) {
  117. console.log(err);
  118. })
  119. }
  120.  
  121.  
  122.  
  123. return service;
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement