Guest User

Untitled

a guest
Aug 28th, 2018
1,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. (function () {
  2.  
  3. function getJwt(user, pass, success, error, complete) {
  4. console.log('getJwt ', user);
  5. $.ajax({
  6. url: '/api/login',
  7. type: 'POST',
  8. contentType: "application/json",
  9. dataType: "json",
  10. data: JSON.stringify({
  11. username: user,
  12. password: pass
  13. }),
  14. success: function (data) {
  15. success && success(data.data.token);
  16. },
  17. error: function (jqXhr, err, msg) {
  18. error && error(JSON.parse(jqXhr.responseText).error_description);
  19.  
  20. },
  21. complete: complete
  22. });
  23. }
  24.  
  25. function setNativeValue(element, value) {
  26. const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
  27. const prototype = Object.getPrototypeOf(element);
  28. const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
  29.  
  30. if (valueSetter && valueSetter !== prototypeValueSetter) {
  31. prototypeValueSetter.call(element, value);
  32. } else {
  33. valueSetter.call(element, value);
  34. }
  35. }
  36.  
  37. function setJwt(key) {
  38. console.log("setJwt ", key);
  39. var inputAuth = $('.auth-container :input[type=text]')[0];
  40. //inputAuth.off().val("Bearer " + key).change();
  41. //inputAuth.attr('value', 'Bearer ' + key);
  42. setNativeValue(inputAuth, 'Bearer ' + key);
  43. inputAuth.dispatchEvent(new Event('input', { bubbles: true }));
  44. }
  45.  
  46. $(function () {
  47. setTimeout(() => {
  48. $('.btn.authorize').click(function () {
  49. setTimeout(() => {
  50. var inputAuth = $('.auth-container :input[type=text]');
  51. inputAuth.attr('placeholder', 'JWT | User,Pass');
  52. inputAuth.off().on('change', function () {
  53. var key = inputAuth.val();
  54. if (key.indexOf(',') > -1) {
  55. var user = key.split(',')[0].trim();
  56. var pass = key.split(',')[1].trim();
  57. inputAuth.prop("disabled", true);
  58. getJwt(user, pass,
  59. function (jwt) {
  60. inputAuth.css('background', '#65f30f');
  61. setJwt(jwt);
  62. window.localStorage.setItem('key', jwt);
  63. },
  64. function (err) {
  65. inputAuth.css('background', '#fd7474');
  66. //setJwt('');
  67. alert('Login failed. ', err);
  68. }, function () {
  69. inputAuth.prop("disabled", false);
  70.  
  71. });
  72. } else {
  73. inputAuth.css('background', '#FFF');
  74. //setJwt(key);
  75. }
  76. });
  77. var oldKey = window.localStorage.getItem('key');
  78. if (oldKey) {
  79. setJwt(oldKey);
  80. }
  81. }, 200);
  82. });
  83.  
  84. }, 2000 );
  85. });
  86. })();
Add Comment
Please, Sign In to add comment