front/auth: implement cookie support
This commit is contained in:
parent
e95ed4cc0b
commit
bb474e4cf5
3 changed files with 38 additions and 16 deletions
|
@ -13,6 +13,7 @@
|
||||||
"handlebars": "^4.0.5",
|
"handlebars": "^4.0.5",
|
||||||
"html-minifier": "^1.3.1",
|
"html-minifier": "^1.3.1",
|
||||||
"ini": "^1.3.4",
|
"ini": "^1.3.4",
|
||||||
|
"js-cookie": "^2.1.0",
|
||||||
"merge": "^1.2.0",
|
"merge": "^1.2.0",
|
||||||
"page": "^1.7.1",
|
"page": "^1.7.1",
|
||||||
"superagent": "^1.8.3",
|
"superagent": "^1.8.3",
|
||||||
|
|
|
@ -11,15 +11,15 @@ class Api {
|
||||||
|
|
||||||
get(url) {
|
get(url) {
|
||||||
const fullUrl = this.getFullUrl(url);
|
const fullUrl = this.getFullUrl(url);
|
||||||
return this.process(fullUrl, () => request.get(fullUrl));
|
return this._process(fullUrl, () => request.get(fullUrl));
|
||||||
}
|
}
|
||||||
|
|
||||||
post(url, data) {
|
post(url, data) {
|
||||||
const fullUrl = this.getFullUrl(url);
|
const fullUrl = this.getFullUrl(url);
|
||||||
return this.process(fullUrl, () => request.post(fullUrl).send(data));
|
return this._process(fullUrl, () => request.post(fullUrl).send(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
process(url, requestFactory) {
|
_process(url, requestFactory) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let req = requestFactory();
|
let req = requestFactory();
|
||||||
if (this.userName && this.userPassword) {
|
if (this.userName && this.userPassword) {
|
||||||
|
@ -42,8 +42,16 @@ class Api {
|
||||||
}
|
}
|
||||||
|
|
||||||
login(userName, userPassword) {
|
login(userName, userPassword) {
|
||||||
this.userName = userName;
|
return new Promise((resolve, reject) => {
|
||||||
this.userPassword = userPassword;
|
this.userName = userName;
|
||||||
|
this.userPassword = userPassword;
|
||||||
|
this.get('/user/' + userName)
|
||||||
|
.then(() => { resolve(); })
|
||||||
|
.catch(response => {
|
||||||
|
reject(response.description);
|
||||||
|
this.logout();
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const cookies = require('js-cookie');
|
||||||
const page = require('page');
|
const page = require('page');
|
||||||
const config = require('../config.js');
|
const config = require('../config.js');
|
||||||
|
|
||||||
|
@ -8,32 +9,44 @@ class AuthController {
|
||||||
this.api = api;
|
this.api = api;
|
||||||
this.topNavigationController = topNavigationController;
|
this.topNavigationController = topNavigationController;
|
||||||
this.loginView = loginView;
|
this.loginView = loginView;
|
||||||
/* TODO: load from cookies */
|
|
||||||
|
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() {
|
loginRoute() {
|
||||||
this.topNavigationController.activate('login');
|
this.topNavigationController.activate('login');
|
||||||
this.loginView.render({
|
this.loginView.render({
|
||||||
login: (userName, userPassword, doRemember) => {
|
login: (name, password, doRemember) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.api.login(userName, userPassword);
|
this.api.login(name, password)
|
||||||
this.api.get('/user/' + userName)
|
.then(() => {
|
||||||
.then(response => {
|
const options = {};
|
||||||
if (doRemember) {
|
if (doRemember) {
|
||||||
/* TODO: set cookie */
|
options.expires = 365;
|
||||||
}
|
}
|
||||||
|
cookies.set(
|
||||||
|
'auth',
|
||||||
|
{'user': name, 'password': password},
|
||||||
|
options);
|
||||||
resolve();
|
resolve();
|
||||||
page('/');
|
page('/');
|
||||||
/* TODO: update top navigation */
|
/* TODO: notify top navigation */
|
||||||
})
|
}).catch(errorMessage => { reject(errorMessage); });
|
||||||
.catch(response => { reject(response.description); });
|
|
||||||
});
|
});
|
||||||
}});
|
}});
|
||||||
}
|
}
|
||||||
|
|
||||||
logoutRoute() {
|
logoutRoute() {
|
||||||
this.topNavigationController.activate('logout');
|
this.api.logout();
|
||||||
/* TODO: clear cookie */
|
cookies.remove('auth');
|
||||||
|
page('/');
|
||||||
|
/* TODO: notify top navigation */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue