Skip to content

Commit 869b8bc

Browse files
committed
GH-4: Rearrange the config and router
1 parent 42308d0 commit 869b8bc

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

demo/__init__.py

Whitespace-only changes.

examples/demonstration/config.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
3+
from dotenv import load_dotenv
4+
from social_core.backends.github import GithubOAuth2
5+
6+
from fastapi_oauth2.client import OAuth2Client
7+
from fastapi_oauth2.config import OAuth2Config
8+
9+
load_dotenv()
10+
11+
oauth2_config = OAuth2Config(
12+
allow_http=True,
13+
jwt_secret=os.getenv("JWT_SECRET"),
14+
jwt_expires=os.getenv("JWT_EXPIRES"),
15+
jwt_algorithm=os.getenv("JWT_ALGORITHM"),
16+
clients=[
17+
OAuth2Client(
18+
backend=GithubOAuth2,
19+
client_id=os.getenv("OAUTH2_CLIENT_ID"),
20+
client_secret=os.getenv("OAUTH2_CLIENT_SECRET"),
21+
# redirect_uri="http://127.0.0.1:8000/",
22+
scope=["user:email"],
23+
),
24+
]
25+
)

examples/demonstration/router.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json
2+
3+
from fastapi import Depends
4+
from fastapi import Request, APIRouter
5+
from fastapi.responses import HTMLResponse
6+
from fastapi.security import OAuth2
7+
from fastapi.templating import Jinja2Templates
8+
9+
oauth2 = OAuth2()
10+
router = APIRouter()
11+
templates = Jinja2Templates(directory="templates")
12+
13+
14+
@router.get("/", response_class=HTMLResponse)
15+
async def root(request: Request):
16+
return templates.TemplateResponse("index.html", {"request": request, "user": request.user, "json": json})
17+
18+
19+
@router.get("/user")
20+
def user(request: Request, _: str = Depends(oauth2)):
21+
return request.user

0 commit comments

Comments
 (0)