Skip to content

Implemented logout functionality #60

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions app/routers/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta

import flux.job
from fastapi import APIRouter, Depends, HTTPException, Request, Response
from fastapi import APIRouter, Depends, HTTPException, Request, Response, FastAPI
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.security import OAuth2PasswordRequestForm
from fastapi.templating import Jinja2Templates
Expand Down Expand Up @@ -66,6 +66,7 @@ async def home(request: Request):
{
"request": request,
"data": data,
"user": None,
},
)

Expand All @@ -79,28 +80,32 @@ async def jobs_table(request: Request, user=user_auth):
{
"request": request,
"jobs": jobs,
"user": user,
},
)


app = FastAPI()


@app.exception_handler(HTTPException)
def logout_exception_handler(request: Request, e):
if e.status_code == 401:
return RedirectResponse(url="/")
else:
return e


@router.get("/logout")
async def logout(request: Request, response: Response):
"""
This isn't entirely working yet.

I usually open a new tab/window to reset basic auth. We likely
need a logout button to be handled somehow in javascript.
Raise HTTPException with 401 status code to clear out credentials.
"""
response.delete_cookie("basic")
response.delete_cookie("bearer")
response.delete_cookie("access_token")
data = helpers.get_page("index.md")
return templates.TemplateResponse(
"index.html",
{
"request": request,
"data": data,
},
# TODO: Figure out how to redirect response
raise HTTPException(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still want to give the user a good experience here - the initial intention to redirect somewhere was to do that. Right now this logout function seems to just raise an exception but I'm not sure it does anything?

Copy link
Contributor Author

@khoing0810 khoing0810 Feb 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It was just the old code I did last year but unable to get to work on git workflow issue until now. The exception is raised with 401 status code to invalidate current credential, and hence, logging out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better user experience, I think we should start looking into another authentication method since Basic Auth is not designed to handle logout functionality (here's the rationale I found: https://stackoverflow.com/a/233551) but I'm open to any suggestion!

status_code=401,
detail="Logged out successfully",
headers={"WWW-Authenticate": "Basic"},
)


Expand Down Expand Up @@ -132,6 +137,7 @@ async def job_info(request: Request, jobid, msg=None, user=user_auth):
"request": request,
"job": job,
"info": info,
"user": user,
},
)

Expand All @@ -143,7 +149,12 @@ async def submit_job(request: Request, user=user_auth):
form = SubmitForm(request)
return templates.TemplateResponse(
"jobs/submit.html",
{"request": request, "has_gpus": settings.has_gpus, "form": form},
{
"request": request,
"has_gpus": settings.has_gpus,
"form": form,
"user": user,
},
)


Expand Down Expand Up @@ -186,6 +197,7 @@ async def submit_job_post(request: Request, user=user_auth):
"form": form,
"messages": messages,
"has_gpus": settings.has_gpus,
"user": user,
**form.__dict__,
},
)
Expand All @@ -212,6 +224,7 @@ def submit_job_helper(request, form, user):
context={
"request": request,
"form": form,
"user": user,
"messages": [message],
},
)
Expand All @@ -224,7 +237,7 @@ def submit_job_helper(request, form, user):
"request": request,
"form": form,
"has_gpus": settings.has_gpus,
**form.__dict__,
"user": user**form.__dict__,
},
)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.black]
profile = "black"
exclude = ["^env/"]
exclude = "^env/"

[tool.isort]
profile = "black" # needed for black/isort compatibility
Expand Down
59 changes: 38 additions & 21 deletions templates/include/topnav.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<button type="button" id="sidebarCollapse" class="btn btn-info">
<i class="fas fa-align-left"></i>
</button>
{% if title %}<h3 style="margin-left:20px">{{ title }}</h3>{% endif %}
<div class="container-fluid">
<button type="button" id="sidebarCollapse" class="btn btn-info">
<i class="fas fa-align-left"></i>
</button>
{% if title %}
<h3 style="margin-left: 20px">{{ title }}</h3>
{% endif %}

<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item {{'active' if active_page == 'submit_job' }}">
<a class="nav-link" href="/jobs/submit">Submit</a>
</li>
<li class="nav-item {{'active' if active_page == 'jobs' }}">
<a class="nav-link" href="/jobs">Jobs</a>
</li>
<li class="nav-item {{'active' if active_page == 'api' }}">
<a class="nav-link" href="/docs">API</a>
</li>
</ul>
</div>
<button
class="navbar-toggler"
type="button"
data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
{% set authenticated = user | string != 'Depends(check_auth)' and user !=
None %}
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item {{'' if authenticated else 'invisible'}}">
<a class="nav-link" href="{{'/logout' if authenticated else '#'}}"
>Logout</a
>
</li>
<li class="nav-item {{'active' if active_page == 'submit_job' }}">
<a class="nav-link" href="/jobs/submit">Submit</a>
</li>
<li class="nav-item {{'active' if active_page == 'jobs' }}">
<a class="nav-link" href="/jobs">Jobs</a>
</li>
<li class="nav-item {{'active' if active_page == 'api' }}">
<a class="nav-link" href="/docs">API</a>
</li>
</ul>
</div>
</div>
</nav>