Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- function getJwt(user, pass, success, error, complete) {
- console.log('getJwt ', user);
- $.ajax({
- url: '/api/login',
- type: 'POST',
- contentType: "application/json",
- dataType: "json",
- data: JSON.stringify({
- username: user,
- password: pass
- }),
- success: function (data) {
- success && success(data.data.token);
- },
- error: function (jqXhr, err, msg) {
- error && error(JSON.parse(jqXhr.responseText).error_description);
- },
- complete: complete
- });
- }
- function setNativeValue(element, value) {
- const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
- const prototype = Object.getPrototypeOf(element);
- const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
- if (valueSetter && valueSetter !== prototypeValueSetter) {
- prototypeValueSetter.call(element, value);
- } else {
- valueSetter.call(element, value);
- }
- }
- function setJwt(key) {
- console.log("setJwt ", key);
- var inputAuth = $('.auth-container :input[type=text]')[0];
- //inputAuth.off().val("Bearer " + key).change();
- //inputAuth.attr('value', 'Bearer ' + key);
- setNativeValue(inputAuth, 'Bearer ' + key);
- inputAuth.dispatchEvent(new Event('input', { bubbles: true }));
- }
- $(function () {
- setTimeout(() => {
- $('.btn.authorize').click(function () {
- setTimeout(() => {
- var inputAuth = $('.auth-container :input[type=text]');
- inputAuth.attr('placeholder', 'JWT | User,Pass');
- inputAuth.off().on('change', function () {
- var key = inputAuth.val();
- if (key.indexOf(',') > -1) {
- var user = key.split(',')[0].trim();
- var pass = key.split(',')[1].trim();
- inputAuth.prop("disabled", true);
- getJwt(user, pass,
- function (jwt) {
- inputAuth.css('background', '#65f30f');
- setJwt(jwt);
- window.localStorage.setItem('key', jwt);
- },
- function (err) {
- inputAuth.css('background', '#fd7474');
- //setJwt('');
- alert('Login failed. ', err);
- }, function () {
- inputAuth.prop("disabled", false);
- });
- } else {
- inputAuth.css('background', '#FFF');
- //setJwt(key);
- }
- });
- var oldKey = window.localStorage.getItem('key');
- if (oldKey) {
- setJwt(oldKey);
- }
- }, 200);
- });
- }, 2000 );
- });
- })();
Add Comment
Please, Sign In to add comment