2727<p align =" center " >
2828 <a href =" https://github.com/amisadmin/fastapi_user_auth " target =" _blank " >源码</a >
2929 ·
30- <a href =" http://demo.amis.work/admin " target =" _blank " >在线演示</a >
30+ <a href =" http://user-auth. demo.amis.work/ " target =" _blank " >在线演示</a >
3131 ·
3232 <a href =" http://docs.amis.work " target =" _blank " >文档</a >
3333 ·
3636
3737------
3838
39- ` FastAPI-User-Auth ` 是一个基于 [ FastAPI-Amis-Admin] ( https://github.com/amisadmin/fastapi_amis_admin ) 的应用插件,与` FastAPI-Amis-Admin ` 深度结合,为其提供用户认证与授权.
40-
41-
39+ ` FastAPI-User-Auth ` 是一个基于 [ FastAPI-Amis-Admin] ( https://github.com/amisadmin/fastapi_amis_admin )
40+ 的应用插件,与` FastAPI-Amis-Admin ` 深度结合,为其提供用户认证与授权.
4241
4342## 安装
4443
@@ -63,6 +62,7 @@ auth = site.auth
6362# 挂载后台管理系统
6463site.mount_app(app)
6564
65+
6666# 创建初始化数据库表
6767@app.on_event (" startup" )
6868async def startup ():
@@ -71,14 +71,17 @@ async def startup():
7171 await auth.create_role_user(' admin' )
7272 await auth.create_role_user(' vip' )
7373
74+
7475# 要求: 用户必须登录
7576@app.get (" /auth/get_user" )
7677@auth.requires ()
7778def get_user (request : Request):
7879 return request.user
7980
81+
8082if __name__ == ' __main__' :
8183 import uvicorn
84+
8285 uvicorn.run(app, debug = True )
8386
8487```
@@ -194,13 +197,15 @@ auth.backend.attach_middleware(app)
194197``` python
195198from fastapi_user_auth.auth.models import User
196199
197- async def get_request_user (request : Request)->Optional[User]:
200+
201+ async def get_request_user (request : Request) -> Optional[User]:
198202 if await auth.requires(' admin' , response = False )(request):
199203 return request.user
200204 else :
201205 return None
202206
203207```
208+
204209## Token存储后端
205210
206211` fastapi-user-auth ` 支持多种token存储方式.默认为: ` DbTokenStore ` , 建议自定义修改为: ` JwtTokenStore `
@@ -211,6 +216,7 @@ async def get_request_user(request: Request)->Optional[User]:
211216from fastapi_user_auth.auth.backends.jwt import JwtTokenStore
212217from sqlalchemy.ext.asyncio import create_async_engine
213218from fastapi_amis_admin.utils.db import SqlalchemyAsyncClient
219+
214220# 创建异步数据库引擎
215221engine = create_async_engine(url = ' sqlite+aiosqlite:///admisadmin.db' , future = True )
216222# 使用`JwtTokenStore`创建auth对象
@@ -228,6 +234,7 @@ site = AuthAdminSite(settings=Settings(database_url_async='sqlite+aiosqlite:///a
228234``` python
229235# 使用`DbTokenStore`创建auth对象
230236from fastapi_user_auth.auth.backends.db import DbTokenStore
237+
231238auth = Auth(db = SqlalchemyAsyncClient(engine),
232239 token_store = DbTokenStore(db = SqlalchemyAsyncClient(engine)))
233240```
@@ -238,6 +245,7 @@ auth = Auth(db=SqlalchemyAsyncClient(engine),
238245# 使用`RedisTokenStore`创建auth对象
239246from fastapi_user_auth.auth.backends.redis import RedisTokenStore
240247from aioredis import Redis
248+
241249auth = Auth(db = SqlalchemyAsyncClient(engine),
242250 token_store = RedisTokenStore(redis = Redis.from_url(' redis://localhost?db=0' )))
243251```
@@ -256,30 +264,26 @@ flowchart LR
256264 Role -. m:n .-> Perimission
257265```
258266
259-
260-
261-
262267## 界面预览
263268
264269- Open ` http://127.0.0.1:8000/admin/auth/form/login ` in your browser:
265270
266- ![ Login] ( https://raw.githubusercontent.com/amisadmin/fastapi_amis_admin_demo/master/upload/img/fastapi-user-auth-login .png )
271+ ![ Login] ( https://s2.loli.net/2022/03/20/SZy6sjaVlBT8gin .png )
267272
268273- Open ` http://127.0.0.1:8000/admin/ ` in your browser:
269274
270- ![ Admin ] ( https://raw.githubusercontent.com/amisadmin/fastapi_amis_admin_demo/master/upload/img/fastapi-user-auth-admin .png )
275+ ![ ModelAdmin ] ( https://s2.loli.net/2022/03/20/ItgFYGUONm1jCz5 .png )
271276
272277- Open ` http://127.0.0.1:8000/admin/docs ` in your browser:
273278
274- ![ Docs] ( https://raw.githubusercontent.com/amisadmin/fastapi_amis_admin_demo/master/upload/img/fastapi-user-auth-docs .png )
279+ ![ Docs] ( https://s2.loli.net/2022/03/20/1GcCiPdmXayxrbH .png )
275280
276281## 未来计划
277282
278283- [ ] bug修复,细节完善.
279284- [ ] 完善用户教程文档.
280285- [ ] 不断拓展与完善核心功能.
281286
282-
283287## 许可协议
284288
285289- ` fastapi-amis-admin ` 基于` Apache2.0 ` 开源免费使用,可以免费用于商业用途,但请在展示界面中明确显示关于FastAPI-Amis-Admin的版权信息.
0 commit comments