summaryrefslogtreecommitdiff
path: root/client/js/controllers/auth_controller.js
diff options
context:
space:
mode:
Diffstat (limited to 'client/js/controllers/auth_controller.js')
-rw-r--r--client/js/controllers/auth_controller.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/client/js/controllers/auth_controller.js b/client/js/controllers/auth_controller.js
new file mode 100644
index 0000000..39a0c43
--- /dev/null
+++ b/client/js/controllers/auth_controller.js
@@ -0,0 +1,56 @@
+'use strict';
+
+const cookies = require('js-cookie');
+const page = require('page');
+const api = require('../api.js');
+const topNavController = require('../controllers/top_nav_controller.js');
+const LoginView = require('../views/login_view.js');
+
+class AuthController {
+ constructor() {
+ this.loginView = new LoginView();
+
+ const auth = cookies.getJSON('auth');
+ if (auth && auth.user && auth.password) {
+ api.login(auth.user, auth.password).catch(errorMessage => {
+ cookies.remove('auth');
+ page('/');
+ this.loginView.notifyError(
+ 'An error happened while trying to log you in: ' +
+ errorMessage);
+ });
+ }
+ }
+
+ loginRoute() {
+ topNavController.activate('login');
+ this.loginView.render({
+ login: (name, password, doRemember) => {
+ return new Promise((resolve, reject) => {
+ api.login(name, password)
+ .then(() => {
+ const options = {};
+ if (doRemember) {
+ options.expires = 365;
+ }
+ cookies.set(
+ 'auth',
+ {'user': name, 'password': password},
+ options);
+ resolve();
+ page('/');
+ this.loginView.notifySuccess('Logged in');
+ }).catch(errorMessage => { reject(errorMessage); });
+ });
+ }});
+ }
+
+ logoutRoute() {
+ api.logout();
+ cookies.remove('auth');
+ page('/');
+ this.loginView.notifySuccess('Logged out');
+ }
+}
+
+module.exports = new AuthController();