|
31 | 31 | from mytral import security |
32 | 32 | from mytral import settings as user_settings |
33 | 33 | from mytral.migrations import FsPersistenceMigrations |
| 34 | +from mytral.routes import COOKIE_AUTO_LOGIN_SUPPRESSED |
34 | 35 | from mytral.routes import COOKIE_MOBILE |
35 | 36 | from mytral.routes import COOKIE_TOKEN |
36 | 37 | from mytral.routes import COOKIE_USER |
@@ -162,6 +163,8 @@ def signup(): |
162 | 163 | # log user in - store user_id (UUID) not username |
163 | 164 | flask.session[COOKIE_USER] = user_id |
164 | 165 | flask.session[COOKIE_TOKEN] = uuid.uuid4() |
| 166 | + # a fresh, explicit login re-enables auto-login for the next boot |
| 167 | + flask.session.pop(COOKIE_AUTO_LOGIN_SUPPRESSED, None) |
165 | 168 |
|
166 | 169 | flask.flash( |
167 | 170 | message=f"Welcome {new_username}! Your account has been created.", |
@@ -204,7 +207,24 @@ def login(): |
204 | 207 |
|
205 | 208 | auto_login_usernames = [] |
206 | 209 | if app_config.incarnation == config.MytralIncarnation.DESKTOP: |
207 | | - auto_login_usernames = list(ds.list_profile_names(auto_login=True).keys()) |
| 210 | + auto_login_profiles = ds.list_profile_names(auto_login=True) |
| 211 | + auto_login_usernames = list(auto_login_profiles.keys()) |
| 212 | + |
| 213 | + # smooth first start / single-user desktop: silently log in as the |
| 214 | + # sole auto-login user, unless the user just logged out on purpose |
| 215 | + if len(auto_login_profiles) == 1 and not flask.session.get( |
| 216 | + COOKIE_AUTO_LOGIN_SUPPRESSED, False |
| 217 | + ): |
| 218 | + user_name, user_id = next(iter(auto_login_profiles.items())) |
| 219 | + |
| 220 | + flask.session[COOKIE_USER] = user_id |
| 221 | + flask.session[COOKIE_TOKEN] = str(uuid.uuid4()) |
| 222 | + |
| 223 | + flask.flash( |
| 224 | + message=f"Auto logged in as user '{user_name}'", |
| 225 | + category="success", |
| 226 | + ) |
| 227 | + return flask.redirect(flask.url_for("home")) |
208 | 228 |
|
209 | 229 | return flask.render_template( |
210 | 230 | "log-in.html", |
@@ -294,6 +314,8 @@ def login(): |
294 | 314 | flask.session[COOKIE_USER] = user_id |
295 | 315 | # safe foo token to client cookies |
296 | 316 | flask.session[COOKIE_TOKEN] = str(uuid.uuid4()) |
| 317 | + # a fresh, explicit login re-enables auto-login for the next boot |
| 318 | + flask.session.pop(COOKIE_AUTO_LOGIN_SUPPRESSED, None) |
297 | 319 |
|
298 | 320 | # if the page width is <800px, then switch to mobile view |
299 | 321 | app_logger.debug(f"Storing page width to session: {form.page_width.data}") |
@@ -362,6 +384,9 @@ def logout(): |
362 | 384 | flask.session.pop(COOKIE_TOKEN, None) |
363 | 385 | if user_id: |
364 | 386 | ds.cache_evict(user_id=user_id) |
| 387 | + # desktop: suppress auto-login so the user can pick/create another account |
| 388 | + if app_config.incarnation == config.MytralIncarnation.DESKTOP: |
| 389 | + flask.session[COOKIE_AUTO_LOGIN_SUPPRESSED] = True |
365 | 390 | return flask.redirect(flask.url_for("login")) |
366 | 391 |
|
367 | 392 |
|
|
0 commit comments