title: How to logout in FastAPI-login?
body: <p>I have a basic web site on FastAPI. I would like to implement login/logout (Auth) behavior similar to Flask-login, i.e. allow access to a function/path with decorator like @login_required or FastAPI Dependecy injection.</p>
  <p>I found <a href="https://fastapi-login.readthedocs.io/" rel="nofollow noreferrer">fastapi-login</a> module that advertised to be similar to Flask-login, but it thin on documentation to say the least. It is totally missing logout</p>
  <p>So, I'm looking for help to correctly implement logout.</p>
  <p>Here is  file structure:</p>
  <pre><code>├── app
  │   ├── __init__.py
  │   └── main.py
  ├── requirements.txt
  └── templates
      └── login.html.j2 
  </code></pre>
  <p>and working app.main.py code without logout:</p>
  <pre><code>from fastapi.responses import RedirectResponse, HTMLResponse
  from fastapi.security import OAuth2PasswordRequestForm
  from fastapi_login import LoginManager 
  from fastapi_login.exceptions import InvalidCredentialsException
  from fastapi.templating import Jinja2Templates
  from sys import path
  
  app = FastAPI()
  
  SECRET = &quot;secret-key&quot;
  
  templates = Jinja2Templates(directory=&quot;templates&quot;)
  
  manager = LoginManager(SECRET, tokenUrl=&quot;/auth/login&quot;, use_cookie=True)
  manager.cookie_name = &quot;some-name&quot;
  
  DB = {&quot;username&quot;: {&quot;password&quot;: &quot;1234567&quot;}}  # unhashed
  
  
  @manager.user_loader
  def load_user(username: str):
     user = DB.get(username)
     return user
  
  
  @app.post(&quot;/auth/login&quot;)
  def login(data: OAuth2PasswordRequestForm = Depends()):
     username = data.username
     password = data.password
     user = load_user(username)
     if not user:
         raise InvalidCredentialsException
     elif password != user['password']:
         raise InvalidCredentialsException
     access_token = manager.create_access_token(
         data={&quot;sub&quot;: username}
     )
     resp = RedirectResponse(url=&quot;/private&quot;, status_code=status.HTTP_302_FOUND)
     manager.set_cookie(resp, access_token)
     return resp
  
  
  @app.get(&quot;/private&quot;)
  def getPrivateendpoint(_=Depends(manager)):
     return &quot;You are an authentciated user&quot;
  
  
  @app.get(&quot;/public&quot;)
  def getPublicendpoint():
     return &quot;You are just a user&quot;
  
  
  @ app.get(&quot;/auth/login&quot;, response_class=HTMLResponse)
  def login(request: Request):
     context = {'request': request, }
     return templates.TemplateResponse(&quot;login.html.j2&quot;, context)
  
  </code></pre>
  <p>I have attempted logout function (below), but it does not work.  it just redirects to login page, but does not actually remove access.</p>
  <pre><code>@ app.get(&quot;/auth/logout&quot;, response_class=HTMLResponse)
  def logout(request: Request):
     response = RedirectResponse(url=&quot;/auth/login&quot;)
     response.delete_cookie(&quot;Authorization&quot;, domain=&quot;localtest.me&quot;)
     return response
  </code></pre>
  <p>I suspect that something wrong with delete_cookie params, but has no clue what these params should be:</p>
  <pre><code>response.delete_cookie(&quot;Authorization&quot;, domain=&quot;localtest.me&quot;) 
  </code></pre>
  <p>Here is  the template:</p>
  <pre><code>&lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/twinklecss@1.1.0/twinkle.min.css&quot;/&gt;
  &lt;/head&gt;
  &lt;body&gt;
     &lt;div class=&quot;flex p-4 m-6 justify-center&quot;&gt;
         &lt;form class=&quot;bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4&quot; method=&quot;POST&quot; action=&quot;/auth/login&quot; &gt;
           &lt;div class=&quot;mb-4&quot;&gt;
             &lt;label class=&quot;block text-gray-700 text-sm font-bold mb-2&quot; for=&quot;username&quot;&gt;
               Username
             &lt;/label&gt;
             &lt;input class=&quot;shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline&quot; id=&quot;username&quot; name=&quot;username&quot; type=&quot;text&quot;&gt;
           &lt;/div&gt;
           &lt;div class=&quot;mb-6&quot;&gt;
             &lt;label class=&quot;block text-gray-700 text-sm font-bold mb-2&quot; for=&quot;password&quot;&gt;
               Password
             &lt;/label&gt;
             &lt;input class=&quot;shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline&quot; id=&quot;password&quot; name=&quot;password&quot; type=&quot;password&quot;&gt;
           &lt;/div&gt;
           &lt;div class=&quot;flex items-center justify-between&quot;&gt;
             &lt;button type=&quot;submit&quot; class=&quot;bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline&quot;&gt;
               Sign In
             &lt;/button&gt;
           &lt;/div&gt;
         &lt;/form&gt;
       &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>Please help with logout.</p>