szurubooru/client/js/controllers/auth_controller.js
rr- 54e3099c56 client/general: refactor control flow
- Controller lifetime is bound to route lifetime
- View lifetime is bound to controller lifetime
- Control lifetime is bound to view lifetime
- Enhanced event dispatching
- Enhanced responsiveness in some places
- Views communicate user input to controllers via new event system
2016-06-18 10:35:20 +02:00

48 lines
1.3 KiB
JavaScript

'use strict';
const router = require('../router.js');
const api = require('../api.js');
const topNavigation = require('../models/top_navigation.js');
const LoginView = require('../views/login_view.js');
class LoginController {
constructor() {
api.forget();
topNavigation.activate('login');
this._loginView = new LoginView();
this._loginView.addEventListener('submit', e => this._evtLogin(e));
}
_evtLogin(e) {
this._loginView.clearMessages();
this._loginView.disableForm();
api.forget();
api.login(e.detail.name, e.detail.password, e.detail.remember)
.then(() => {
const ctx = router.show('/');
ctx.controller.showSuccess('Logged in');
}, errorMessage => {
this._loginView.showError(errorMessage);
this._loginView.enableForm();
});
}
}
class LogoutController {
constructor() {
api.forget();
api.logout();
const ctx = router.show('/');
ctx.controller.showSuccess('Logged out');
}
}
module.exports = router => {
router.enter('/login', (ctx, next) => {
ctx.controller = new LoginController();
});
router.enter('/logout', (ctx, next) => {
ctx.controller = new LogoutController();
});
};