Skip to content

How to expire token in a logout endpoint ? #2263

@Ahtii

Description

@Ahtii

Hi,
I moved from Django to FastAPI because of its speed (native support for asynchronous code). I am learning it from past few weeks. I have implemented login authentication with OAuth2PasswordBearer and generating tokens with JWT so far so good.

Now I want to implement Logout endpoint I googled it didn't found anything useful so I thought of implementing it by manipulating the token by setting expire minutes to 0 but I don't really know how to do it here is my code so for:

main.py

@app.post("/api/logout")
async def logout(token: str = Depends(settings.TOKEN_MANAGER)):
    views.set_expiry(0, token)
    return {"response": "Logged out"}

settings.py

ACCESS_TOKEN_EXPIRE_MINUTES = 30
TOKEN_URL = "/api/auth/token"
TOKEN_MANAGER = OAuth2PasswordBearer(tokenUrl=TOKEN_URL)

views.py

def set_expiry(timestamp, token):
    # code to expire token

I also thought of simply assigning token to None. I just wanna know what is the best practice to do it ? I will be working in back-end only.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions