diff options
| author | rr- | 2016-03-30 21:01:18 +0200 |
|---|---|---|
| committer | rr- | 2016-03-30 21:04:00 +0200 |
| commit | bb474e4cf5568e1858649cf1675d02a08f4f5299 (patch) | |
| tree | 0762578cbb944bc77275fc00fde28852e3072570 /static/js/controllers/auth_controller.js | |
| parent | e95ed4cc0bbb3a4e6ca5d109a573a0c423bb1dd9 (diff) | |
front/auth: implement cookie support
Diffstat (limited to 'static/js/controllers/auth_controller.js')
| -rw-r--r-- | static/js/controllers/auth_controller.js | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/static/js/controllers/auth_controller.js b/static/js/controllers/auth_controller.js index 4bcd6ae..7529136 100644 --- a/static/js/controllers/auth_controller.js +++ b/static/js/controllers/auth_controller.js @@ -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 */ } } |