diff --git a/client/js/api.js b/client/js/api.js index 24d4a7a4..a9046bfe 100644 --- a/client/js/api.js +++ b/client/js/api.js @@ -1,5 +1,6 @@ 'use strict'; +const cookies = require('js-cookie'); const request = require('superagent'); const config = require('./config.js'); const events = require('./events.js'); @@ -61,12 +62,36 @@ class Api { return myRank >= minViableRank; } - login(userName, userPassword) { + loginFromCookies() { + return new Promise((resolve, reject) => { + const auth = cookies.getJSON('auth'); + if (auth && auth.user && auth.password) { + this.login(auth.user, auth.password, true) + .then(resolve) + .catch(errorMessage => { + reject(errorMessage); + }); + } else { + resolve(); + } + }); + } + + login(userName, userPassword, doRemember) { + cookies.remove('auth'); return new Promise((resolve, reject) => { this.userName = userName; this.userPassword = userPassword; this.get('/user/' + userName + '?bump-login=true') .then(response => { + const options = {}; + if (doRemember) { + options.expires = 365; + } + cookies.set( + 'auth', + {'user': userName, 'password': userPassword}, + options); this.user = response.user; resolve(); events.notify(events.Authentication); @@ -79,6 +104,7 @@ class Api { } logout() { + cookies.remove('auth'); this.user = null; this.userName = null; this.userPassword = null; diff --git a/client/js/controllers/auth_controller.js b/client/js/controllers/auth_controller.js index d68cdb89..08a4a0d5 100644 --- a/client/js/controllers/auth_controller.js +++ b/client/js/controllers/auth_controller.js @@ -1,6 +1,5 @@ 'use strict'; -const cookies = require('js-cookie'); const page = require('page'); const api = require('../api.js'); const events = require('../events.js'); @@ -14,21 +13,6 @@ class AuthController { this.passwordResetView = new PasswordResetView(); } - 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(); - } - }); - } - registerRoutes() { page(/\/password-reset\/([^:]+):([^:]+)$/, (ctx, next) => { @@ -44,17 +28,8 @@ class AuthController { this.loginView.render({ login: (name, password, doRemember) => { return new Promise((resolve, reject) => { - cookies.remove('auth'); - api.login(name, password) + api.login(name, password, doRemember) .then(() => { - const options = {}; - if (doRemember) { - options.expires = 365; - } - cookies.set( - 'auth', - {'user': name, 'password': password}, - options); resolve(); page('/'); events.notify(events.Success, 'Logged in'); @@ -65,7 +40,6 @@ class AuthController { logoutRoute() { api.logout(); - cookies.remove('auth'); page('/'); events.notify(events.Success, 'Logged out'); } @@ -75,7 +49,6 @@ class AuthController { this.passwordResetView.render({ proceed: nameOrEmail => { api.logout(); - cookies.remove('auth'); return new Promise((resolve, reject) => { api.get('/password-reset/' + nameOrEmail) .then(() => { resolve(); }) @@ -86,17 +59,14 @@ class AuthController { 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) + api.login(name, password, false) .then(() => { - cookies.set( - 'auth', {'user': name, 'password': password}, {}); page('/'); - events.notify(events.Success, - 'New password: ' + password); + events.notify( + events.Success, 'New password: ' + password); }).catch(errorMessage => { page('/'); events.notify(events.Error, errorMessage); diff --git a/client/js/controllers/users_controller.js b/client/js/controllers/users_controller.js index ce23f703..552c76fa 100644 --- a/client/js/controllers/users_controller.js +++ b/client/js/controllers/users_controller.js @@ -1,6 +1,5 @@ 'use strict'; -const cookies = require('js-cookie'); const page = require('page'); const api = require('../api.js'); const events = require('../events.js'); @@ -41,14 +40,13 @@ class UsersController { _register(name, password, email) { const data = { - 'name': name, - 'password': password, - 'email': email + name: name, + password: password, + email: email }; return new Promise((resolve, reject) => { api.post('/users/', data).then(() => { - api.login(name, password).then(() => { - cookies.set('auth', {'user': name, 'password': password}); + api.login(name, password, false).then(() => { resolve(); page('/'); events.notify(events.Success, 'Welcome aboard!'); diff --git a/client/js/main.js b/client/js/main.js index c5a22820..7a03bb46 100644 --- a/client/js/main.js +++ b/client/js/main.js @@ -4,8 +4,7 @@ require('./util/polyfill.js'); require('./util/handlebars-helpers.js'); let controllers = []; -const authController = require('./controllers/auth_controller.js'); -controllers.push(authController); +controllers.push(require('./controllers/auth_controller.js')); controllers.push(require('./controllers/posts_controller.js')); controllers.push(require('./controllers/users_controller.js')); controllers.push(require('./controllers/help_controller.js')); @@ -21,7 +20,8 @@ for (let controller of controllers) { controller.registerRoutes(); } -authController.login().then(() => { +const api = require('./api.js'); +api.loginFromCookies().then(() => { page(); }).catch(errorMessage => { page();