Guest User

Untitled

a guest
Jun 19th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Login: thiago.pires@gmail.com
  2. // Password: thiago123
  3.  
  4. // Primeiro você deve fazer o login para pegar o token de autenticação:
  5. //   url: "https://carsystemapp.herokuapp.com/users/sign_in"
  6. //   type: "GET"
  7. //   dataType: "JSON"
  8. //   data: {
  9. //      user: {
  10. //          email: email,
  11. //          password: password
  12. //      }
  13. //  }
  14.  
  15. // Na requicisão acima, irá retornar apenas o parâmetro authentication_token
  16.  
  17.  
  18. // É necessário que em toda a requisição  abaixo, esteja presente no request header os 2 parâmetros "X-USER-EMAIL" e "X-USER-TOKEN", que serve como autenticação segura.
  19.  
  20.  
  21.  
  22. // Listar todos os carros
  23. //   url: "/cars"
  24. //   type: "GET"
  25. //   dataType: "JSON"
  26. //   data: {}
  27.  
  28. // Cadastrar um carro
  29. //   url: "/cars"
  30. //   type: "POST"
  31. //   dataType: "JSON"
  32. //   data:
  33. //     car: { name: "Onix", brand: "Chevrolet", year: 2016 }
  34.  
  35. // Ver informações de um carro
  36. //   url: "/cars/<id>"
  37. //   type: "GET"
  38. //   dataType: "JSON"
  39.  
  40. // Atualizar informações de um carro
  41. //   url: "/cars/<id>"
  42. //   type: "PUT"
  43. //   dataType: "JSON"
  44. //   data:
  45. //     car: { name: "New Onix", brand: "Chevrolet", year: 2016 }
  46.  
  47. // Deletar um carro
  48. //   url: "/cars/<id>"
  49. //   type: "DELETE"
  50. //   dataType: "JSON"
  51. //   data: {}
  52.  
  53.  
  54.  
  55. // Exemplo em JavaScript, pode ser testado através do web console:
  56.  
  57.  
  58.  
  59. var token = null;
  60. var email = "thiago.pires@gmail.com"
  61. var passw = "thiago123"
  62.  
  63.  
  64.  
  65. var insert_cars = function(_email, _passw){
  66.  
  67.     var new_cars = [
  68.         { name: "Civic 2.0", brand: "Honda",     year: 2016 },
  69.         { name: "Corola",    brand: "Toyota",    year: 2016 },
  70.         { name: "Onix",      brand: "Chevrolet", year: 2016 }
  71.     ]
  72.     new_cars.forEach(function(i){
  73.         $.ajax({
  74.             url: "https://carsystemapp.herokuapp.com/cars",
  75.             type: "POST",
  76.             dataType: "JSON",
  77.             data: {
  78.                 car: i
  79.             },
  80.             beforeSend: function(xhr){
  81.                 // Setando email e token no request header para autenticação
  82.                 xhr.setRequestHeader('X-USER-EMAIL', _email);
  83.                 xhr.setRequestHeader('X-USER-TOKEN', _passw);
  84.             },
  85.             success: function(data) {
  86.                 console.log("Data (insert_cars): ", data);
  87.             },
  88.             error: function(err) {
  89.                 console.error("Error (insert_cars): ", err);
  90.             }
  91.         });
  92.     });
  93. };
  94.  
  95. var get_data = function(_email, _passw){
  96.     // inserção de carros para teste
  97.     insert_cars(_email, _passw);
  98.     $.ajax({
  99.         url: "https://carsystemapp.herokuapp.com/cars",
  100.         type: "GET",
  101.         dataType: "JSON",
  102.         beforeSend: function(xhr){
  103.             // Setando email e token no request header para autenticação
  104.             xhr.setRequestHeader('X-USER-EMAIL', _email);
  105.             xhr.setRequestHeader('X-USER-TOKEN', _passw);
  106.         },
  107.         success: function(data) {
  108.             console.log("Data (get_data): ", data);
  109.         },
  110.         error: function(err) {
  111.             console.error("Error (get_data): ", err);
  112.         }
  113.     });
  114. };
  115.  
  116. // Login
  117. $.post({
  118.     url: "https://carsystemapp.herokuapp.com/users/sign_in",
  119.     dataType: "JSON",
  120.     data: {
  121.         user: {
  122.             email: email,
  123.             password: passw
  124.         }
  125.     },
  126.     success: function(dt){
  127.         console.log("Data (log_in): ", dt);
  128.         // Recebendo token e guardando em variável
  129.         token = dt.authentication_token;
  130.         // Chamando função passando como parâmetro email e token de validação
  131.         get_data(email, token);
  132.     },
  133.     error: function(err){
  134.         console.error("Error (log_in): ", err);
  135.     }
  136. })
Add Comment
Please, Sign In to add comment