|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +from reactpy import component, hooks, web |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from django.contrib.sessions.backends.base import SessionBase |
| 10 | + |
| 11 | + |
| 12 | +SetCookie = web.export( |
| 13 | + web.module_from_file("reactpy-django", file=Path(__file__).parent.parent / "static" / "client.js"), |
| 14 | + ("SetCookie"), |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@component |
| 19 | +def auth_manager(): |
| 20 | + session_cookie, set_session_cookie = hooks.use_state("") |
| 21 | + scope = hooks.use_connection().scope |
| 22 | + |
| 23 | + @hooks.use_effect(dependencies=None) |
| 24 | + async def _session_check(): |
| 25 | + """Generate a session cookie if `login` was called in a user's component.""" |
| 26 | + from django.conf import settings |
| 27 | + |
| 28 | + session: SessionBase | None = scope.get("session") |
| 29 | + login_required: bool = scope.get("reactpy-login", False) |
| 30 | + if not login_required or not session or not session.session_key: |
| 31 | + return |
| 32 | + |
| 33 | + # Begin generating a cookie string |
| 34 | + key = session.session_key |
| 35 | + domain: str | None = settings.SESSION_COOKIE_DOMAIN |
| 36 | + httponly: bool = settings.SESSION_COOKIE_HTTPONLY |
| 37 | + name: str = settings.SESSION_COOKIE_NAME |
| 38 | + path: str = settings.SESSION_COOKIE_PATH |
| 39 | + samesite: str | bool = settings.SESSION_COOKIE_SAMESITE |
| 40 | + secure: bool = settings.SESSION_COOKIE_SECURE |
| 41 | + new_cookie = f"{name}={key}" |
| 42 | + if domain: |
| 43 | + new_cookie += f"; Domain={domain}" |
| 44 | + if httponly: |
| 45 | + new_cookie += "; HttpOnly" |
| 46 | + if isinstance(path, str): |
| 47 | + new_cookie += f"; Path={path}" |
| 48 | + if samesite: |
| 49 | + new_cookie += f"; SameSite={samesite}" |
| 50 | + if secure: |
| 51 | + new_cookie += "; Secure" |
| 52 | + if not session.get_expire_at_browser_close(): |
| 53 | + session_max_age: int = session.get_expiry_age() |
| 54 | + session_expiration: str = session.get_expiry_date().strftime("%a, %d-%b-%Y %H:%M:%S GMT") |
| 55 | + if session_expiration: |
| 56 | + new_cookie += f"; Expires={session_expiration}" |
| 57 | + if isinstance(session_max_age, int): |
| 58 | + new_cookie += f"; Max-Age={session_max_age}" |
| 59 | + |
| 60 | + # Save the cookie within this component's state so that the client-side component can ingest it |
| 61 | + scope.pop("reactpy-login") |
| 62 | + if new_cookie != session_cookie: |
| 63 | + set_session_cookie(new_cookie) |
| 64 | + |
| 65 | + def on_complete_callback(success: bool): |
| 66 | + """Remove the cookie from server-side memory if it was successfully set. |
| 67 | + This will subsequently remove the client-side cookie-setter component from the DOM.""" |
| 68 | + if success: |
| 69 | + set_session_cookie("") |
| 70 | + |
| 71 | + # If a session cookie was generated, send it to the client |
| 72 | + if session_cookie: |
| 73 | + print("Session Cookie: ", session_cookie) |
| 74 | + return SetCookie({"sessionCookie": session_cookie}, on_complete_callback) |
0 commit comments