2016-06-14 10:31:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const router = require('../router.js');
|
|
|
|
const api = require('../api.js');
|
2016-06-19 19:16:40 +02:00
|
|
|
const User = require('../models/user.js');
|
2016-06-14 10:31:48 +02:00
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
|
|
|
const RegistrationView = require('../views/registration_view.js');
|
|
|
|
|
|
|
|
class UserRegistrationController {
|
|
|
|
constructor() {
|
|
|
|
topNavigation.activate('register');
|
2016-07-13 21:50:07 +02:00
|
|
|
topNavigation.setTitle('Registration');
|
2016-06-14 10:31:48 +02:00
|
|
|
this._view = new RegistrationView();
|
|
|
|
this._view.addEventListener('submit', e => this._evtRegister(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtRegister(e) {
|
|
|
|
this._view.clearMessages();
|
|
|
|
this._view.disableForm();
|
2016-06-19 19:16:40 +02:00
|
|
|
const user = new User();
|
|
|
|
user.name = e.detail.name;
|
|
|
|
user.email = e.detail.email;
|
|
|
|
user.password = e.detail.password;
|
|
|
|
user.save().then(() => {
|
2016-06-14 10:31:48 +02:00
|
|
|
api.forget();
|
|
|
|
return api.login(e.detail.name, e.detail.password, false);
|
2016-06-23 12:25:04 +02:00
|
|
|
}, errorMessage => {
|
|
|
|
return Promise.reject(errorMessage);
|
2016-06-14 10:31:48 +02:00
|
|
|
}).then(() => {
|
|
|
|
const ctx = router.show('/');
|
|
|
|
ctx.controller.showSuccess('Welcome aboard!');
|
|
|
|
}, errorMessage => {
|
|
|
|
this._view.showError(errorMessage);
|
|
|
|
this._view.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = router => {
|
|
|
|
router.enter('/register', (ctx, next) => {
|
|
|
|
new UserRegistrationController();
|
|
|
|
});
|
|
|
|
};
|