2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-06-12 20:11:43 +02:00
|
|
|
const router = require('../router.js');
|
2016-04-01 00:20:34 +02:00
|
|
|
const api = require('../api.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
2016-04-01 00:20:34 +02:00
|
|
|
const LoginView = require('../views/login_view.js');
|
2016-03-28 22:33:20 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
class LoginController {
|
2016-04-01 00:20:34 +02:00
|
|
|
constructor() {
|
2016-05-08 17:25:55 +02:00
|
|
|
api.forget();
|
2016-06-14 10:31:48 +02:00
|
|
|
topNavigation.activate('login');
|
2016-03-19 21:37:04 +01:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
this._loginView = new LoginView();
|
|
|
|
this._loginView.addEventListener('submit', e => this._evtLogin(e));
|
2016-04-06 17:56:34 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
_evtLogin(e) {
|
|
|
|
this._loginView.clearMessages();
|
|
|
|
this._loginView.disableForm();
|
2016-04-09 19:53:53 +02:00
|
|
|
api.forget();
|
2016-06-14 10:31:48 +02:00
|
|
|
api.login(e.detail.name, e.detail.password, e.detail.remember)
|
|
|
|
.then(() => {
|
|
|
|
const ctx = router.show('/');
|
|
|
|
ctx.controller.showSuccess('Logged in');
|
2016-04-09 23:41:56 +02:00
|
|
|
}, errorMessage => {
|
2016-06-14 10:31:48 +02:00
|
|
|
this._loginView.showError(errorMessage);
|
|
|
|
this._loginView.enableForm();
|
2016-04-06 17:56:34 +02:00
|
|
|
});
|
|
|
|
}
|
2016-06-14 10:31:48 +02:00
|
|
|
}
|
2016-04-08 13:17:00 +02:00
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
class LogoutController {
|
|
|
|
constructor() {
|
2016-04-09 19:53:53 +02:00
|
|
|
api.forget();
|
2016-04-08 13:17:00 +02:00
|
|
|
api.logout();
|
2016-06-14 10:31:48 +02:00
|
|
|
const ctx = router.show('/');
|
|
|
|
ctx.controller.showSuccess('Logged out');
|
2016-04-08 13:17:00 +02:00
|
|
|
}
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-06-14 10:31:48 +02:00
|
|
|
module.exports = router => {
|
|
|
|
router.enter('/login', (ctx, next) => {
|
|
|
|
ctx.controller = new LoginController();
|
|
|
|
});
|
|
|
|
router.enter('/logout', (ctx, next) => {
|
|
|
|
ctx.controller = new LogoutController();
|
|
|
|
});
|
|
|
|
};
|