Advertisement
Marcus_Vinicius

Untitled

Jun 8th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('login').
  2.     component('login',{
  3.         templateUrl: '/api/templates/login.html',
  4.         controller: function(User,$cookies, $http, $location, $routeParams, $scope) {
  5.             // var loginUrl = '/api/auth/token/'
  6.             var loginUrl = '/api/user/login/'
  7.             // $http.post(
  8.             $scope.user = {}
  9.             var tokenExists = $cookies.get("token")
  10.             if (tokenExists){
  11.                 $scope.loggedIn = true;
  12.                 $cookies.remove("token")
  13.                 $scope.user = {
  14.                     username: $cookies.get("username")
  15.                 }
  16.                 //necessario dar o refresh na pagina pois o cookie fica ainda armazenado mesmo apos logoff
  17.                 window.location.reload()
  18.             }
  19.             $scope.loginError = {}
  20.             $scope.$watch(function(){
  21.                 if($scope.user.password){
  22.                     $scope.loginError.password = ""
  23.                 }
  24.                 if ($scope.user.username){
  25.                         $scope.loginError.username = ""
  26.                 }
  27.  
  28.             })
  29.             $scope.doLogin = function(user){
  30.                 //para não precisar acessar o servidor
  31.                 if (!user.username){
  32.                     $scope.loginError.username = ["Field required"]
  33.                 }
  34.                 if (!user.password){
  35.                     $scope.loginError.password = ["Field required"]
  36.                 }
  37.                 if (user.username && user.password){
  38.                     var reqConfig = {
  39.                     method:"POST",
  40.                     url: loginUrl,
  41.                     data:{
  42.                         username: user.username,
  43.                         password: user.password
  44.                     },
  45.                     headers: {},
  46.                     }
  47.                     // var requestAction = $http.post(loginUrl,user)
  48.                     var requestAction = $http(reqConfig)
  49.                     requestAction.success(function(r_data,r_status,r_headers,r_config){
  50.                         //inserindo o cookie que veio no response dentro do $cookies usando put
  51.                         // console.log(r_data)
  52.                         $cookies.put("id",r_data.id)
  53.                         $cookies.put("token",r_data.token)
  54.                         $cookies.put("username",r_data.username)
  55.                         $cookies.put("aplicativos",r_data.aplicativos)
  56.                         $cookies.put("admin",r_data.admin)
  57.                         // window.location.reload()
  58.                         //redireciona pasra pagina após login
  59.                         $location.path("/dashboard")
  60.                          // window.location.reload()
  61.  
  62.  
  63.                     })
  64.                     requestAction.error(function(e_data,e_status,e_headers,e_config){
  65.                         console.log(e_data)//token
  66.                         $scope.loginError = e_data
  67.                     })
  68.  
  69.                 }
  70.  
  71.  
  72.             }
  73.         }
  74.  
  75. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement