add redirection memory

This commit is contained in:
odysseusmax
2021-06-10 09:24:15 +05:30
parent d41485d04a
commit e658eb9b2a
3 changed files with 12 additions and 6 deletions

View File

@@ -16,6 +16,7 @@
{% endif %} {% endif %}
<form class="mt-8 space-y-6" action="/login" method="POST"> <form class="mt-8 space-y-6" action="/login" method="POST">
<input type="hidden" name="remember" value="true"> <input type="hidden" name="remember" value="true">
<input type="hidden" name="redirect_to" value="{{redirect_to}}">
<div class="rounded-md shadow-sm -space-y-px"> <div class="rounded-md shadow-sm -space-y-px">
<div> <div>
<label for="email-address" class="sr-only">Username</label> <label for="email-address" class="sr-only">Username</label>

View File

@@ -13,25 +13,27 @@ class LoginView:
async def login_post(self, req): async def login_post(self, req):
post_data = await req.post() post_data = await req.post()
redirect_to = post_data.get("redirect_to") or "/"
location = req.app.router["login_page"].url_for() location = req.app.router["login_page"].url_for()
if redirect_to != "/":
location = location.update_query({"redirect_to": redirect_to})
if "username" not in post_data: if "username" not in post_data:
loc = location.with_query({"error": "Username missing"}) loc = location.update_query({"error": "Username missing"})
raise web.HTTPFound(location=loc) raise web.HTTPFound(location=loc)
if "password" not in post_data: if "password" not in post_data:
loc = location.with_query({"error": "Password missing"}) loc = location.update_query({"error": "Password missing"})
raise web.HTTPFound(location=loc) raise web.HTTPFound(location=loc)
authenticated = (post_data["username"] == req.app["username"]) and ( authenticated = (post_data["username"] == req.app["username"]) and (
post_data["password"] == req.app["password"] post_data["password"] == req.app["password"]
) )
if not authenticated: if not authenticated:
loc = location.with_query({"error": "Wrong Username or Passowrd"}) loc = location.update_query({"error": "Wrong Username or Passowrd"})
raise web.HTTPFound(location=loc) raise web.HTTPFound(location=loc)
resp = web.Response( resp = web.Response(status=302, headers={"Location": redirect_to})
status=302, headers={"Location": str(req.app.router["home"].url_for())}
)
now = time.time() now = time.time()
resp.set_cookie( resp.set_cookie(
name="_tgindex_session", name="_tgindex_session",

View File

@@ -18,6 +18,9 @@ def middleware_factory():
]: ]:
cookies = request.cookies cookies = request.cookies
url = request.app.router["login_page"].url_for() url = request.app.router["login_page"].url_for()
if str(request.rel_url) != "/":
url = url.with_query(redirect_to=str(request.rel_url))
if any(x not in cookies for x in ("_tgindex_session", "_tgindex_secret")): if any(x not in cookies for x in ("_tgindex_session", "_tgindex_secret")):
raise HTTPFound(url) raise HTTPFound(url)