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