2016-06-14 10:31:48 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const router = require('../router.js');
|
|
|
|
const api = require('../api.js');
|
|
|
|
const topNavigation = require('../models/top_navigation.js');
|
|
|
|
const PasswordResetView = require('../views/password_reset_view.js');
|
|
|
|
|
|
|
|
class PasswordResetController {
|
|
|
|
constructor() {
|
|
|
|
topNavigation.activate('login');
|
2016-07-13 21:50:07 +02:00
|
|
|
topNavigation.setTitle('Password reminder');
|
2016-06-14 10:31:48 +02:00
|
|
|
|
|
|
|
this._passwordResetView = new PasswordResetView();
|
|
|
|
this._passwordResetView.addEventListener(
|
|
|
|
'submit', e => this._evtReset(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
_evtReset(e) {
|
|
|
|
this._passwordResetView.clearMessages();
|
|
|
|
this._passwordResetView.disableForm();
|
|
|
|
api.forget();
|
|
|
|
api.logout();
|
|
|
|
api.get('/password-reset/' + e.detail.userNameOrEmail)
|
|
|
|
.then(() => {
|
|
|
|
this._passwordResetView.showSuccess(
|
|
|
|
'E-mail has been sent. To finish the procedure, ' +
|
|
|
|
'please click the link it contains.');
|
2017-01-08 02:12:38 +01:00
|
|
|
}, error => {
|
|
|
|
this._passwordResetView.showError(error.message);
|
2016-06-14 10:31:48 +02:00
|
|
|
this._passwordResetView.enableForm();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PasswordResetFinishController {
|
|
|
|
constructor(name, token) {
|
|
|
|
api.forget();
|
|
|
|
api.logout();
|
|
|
|
let password = null;
|
|
|
|
api.post('/password-reset/' + name, {token: token})
|
|
|
|
.then(response => {
|
|
|
|
password = response.password;
|
|
|
|
return api.login(name, password, false);
|
|
|
|
}).then(() => {
|
|
|
|
const ctx = router.show('/');
|
|
|
|
ctx.controller.showSuccess('New password: ' + password);
|
2017-01-08 02:12:38 +01:00
|
|
|
}, error => {
|
2016-06-14 10:31:48 +02:00
|
|
|
const ctx = router.show('/');
|
2017-01-08 02:12:38 +01:00
|
|
|
ctx.controller.showError(error.message);
|
2016-06-14 10:31:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = router => {
|
|
|
|
router.enter('/password-reset', (ctx, next) => {
|
|
|
|
ctx.controller = new PasswordResetController();
|
|
|
|
});
|
|
|
|
router.enter(/\/password-reset\/([^:]+):([^:]+)$/, (ctx, next) => {
|
|
|
|
ctx.controller = new PasswordResetFinishController(
|
2016-07-07 21:18:35 +02:00
|
|
|
ctx.parameters[0], ctx.parameters[1]);
|
2016-06-14 10:31:48 +02:00
|
|
|
});
|
|
|
|
};
|