From da1386c7d2033a54ffe2ed625c3d40f9512bdf92 Mon Sep 17 00:00:00 2001
From: rr- <rr-@sakuya.pl>
Date: Sun, 8 May 2016 17:25:55 +0200
Subject: [PATCH] client/auth: forget user when they try to log in

This is to let user log in if their login cookie is broken (for example
when it refers to a non existing user). Normally, the client should
treat errors caused by cookie logins as temporary server failures,
inform the user about it and leave the cookie, so that when the server
is up again, all user needs to do is to reload the page rather than
logging again.
---
 client/js/controllers/auth_controller.js |  1 +
 client/js/main.js                        | 13 +++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/client/js/controllers/auth_controller.js b/client/js/controllers/auth_controller.js
index 9db2a001..01c01431 100644
--- a/client/js/controllers/auth_controller.js
+++ b/client/js/controllers/auth_controller.js
@@ -24,6 +24,7 @@ class AuthController {
     }
 
     loginRoute() {
+        api.forget();
         topNavController.activate('login');
         this.loginView.render({
             login: (name, password, doRemember) => {
diff --git a/client/js/main.js b/client/js/main.js
index 468386c7..0245e5e6 100644
--- a/client/js/main.js
+++ b/client/js/main.js
@@ -30,8 +30,13 @@ const api = require('./api.js');
 api.loginFromCookies().then(() => {
     page();
 }).catch(errorMessage => {
-    page('/');
-    events.notify(
-        events.Error,
-        'An error happened while trying to log you in: ' + errorMessage);
+    if (window.location.href.indexOf('login') !== -1) {
+        api.forget();
+        page();
+    } else {
+        page('/');
+        events.notify(
+            events.Error,
+            'An error happened while trying to log you in: ' + errorMessage);
+    }
 });