szurubooru/static/js/controllers/auth_controller.js

54 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
2016-03-30 21:01:18 +02:00
const cookies = require('js-cookie');
const page = require('page');
const config = require('../config.js');
class AuthController {
constructor(api, topNavigationController, loginView) {
this.api = api;
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 */
});
}
}
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) => {
return new Promise((resolve, reject) => {
2016-03-30 21:01:18 +02:00
this.api.login(name, password)
.then(() => {
const options = {};
if (doRemember) {
2016-03-30 21:01:18 +02:00
options.expires = 365;
}
2016-03-30 21:01:18 +02:00
cookies.set(
'auth',
{'user': name, 'password': password},
options);
resolve();
page('/');
/* TODO: notify about login */
2016-03-30 21:01:18 +02:00
}).catch(errorMessage => { reject(errorMessage); });
});
2016-03-28 00:19:44 +02:00
}});
}
logoutRoute() {
2016-03-30 21:01:18 +02:00
this.api.logout();
cookies.remove('auth');
page('/');
/* TODO: notify about logout */
}
}
module.exports = AuthController;