-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from alex-oleshkevich/clear-storage
- Loading branch information
Showing
6 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,69 @@ | ||
"""This examples demonstrates base usage of this library. | ||
A CookieBackend is used. | ||
Required dependencies: python-multipart | ||
Usage: | ||
> uvicorn examples.login:app | ||
Access localhost:8000/ to see a landing page | ||
""" | ||
from starlette.applications import Starlette | ||
from starlette.middleware import Middleware | ||
from starlette.requests import Request | ||
from starlette.responses import HTMLResponse, RedirectResponse, Response | ||
from starlette.routing import Route | ||
|
||
from starsessions import SessionMiddleware | ||
|
||
|
||
async def homepage(request: Request) -> Response: | ||
"""Access this view (GET "/") to display session contents.""" | ||
return HTMLResponse( | ||
""" | ||
<form method="post" action="/login"> | ||
<label> Username <input type="text" name="username"> </label> | ||
<button type="submit">Sign in</button> | ||
</form> | ||
""" | ||
) | ||
|
||
|
||
async def login(request: Request) -> Response: | ||
form_data = await request.form() | ||
username = form_data['username'] | ||
request.session['username'] = username | ||
request.session.regenerate_id() | ||
return RedirectResponse('/profile', 302) | ||
|
||
|
||
async def logout(request: Request) -> Response: | ||
request.session.clear() | ||
return RedirectResponse('/', 302) | ||
|
||
|
||
async def profile(request: Request) -> Response: | ||
username = request.session.get('username') | ||
if not username: | ||
return RedirectResponse('/', 302) | ||
|
||
return HTMLResponse( | ||
""" | ||
<p>Hi, {username}!</p> | ||
<form method="post" action="/logout"> | ||
<button type="submit">logout</button> | ||
</form> | ||
""".format( | ||
username=username | ||
) | ||
) | ||
|
||
|
||
routes = [ | ||
Route("/", endpoint=homepage), | ||
Route("/login", endpoint=login, methods=['POST']), | ||
Route("/logout", endpoint=logout, methods=['POST']), | ||
Route("/profile", endpoint=profile), | ||
] | ||
middleware = [Middleware(SessionMiddleware, secret_key="secret", autoload=True)] | ||
app = Starlette(debug=True, routes=routes, middleware=middleware) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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