Skip to content

Commit 41da464

Browse files
committed
REACTPY_AUTO_RELOGIN
1 parent a9d98d5 commit 41da464

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

CHANGELOG.md

+7-10
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,17 @@ Using the following categories, list your changes in this order:
3636

3737
### Added
3838

39-
- ReactPy components can now use SEO compatible rendering!
40-
- `settings.py:REACTPY_PRERENDER` can be set to `True` to enable this behavior by default
41-
- Or, you can enable it on individual components via the template tag: `{% component "..." prerender="True" %}`
42-
- New components!
39+
- SEO compatible rendering!
40+
- `settings.py:REACTPY_PRERENDER` can be set to `True` to enable this behavior by default.
41+
- Or, you can enable it on individual components via the template tag: `{% component "..." prerender="True" %}`.
42+
- New `view_to_iframe` feature!
4343
- `reactpy_django.components.view_to_iframe` uses an `<iframe>` to render a Django view.
44-
- New utilies!
4544
- `reactpy_django.utils.register_iframe` notifies ReactPy which views are allowed to be used with `view_to_iframe`.
46-
- New decorators!
47-
- `reactpy_django.decorators.user_passes_test` is a similar equivalent to Django's `user_passes_test` decorator, but ours works with ReactPy components.
48-
- New hooks!
45+
- New Django `User` related features!
4946
- `reactpy_django.hooks.use_user` can be used to access the current user.
5047
- `reactpy_django.hooks.use_user_data` provides a simplified interface for storing user key-value data.
51-
- New settings!
52-
- `REACTPY_AUTO_LOGIN` automatically logs in **pre-authenticated** users that during the initial component WebSocket connection. This is useful to continuously update `last_login` timestamps.
48+
- `reactpy_django.decorators.user_passes_test` is inspired by Django's [`user_passes_test`](http://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.user_passes_test) decorator, but works with ReactPy components.
49+
- `settings.py:REACTPY_AUTO_RELOGIN` will cause component WebSocket connections to automatically perform a re-login on authenticated users. This is useful to continuously update `last_login` timestamps and refresh the [Django login session](https://docs.djangoproject.com/en/dev/topics/http/sessions/).
5350

5451
### Changed
5552

docs/src/reference/settings.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ Dotted path to the Django authentication backend to use for ReactPy components.
6262

6363
---
6464

65+
### `#!python REACTPY_AUTO_RELOGIN`
66+
67+
**Default:** `#!python False`
68+
69+
**Example Value(s):** `#!python True`
70+
71+
Enabling this will cause component WebSocket connections to automatically perform a [re-login](https://channels.readthedocs.io/en/latest/topics/authentication.html#how-to-log-a-user-in-out) on users that are already authenticated.
72+
73+
This is useful to continuously update `#!python last_login` timestamps and refresh the [Django login session](https://docs.djangoproject.com/en/dev/topics/http/sessions/).
74+
75+
---
76+
6577
## Performance Settings
6678

6779
---
@@ -128,7 +140,7 @@ You can use the `#!python host` argument in your [template tag](../reference/tem
128140

129141
**Example Value(s):** `#!python True`
130142

131-
Configures whether to pre-render your components, which enables SEO compatibility and reduces perceived latency.
143+
Configures whether to HTTP pre-render your components, which enables SEO compatibility and reduces perceived latency.
132144

133145
During pre-rendering, there are some key differences in behavior:
134146

src/reactpy_django/checks.py

+9
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,13 @@ def reactpy_errors(app_configs, **kwargs):
482482
)
483483
)
484484

485+
if not isinstance(config.REACTPY_AUTO_RELOGIN, bool):
486+
errors.append(
487+
Error(
488+
"Invalid type for REACTPY_AUTO_RELOGIN.",
489+
hint="REACTPY_AUTO_RELOGIN should be a boolean.",
490+
id="reactpy_django.E022",
491+
)
492+
)
493+
485494
return errors

src/reactpy_django/config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@
113113
"REACTPY_PRERENDER",
114114
False,
115115
)
116-
REACTPY_AUTO_LOGIN: bool = getattr(
116+
REACTPY_AUTO_RELOGIN: bool = getattr(
117117
settings,
118-
"REACTPY_AUTO_LOGIN",
119-
True,
118+
"REACTPY_AUTO_RELOGIN",
119+
False,
120120
)

src/reactpy_django/websocket/consumer.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@ async def connect(self) -> None:
4848
from reactpy_django import models
4949
from reactpy_django.config import (
5050
REACTPY_AUTH_BACKEND,
51-
REACTPY_AUTO_LOGIN,
51+
REACTPY_AUTO_RELOGIN,
5252
REACTPY_BACKHAUL_THREAD,
5353
)
5454

5555
await super().connect()
5656

57-
user: AbstractUser = self.scope.get("user")
58-
if REACTPY_AUTO_LOGIN and user and user.is_authenticated:
57+
# Automatically re-login the user, if needed
58+
user: AbstractUser | None = self.scope.get("user")
59+
if REACTPY_AUTO_RELOGIN and user and user.is_authenticated and user.is_active:
5960
try:
6061
await login(self.scope, user, backend=REACTPY_AUTH_BACKEND)
6162
except Exception:

0 commit comments

Comments
 (0)