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');
|
|
|
|
const config = require('../config.js');
|
|
|
|
|
2016-03-19 21:37:04 +01:00
|
|
|
class AuthController {
|
2016-03-28 22:33:20 +02:00
|
|
|
constructor(api, topNavigationController, loginView) {
|
|
|
|
this.api = api;
|
2016-03-19 21:37:04 +01:00
|
|
|
this.topNavigationController = topNavigationController;
|
2016-03-28 00:19:44 +02:00
|
|
|
this.loginView = loginView;
|
2016-03-30 21:01:18 +02:00
|
|
|
|
|
|
|
const auth = cookies.getJSON('auth');
|
|
|
|
if (auth && auth.user && auth.password) {
|
|
|
|
this.api.login(auth.user, auth.password).catch(() => {
|
|
|
|
cookies.remove('auth');
|
|
|
|
/* TODO: notify the user what just happened */
|
|
|
|
});
|
|
|
|
}
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loginRoute() {
|
|
|
|
this.topNavigationController.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-03-30 21:01:18 +02:00
|
|
|
this.api.login(name, password)
|
|
|
|
.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-03-30 22:05:57 +02:00
|
|
|
/* TODO: notify about login */
|
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-03-30 21:01:18 +02:00
|
|
|
this.api.logout();
|
|
|
|
cookies.remove('auth');
|
|
|
|
page('/');
|
2016-03-30 22:05:57 +02:00
|
|
|
/* TODO: notify about logout */
|
2016-03-19 21:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AuthController;
|