diff options
| author | rr- | 2016-04-07 19:03:49 +0200 |
|---|---|---|
| committer | rr- | 2016-04-08 09:48:47 +0200 |
| commit | 8a1140eff651084b2d71cc322d0a382f1d726776 (patch) | |
| tree | ab61f8dd400d9e2f23642ee7e5ce1e716f3c8536 /client/js/controllers/auth_controller.js | |
| parent | c1816a292f6649e70e6781ecc3269c03b6efe913 (diff) | |
client/api: convert messages to events
Diffstat (limited to 'client/js/controllers/auth_controller.js')
| -rw-r--r-- | client/js/controllers/auth_controller.js | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/client/js/controllers/auth_controller.js b/client/js/controllers/auth_controller.js index 04cfec5..d68cdb8 100644 --- a/client/js/controllers/auth_controller.js +++ b/client/js/controllers/auth_controller.js @@ -3,6 +3,7 @@ const cookies = require('js-cookie'); const page = require('page'); const api = require('../api.js'); +const events = require('../events.js'); const topNavController = require('../controllers/top_nav_controller.js'); const LoginView = require('../views/login_view.js'); const PasswordResetView = require('../views/password_reset_view.js'); @@ -11,16 +12,21 @@ class AuthController { constructor() { this.loginView = new LoginView(); this.passwordResetView = new PasswordResetView(); + } - const auth = cookies.getJSON('auth'); - if (auth && auth.user && auth.password) { - api.login(auth.user, auth.password).catch(errorMessage => { - page('/'); - this.loginView.notifyError( - 'An error happened while trying to log you in: ' + - errorMessage); - }); - } + 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() { @@ -51,7 +57,7 @@ class AuthController { options); resolve(); page('/'); - this.loginView.notifySuccess('Logged in'); + events.notify(events.Success, 'Logged in'); }).catch(errorMessage => { reject(errorMessage); }); }); }}); @@ -61,7 +67,7 @@ class AuthController { api.logout(); cookies.remove('auth'); page('/'); - this.loginView.notifySuccess('Logged out'); + events.notify(events.Success, 'Logged out'); } passwordResetRoute() { @@ -89,15 +95,15 @@ class AuthController { cookies.set( 'auth', {'user': name, 'password': password}, {}); page('/'); - this.passwordResetView.notifySuccess( + events.notify(events.Success, 'New password: ' + password); }).catch(errorMessage => { page('/'); - this.passwordResetView.notifyError(errorMessage); + events.notify(events.Error, errorMessage); }); }).catch(response => { page('/'); - this.passwordResetView.notifyError(response.description); + events.notify(events.Error, response.description); }); } } |