script.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. class LoginPage {
  2. static AuthorizeResponse = {
  3. SUCCESS: 'success',
  4. RETURN_TO: 'returnTo'
  5. };
  6. start() {
  7. this.loginInput = DOM.get('.login-form>.login-form-login>.login-form-login-input').focus();
  8. this.passwordInput = DOM.get('.login-form>.login-form-password>.login-form-password-input');
  9. this.loginButton = DOM.get('.login-form>.login-form-button');
  10. this.failureText = DOM.get('.login-form>.login-form-failure');
  11. this.registerEventHandlers();
  12. return this;
  13. }
  14. registerEventHandlers() {
  15. this.loginButton.on(DOM.Events.Click, this.authorize.bind(this));
  16. window_.on(DOM.Events.KeyDown, (e) => {
  17. if(e.key === DOM.Keys.Enter)
  18. this.authorize();
  19. });
  20. }
  21. authorize(e) {
  22. const login = this.loginInput.getValue();
  23. const password = this.passwordInput.getValue();
  24. fetch('/authorize', {
  25. method: 'POST',
  26. headers:{
  27. 'Content-Type': 'application/x-www-form-urlencoded'
  28. },
  29. body: new URLSearchParams({
  30. 'login': login,
  31. 'password': password
  32. })
  33. }).then((res) => res.json()).then((res) => {
  34. if(res[LoginPage.AuthorizeResponse.SUCCESS]) {
  35. const returnTo = res[LoginPage.AuthorizeResponse.RETURN_TO];
  36. Url.goTo(returnTo ? returnTo : '/');
  37. } else {
  38. this.failureText.setInnerHTML('Wrong login or password');
  39. }
  40. });
  41. }
  42. }
  43. window_.on(DOM.Events.Load, (e) => {
  44. window.page = new LoginPage().start();
  45. });