-
Notifications
You must be signed in to change notification settings - Fork 131
add logout all sessions example #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ncoop57
wants to merge
9
commits into
main
Choose a base branch
from
auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca65d7f
add logout all sessions example
ncoop57 5b003c4
add reqs
ncoop57 1511d05
Update logout_all/main.py
ncoop57 2407496
Update logout_all/main.py
ncoop57 d06bee7
Update logout_all/main.py
ncoop57 dcb75a3
Update logout_all/main.py
ncoop57 2f674b7
Update logout_all/main.py
ncoop57 f65132a
Update logout_all/main.py
ncoop57 55bb520
Update logout_all/main.py
ncoop57 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, UTC | ||
| from fasthtml.common import * | ||
| from fasthtml.oauth import GoogleAppClient, OAuth | ||
|
|
||
| import os | ||
|
|
||
| now = lambda: int(datetime.now(UTC).timestamp()) | ||
|
|
||
| @dataclass | ||
| class User: id: str; logout_time: int | ||
| db = database('/tmp/data.db') | ||
| db.users = db.create(User, transform=True) | ||
|
ncoop57 marked this conversation as resolved.
Outdated
|
||
|
|
||
| logout_time = int(datetime.now(UTC).timestamp()) | ||
|
ncoop57 marked this conversation as resolved.
Outdated
|
||
|
|
||
| class Auth(OAuth): | ||
| def get_auth(self, info, ident, session, state): | ||
| if not info.sub: RedirectResponse('/login', status_code=303) | ||
| db.users.upsert(User(id=info.sub, logout_time=now())) | ||
| session['login_time'] = now() | ||
| return RedirectResponse('/', status_code=303) | ||
|
|
||
| def check_invalid(self, req, session, auth): | ||
| if session.get('login_time', 0) < db.users[auth].logout_time: | ||
| session.pop('auth', None) | ||
| return RedirectResponse('/login', status_code=419) | ||
| return False | ||
|
|
||
| app = FastHTML() | ||
|
ncoop57 marked this conversation as resolved.
Outdated
|
||
| cli = GoogleAppClient(os.environ['GOOGLE_CLIENT_ID'], os.environ['GOOGLE_CLIENT_SECRET']) | ||
| oauth = Auth(app, cli) | ||
|
|
||
| @app.get('/logout') | ||
|
ncoop57 marked this conversation as resolved.
Outdated
|
||
| def logout(session): | ||
| if auth := session.pop('auth', None): db.users.upsert(id=auth, logout_time=now()) | ||
| return oauth.redir_login(session) | ||
|
|
||
| @app.get('/') | ||
|
ncoop57 marked this conversation as resolved.
Outdated
|
||
| def home(auth): return P('Logged in!'), A('Log out', href='/logout') | ||
|
|
||
| @app.get('/login') | ||
|
ncoop57 marked this conversation as resolved.
Outdated
|
||
| def login(req): return Div(P("Not logged in"), A('Log in', href=oauth.login_link(req))) | ||
|
|
||
| serve() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| python-fasthtml |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.