2016-03-19 21:37:04 +01:00
|
|
|
'use strict';
|
|
|
|
|
2016-03-30 21:01:18 +02:00
|
|
|
const cookies = require('js-cookie');
|
2016-03-28 22:33:20 +02:00
|
|
|
const page = require('page');
|
2016-04-01 00:20:34 +02:00
|
|
|
const api = require('../api.js');
|
2016-04-07 19:03:49 +02:00
|
|
|
const events = require('../events.js');
|
2016-04-01 00:20:34 +02:00
|
|
|
const topNavController = require('../controllers/top_nav_controller.js');
|
|
|
|
const LoginView = require('../views/login_view.js');
|
2016-04-06 17:56:34 +02:00
|
|
|
const PasswordResetView = require('../views/password_reset_view.js');
|
2016-03-28 22:33:20 +02:00
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
class AuthController {
|
2016-04-01 00:20:34 +02:00
|
|
|
constructor() {
|
|
|
|
this.loginView = new LoginView();
|
2016-04-06 17:56:34 +02:00
|
|
|
this.passwordResetView = new PasswordResetView();
|
2016-04-07 19:03:49 +02:00
|
|
|
}
|
2016-03-30 21:01:18 +02:00
|
|
|
|
2016-04-07 19:03:49 +02:00
|
|
|
login() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const auth = cookies.getJSON('auth');
|
|
|
|
if (auth && auth.user && auth.password) {
|
|
|
|
api.login(auth.user, auth.password)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(errorMessage => {
|
|
|
|
reject(errorMessage);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:49:26 +02:00
|
|
|
registerRoutes() {
|
|
|
|
page(/\/password-reset\/([^:]+):([^:]+)$/,
|
|
|
|
(ctx, next) => {
|
|
|
|
this.passwordResetFinishRoute(ctx.params[0], ctx.params[1]);
|
|
|
|
});
|
|
|
|
page('/password-reset', (ctx, next) => { this.passwordResetRoute(); });
|
|
|
|
page('/login', (ctx, next) => { this.loginRoute(); });
|
|
|
|
page('/logout', (ctx, next) => { this.logoutRoute(); });
|
|
|
|
}
|
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
loginRoute() {
|
2016-04-01 00:20:34 +02:00
|
|
|
topNavController.activate('login');
|
2016-03-28 00:19:44 +02:00
|
|
|
this.loginView.render({
|
2016-03-30 21:01:18 +02:00
|
|
|
login: (name, password, doRemember) => {
|
2016-03-28 22:33:20 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-04-03 19:33:15 +02:00
|
|
|
cookies.remove('auth');
|
2016-04-01 00:20:34 +02:00
|
|
|
api.login(name, password)
|
2016-03-30 21:01:18 +02:00
|
|
|
.then(() => {
|
|
|
|
const options = {};
|
2016-03-28 22:33:20 +02:00
|
|
|
if (doRemember) {
|
2016-03-30 21:01:18 +02:00
|
|
|
options.expires = 365;
|
2016-03-28 22:33:20 +02:00
|
|
|
}
|
2016-03-30 21:01:18 +02:00
|
|
|
cookies.set(
|
|
|
|
'auth',
|
|
|
|
{'user': name, 'password': password},
|
|
|
|
options);
|
2016-03-28 22:33:20 +02:00
|
|
|
resolve();
|
|
|
|
page('/');
|
2016-04-07 19:03:49 +02:00
|
|
|
events.notify(events.Success, 'Logged in');
|
2016-03-30 21:01:18 +02:00
|
|
|
}).catch(errorMessage => { reject(errorMessage); });
|
2016-03-28 22:33:20 +02:00
|
|
|
});
|
2016-03-28 00:19:44 +02:00
|
|
|
}});
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logoutRoute() {
|
2016-04-01 00:20:34 +02:00
|
|
|
api.logout();
|
2016-03-30 21:01:18 +02:00
|
|
|
cookies.remove('auth');
|
|
|
|
page('/');
|
2016-04-07 19:03:49 +02:00
|
|
|
events.notify(events.Success, 'Logged out');
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
2016-04-06 17:56:34 +02:00
|
|
|
|
|
|
|
passwordResetRoute() {
|
|
|
|
topNavController.activate('login');
|
|
|
|
this.passwordResetView.render({
|
|
|
|
proceed: nameOrEmail => {
|
|
|
|
api.logout();
|
|
|
|
cookies.remove('auth');
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
api.get('/password-reset/' + nameOrEmail)
|
|
|
|
.then(() => { resolve(); })
|
|
|
|
.catch(errorMessage => { reject(errorMessage); });
|
|
|
|
});
|
|
|
|
}});
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordResetFinishRoute(name, token) {
|
|
|
|
api.logout();
|
|
|
|
cookies.remove('auth');
|
|
|
|
api.post('/password-reset/' + name, {token: token})
|
|
|
|
.then(response => {
|
|
|
|
const password = response.password;
|
|
|
|
api.login(name, password)
|
|
|
|
.then(() => {
|
|
|
|
cookies.set(
|
|
|
|
'auth', {'user': name, 'password': password}, {});
|
|
|
|
page('/');
|
2016-04-07 19:03:49 +02:00
|
|
|
events.notify(events.Success,
|
2016-04-06 17:56:34 +02:00
|
|
|
'New password: ' + password);
|
|
|
|
}).catch(errorMessage => {
|
|
|
|
page('/');
|
2016-04-07 19:03:49 +02:00
|
|
|
events.notify(events.Error, errorMessage);
|
2016-04-06 17:56:34 +02:00
|
|
|
});
|
|
|
|
}).catch(response => {
|
|
|
|
page('/');
|
2016-04-07 19:03:49 +02:00
|
|
|
events.notify(events.Error, response.description);
|
2016-04-06 17:56:34 +02:00
|
|
|
});
|
|
|
|
}
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
2016-04-01 00:20:34 +02:00
|
|
|
module.exports = new AuthController();
|