Skip to content

Commit

Permalink
Login event
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 31, 2024
1 parent 3c6ab14 commit 36be512
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion datasette/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def properties(self):
return properties

Check warning on line 17 in datasette/events.py

View check run for this annotation

Codecov / codecov/patch

datasette/events.py#L15-L17

Added lines #L15 - L17 were not covered by tests


@dataclass
class LoginEvent(Event):
name = "login"


@dataclass
class LogoutEvent(Event):
name = "logout"
Expand All @@ -32,4 +37,4 @@ class CreateTableEvent(Event):

@hookimpl
def register_events():
return [LogoutEvent, CreateTableEvent]
return [LoginEvent, LogoutEvent, CreateTableEvent]
10 changes: 5 additions & 5 deletions datasette/views/special.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from datasette.events import LogoutEvent
from datasette.events import LogoutEvent, LoginEvent
from datasette.utils.asgi import Response, Forbidden
from datasette.utils import (
actor_matches_allow,
Expand Down Expand Up @@ -81,9 +81,9 @@ async def get(self, request):
if secrets.compare_digest(token, self.ds._root_token):
self.ds._root_token = None
response = Response.redirect(self.ds.urls.instance())
response.set_cookie(
"ds_actor", self.ds.sign({"a": {"id": "root"}}, "actor")
)
root_actor = {"id": "root"}
response.set_cookie("ds_actor", self.ds.sign({"a": root_actor}, "actor"))
await self.ds.track_event(LoginEvent(actor=root_actor))
return response
else:
raise Forbidden("Invalid token")
Expand All @@ -106,7 +106,7 @@ async def post(self, request):
response = Response.redirect(self.ds.urls.instance())
response.set_cookie("ds_actor", "", expires=0, max_age=0)
self.ds.add_message(request, "You are now logged out", self.ds.WARNING)
await self.ds.track_event(LogoutEvent(request.actor))
await self.ds.track_event(LogoutEvent(actor=request.actor))
return response


Expand Down

0 comments on commit 36be512

Please sign in to comment.