Advertisement
Guest User

Untitled

a guest
Sep 5th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. angular.module("login")
  2. .controller('loginController',function($scope,$http,LocalStore,jwtHelper,$location,authFactory){
  3.  
  4. $scope.jwt = LocalStore.getJWT();
  5. $scope.error;
  6.  
  7. if(authFactory.isLogIn()){
  8. $location.path("/home");
  9. }
  10.  
  11. $scope.logIn = function(user){
  12. authFactory.logIn(user);
  13. $scope.error = authFactory.getError();
  14.  
  15. };
  16.  
  17. $scope.logOut = function(){
  18. console.log($scope.error);
  19. $scope.error = authFactory.getError();
  20. };
  21.  
  22. });
  23.  
  24. factory("authFactory", function($http, LocalStore, $location, jwtHelper){
  25.  
  26. var authFactory = {};
  27. authFactory.errores="-";
  28.  
  29. authFactory.logIn = function(user){
  30. $http({
  31. method: 'POST',
  32. skipAuthorization: true,
  33. url: 'login.php',
  34. data: { op:'login', username: user.username, password: user.password } ,
  35. headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  36. }).then(function successCallback(response) {
  37. console.log("Entrando al success");
  38. if('jwt' in response.data){
  39. console.log("Login Exitoso");
  40. LocalStore.saveJWT(response.data.jwt);
  41. }else if('error' in response.data){
  42. console.log("Login Erroneo");
  43. authFactory.errores = response.data.error;
  44. console.log("Imprimiendo authFactory: " + authFactory.errores);
  45. };
  46. if(authFactory.isLogIn){
  47. $location.path("/home");
  48. }else{
  49. $location.path("/login");
  50. }
  51. }, function errorCallback(response) {
  52. console.log("Login Fallido jajajajaj");
  53. });
  54.  
  55.  
  56. };
  57.  
  58. authFactory.getError=function(){
  59. console.log("Obteniendo AuthFactory en get Error: "+ authFactory.errores);
  60. return authFactory.errores;
  61. };
  62.  
  63. <div class="row">
  64. <div class="col-sm-4">
  65. <div class="alert alert-success" role="alert" ng-show="error">
  66. <strong>Well done!</strong> {{error}}.
  67. </div>
  68. </div>
  69. <div class="col-sm-4" align="center">
  70. <input type="text" ng-model="user.username" placeholder="Username" class="form-control form-group">
  71. <input type="password" ng-model="user.password" placeholder="Password" class="form-control form-group">
  72. <button class="btn btn-primary btn-large form-group" ng-click="logIn(user)">Log In</button>
  73.  
  74. </div>
  75. <div class="col-sm-4"></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement