client/api: move cookies management to API
This commit is contained in:
parent
51a889e3a8
commit
90d4401024
4 changed files with 38 additions and 44 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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!');
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue