mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Implement logout route
This commit is contained in:
@@ -47,6 +47,14 @@ This method will return a response with a valid `set-cookie` header upon success
|
||||
|
||||
> Check documentation about [login route](../../usage/routes.md#post-loginname).
|
||||
|
||||
## Logout
|
||||
|
||||
This method will remove the authentication cookie:
|
||||
|
||||
!!! success "`200 OK`"
|
||||
|
||||
> Check documentation about [logout route](../../usage/routes.md#post-logoutname).
|
||||
|
||||
## Authentication
|
||||
|
||||
This method expects that you provide a valid cookie in the headers.
|
||||
|
||||
@@ -10,6 +10,8 @@ When checking authentication, each method is run one after the other. The first
|
||||
|
||||
Each defined method will generate a [`/login/{name}`](../../usage/routes.md#post-loginname) route where `name` is defined on the authentication method object.
|
||||
|
||||
Each defined method will generate a [`/logout/{name}`](../../usage/routes.md#post-logoutname) route where `name` is defined on the authentication method object.
|
||||
|
||||
## Provided methods
|
||||
|
||||
* [JWT authentication](jwt.md)
|
||||
|
||||
@@ -41,6 +41,14 @@ This method will return a JWT token upon successful login:
|
||||
|
||||
> Check documentation about [login route](../../usage/routes.md#post-loginname).
|
||||
|
||||
## Logout
|
||||
|
||||
This method is not applicable to this backend and won't do anything.
|
||||
|
||||
!!! success "`202 Accepted`"
|
||||
|
||||
> Check documentation about [logout route](../../usage/routes.md#post-logoutname).
|
||||
|
||||
## Authentication
|
||||
|
||||
This method expects that you provide a `Bearer` authentication with a valid JWT.
|
||||
|
||||
77
docs/src/full_sqlalchemy.py
Normal file
77
docs/src/full_sqlalchemy.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import databases
|
||||
import sqlalchemy
|
||||
from fastapi import FastAPI
|
||||
from fastapi_users import FastAPIUsers, models
|
||||
from fastapi_users.authentication import JWTAuthentication
|
||||
from fastapi_users.db import (
|
||||
SQLAlchemyBaseUserTable,
|
||||
SQLAlchemyUserDatabase,
|
||||
)
|
||||
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
||||
|
||||
DATABASE_URL = "sqlite:///./test.db"
|
||||
SECRET = "SECRET"
|
||||
|
||||
|
||||
class User(models.BaseUser):
|
||||
pass
|
||||
|
||||
|
||||
class UserCreate(User, models.BaseUserCreate):
|
||||
pass
|
||||
|
||||
|
||||
class UserUpdate(User, models.BaseUserUpdate):
|
||||
pass
|
||||
|
||||
|
||||
class UserDB(User, models.BaseUserDB):
|
||||
pass
|
||||
|
||||
|
||||
database = databases.Database(DATABASE_URL)
|
||||
Base: DeclarativeMeta = declarative_base()
|
||||
|
||||
|
||||
class UserTable(Base, SQLAlchemyBaseUserTable):
|
||||
pass
|
||||
|
||||
|
||||
engine = sqlalchemy.create_engine(
|
||||
DATABASE_URL, connect_args={"check_same_thread": False}
|
||||
)
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
users = UserTable.__table__
|
||||
user_db = SQLAlchemyUserDatabase(UserDB, database, users)
|
||||
|
||||
|
||||
auth_backends = [
|
||||
JWTAuthentication(secret=SECRET, lifetime_seconds=3600),
|
||||
]
|
||||
|
||||
app = FastAPI()
|
||||
fastapi_users = FastAPIUsers(
|
||||
user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, SECRET,
|
||||
)
|
||||
app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
|
||||
|
||||
|
||||
@fastapi_users.on_after_register()
|
||||
def on_after_register(user: User):
|
||||
print(f"User {user.id} has registered.")
|
||||
|
||||
|
||||
@fastapi_users.on_after_forgot_password()
|
||||
def on_after_forgot_password(user: User, token: str):
|
||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup():
|
||||
await database.connect()
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
await database.disconnect()
|
||||
55
docs/src/full_tortoise.py
Normal file
55
docs/src/full_tortoise.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi_users import FastAPIUsers, models
|
||||
from fastapi_users.authentication import JWTAuthentication
|
||||
from fastapi_users.db import (
|
||||
TortoiseBaseUserModel,
|
||||
TortoiseUserDatabase,
|
||||
)
|
||||
from tortoise.contrib.starlette import register_tortoise
|
||||
|
||||
DATABASE_URL = "sqlite://./test.db"
|
||||
SECRET = "SECRET"
|
||||
|
||||
|
||||
class User(models.BaseUser):
|
||||
pass
|
||||
|
||||
|
||||
class UserCreate(User, models.BaseUserCreate):
|
||||
pass
|
||||
|
||||
|
||||
class UserUpdate(User, models.BaseUserUpdate):
|
||||
pass
|
||||
|
||||
|
||||
class UserDB(User, models.BaseUserDB):
|
||||
pass
|
||||
|
||||
|
||||
class UserModel(TortoiseBaseUserModel):
|
||||
pass
|
||||
|
||||
|
||||
user_db = TortoiseUserDatabase(UserDB, UserModel)
|
||||
app = FastAPI()
|
||||
register_tortoise(app, db_url=DATABASE_URL, modules={"models": ["test"]})
|
||||
|
||||
auth_backends = [
|
||||
JWTAuthentication(secret=SECRET, lifetime_seconds=3600),
|
||||
]
|
||||
|
||||
fastapi_users = FastAPIUsers(
|
||||
user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, SECRET,
|
||||
)
|
||||
app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
|
||||
|
||||
|
||||
@fastapi_users.on_after_register()
|
||||
def on_after_register(user: User):
|
||||
print(f"User {user.id} has registered.")
|
||||
|
||||
|
||||
@fastapi_users.on_after_forgot_password()
|
||||
def on_after_forgot_password(user: User, token: str):
|
||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||
@@ -57,6 +57,19 @@ Login a user against the method named `name`. Check the corresponding [authentic
|
||||
}
|
||||
```
|
||||
|
||||
### `POST /logout/{name}`
|
||||
|
||||
Logout the authenticated user against the method named `name`. Check the corresponding [authentication method](../configuration/authentication/index.md) to view the success response.
|
||||
|
||||
!!! fail "`401 Unauthorized`"
|
||||
Missing token or inactive user.
|
||||
|
||||
!!! success "`200 OK`"
|
||||
The logout process was successful.
|
||||
|
||||
!!! success "`202 Accepted`"
|
||||
The logout process is not applicable for this authentication backend (e.g. JWT).
|
||||
|
||||
### `POST /forgot-password`
|
||||
|
||||
Request a reset password procedure. Will generate a temporary token and call the `on_after_forgot_password` [event handlers](../configuration/router.md#event-handlers) if the user exists.
|
||||
|
||||
Reference in New Issue
Block a user