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