Set last seen and increment visits during registration, not only on authentication.
This commit is contained in:
+4
-14
@@ -9,7 +9,6 @@ Write operations: Functions that validate and commit, or raise ValueError.
|
|||||||
import hashlib
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import secrets
|
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
@@ -477,7 +476,7 @@ def create_session(
|
|||||||
if session.key in _db.sessions:
|
if session.key in _db.sessions:
|
||||||
raise ValueError("Session already exists")
|
raise ValueError("Session already exists")
|
||||||
with _db.transaction("create_session", ctx):
|
with _db.transaction("create_session", ctx):
|
||||||
_db.sessions[session.key] = session
|
session.store()
|
||||||
return session.key
|
return session.key
|
||||||
|
|
||||||
|
|
||||||
@@ -607,11 +606,6 @@ def cleanup_expired() -> int:
|
|||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def _create_token() -> str:
|
|
||||||
"""Generate a 16-character URL-safe session token."""
|
|
||||||
return secrets.token_urlsafe(12)
|
|
||||||
|
|
||||||
|
|
||||||
def login(
|
def login(
|
||||||
user_uuid: UUID,
|
user_uuid: UUID,
|
||||||
credential_uuid: UUID,
|
credential_uuid: UUID,
|
||||||
@@ -649,14 +643,10 @@ def login(
|
|||||||
)
|
)
|
||||||
user_str = str(user_uuid)
|
user_str = str(user_uuid)
|
||||||
with _db.transaction("login", user=user_str):
|
with _db.transaction("login", user=user_str):
|
||||||
# Update user
|
session.store()
|
||||||
_db.users[user_uuid].last_seen = now
|
|
||||||
_db.users[user_uuid].visits += 1
|
|
||||||
# Update credential
|
# Update credential
|
||||||
_db.credentials[credential_uuid].sign_count = sign_count
|
_db.credentials[credential_uuid].sign_count = sign_count
|
||||||
_db.credentials[credential_uuid].last_used = now
|
_db.credentials[credential_uuid].last_used = now
|
||||||
# Create session
|
|
||||||
_db.sessions[session.key] = session
|
|
||||||
return session.key
|
return session.key
|
||||||
|
|
||||||
|
|
||||||
@@ -703,8 +693,8 @@ def create_credential_session(
|
|||||||
# Create credential
|
# Create credential
|
||||||
_db.credentials[credential.uuid] = credential
|
_db.credentials[credential.uuid] = credential
|
||||||
|
|
||||||
# Create session
|
# Store session and record visit
|
||||||
_db.sessions[session.key] = session
|
session.store()
|
||||||
|
|
||||||
# Delete reset token if provided
|
# Delete reset token if provided
|
||||||
if reset_key:
|
if reset_key:
|
||||||
|
|||||||
@@ -309,6 +309,18 @@ class Session(msgspec.Struct, dict=True):
|
|||||||
"expiry": self.expiry.isoformat(),
|
"expiry": self.expiry.isoformat(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def store(self) -> None:
|
||||||
|
"""Store this session in the database and record a visit.
|
||||||
|
|
||||||
|
Updates user.last_seen and user.visits. Must be called inside
|
||||||
|
a database transaction.
|
||||||
|
"""
|
||||||
|
_data = db.data()
|
||||||
|
_data.sessions[self.key] = self
|
||||||
|
now = datetime.now(UTC)
|
||||||
|
_data.users[self.user_uuid].last_seen = now
|
||||||
|
_data.users[self.user_uuid].visits += 1
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(
|
def create(
|
||||||
cls,
|
cls,
|
||||||
|
|||||||
Reference in New Issue
Block a user