1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- class LoginPage {
- static AuthorizeResponse = {
- SUCCESS: 'success',
- RETURN_TO: 'returnTo'
- };
- start() {
- this.loginInput = DOM.get('.login-form>.login-form-login>.login-form-login-input').focus();
- this.passwordInput = DOM.get('.login-form>.login-form-password>.login-form-password-input');
- this.loginButton = DOM.get('.login-form>.login-form-button');
- this.failureText = DOM.get('.login-form>.login-form-failure');
- this.registerEventHandlers();
- return this;
- }
- registerEventHandlers() {
- this.loginButton.on(DOM.Events.Click, this.authorize.bind(this));
- window_.on(DOM.Events.KeyDown, (e) => {
- if(e.key === DOM.Keys.Enter)
- this.authorize();
- });
- }
- authorize(e) {
- const login = this.loginInput.getValue();
- const password = this.passwordInput.getValue();
-
- fetch('/authorize', {
- method: 'POST',
- headers:{
- 'Content-Type': 'application/x-www-form-urlencoded'
- },
- body: new URLSearchParams({
- 'login': login,
- 'password': password
- })
- }).then((res) => res.json()).then((res) => {
- if(res[LoginPage.AuthorizeResponse.SUCCESS]) {
- const returnTo = res[LoginPage.AuthorizeResponse.RETURN_TO];
- Url.goTo(returnTo ? returnTo : '/');
- } else {
- this.failureText.setInnerHTML('Wrong login or password');
- }
- });
- }
- }
- window_.on(DOM.Events.Load, (e) => {
- window.page = new LoginPage().start();
- });
|