mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 11:11:16 +08:00

* Implement Transport classes * Implement authentication strategy classes * Revamp authentication with Transport and Strategy * Revamp strategy and OAuth so that they can use a callable dependency * Update docstring * Make ErrorCode a proper Enum and cleanup unused OpenAPI utils * Remove useless check * Tweak typing in authenticator * Update docs * Improve logout/destroy token logic * Update docs * Update docs * Update docs and full examples * Apply formatting to examples * Update OAuth doc and examples * Add migration doc * Implement Redis session token * Add Redis Session documentation * RedisSession -> Redis * Fix links in docs
1.2 KiB
1.2 KiB
Redis
Redis is an ultra-fast key-store database. As such, it's a good candidate for token management. In this strategy, a token is generated and associated with the user id. in the database. On each request, we try to retrieve this token from Redis to get the corresponding user id.
Configuration
import aioredis
from fastapi_users.authentication import RedisStrategy
redis = aioredis.from_url("redis://localhost:6379", decode_responses=True)
def get_redis_strategy() -> RedisStrategy:
return RedisStrategy(redis, lifetime_seconds=3600)
As you can see, instantiation is quite simple. It accepts the following arguments:
redis
(aioredis.Redis
): An instance ofaioredis.Redis
. Note that thedecode_responses
flag set toTrue
is necessary.lifetime_seconds
(Optional[int]
): The lifetime of the token in seconds. Defaults toNone
, which means the token doesn't expire.
!!! tip "Why it's inside a function?" To allow strategies to be instantiated dynamically with other dependencies, they have to be provided as a callable to the authentication backend.
Logout
On logout, this strategy will delete the token from the Redis store.