-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
A basic FastAPI app that demonstrates "login with Discord", returning the user who was logged in. | ||
""" | ||
|
||
import uvicorn | ||
from fastapi import FastAPI | ||
from starlette_discord.client import DiscordOAuthClient | ||
|
||
|
||
CLIENT_ID = 'YOUR_CLIENT_ID' | ||
CLIENT_SECRET = 'YOUR_CLIENT_SECRET' | ||
REDIRECT_URI = 'YOUR_REDIRECT_URI' | ||
|
||
|
||
app = FastAPI() | ||
client = DiscordOAuthClient(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI) | ||
|
||
|
||
@app.get('/login') | ||
async def login_with_discord(): | ||
return client.redirect() | ||
|
||
|
||
# NOTE: REDIRECT_URI should be this path. | ||
@app.get('/callback') | ||
async def callback(code: str): | ||
user = await client.login(code) | ||
return user | ||
|
||
|
||
uvicorn.run(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
A basic Starlette app that demonstrates "login with Discord", returning the user who was logged in. | ||
""" | ||
|
||
import uvicorn | ||
from starlette.applications import Starlette | ||
from starlette.responses import JSONResponse | ||
from starlette_discord.client import DiscordOAuthClient | ||
|
||
|
||
CLIENT_ID = 'YOUR_CLIENT_ID' | ||
CLIENT_SECRET = 'YOUR_CLIENT_SECRET' | ||
REDIRECT_URI = 'YOUR_REDIRECT_URI' | ||
|
||
|
||
app = Starlette() | ||
client = DiscordOAuthClient(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI) | ||
|
||
|
||
@app.route('/login') | ||
async def login_with_discord(_): | ||
return client.redirect() | ||
|
||
|
||
# NOTE: REDIRECT_URI should be this path. | ||
@app.route('/callback') | ||
async def callback(request): | ||
code = request.query_params['code'] | ||
u = await client.login(code) | ||
return JSONResponse(u) | ||
|
||
|
||
uvicorn.run(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
A basic FastAPI app that demonstrates "login with Discord". | ||
It also saves the user's login session, so any subsequent visits to /dash | ||
will return the same data without requiring re-authorization. | ||
""" | ||
|
||
import secrets | ||
import uvicorn | ||
from fastapi import FastAPI, HTTPException | ||
from starlette.requests import Request | ||
from starlette.responses import RedirectResponse | ||
from starlette.middleware.sessions import SessionMiddleware | ||
from starlette_discord.client import DiscordOAuthClient | ||
|
||
|
||
CLIENT_ID = 'YOUR_CLIENT_ID' | ||
CLIENT_SECRET = 'YOUR_CLIENT_SECRET' | ||
REDIRECT_URI = 'YOUR_REDIRECT_URI' | ||
|
||
|
||
app = FastAPI() | ||
client = DiscordOAuthClient(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI) | ||
|
||
|
||
@app.get('/login') | ||
async def login_with_discord(): | ||
return client.redirect() | ||
|
||
|
||
# NOTE: REDIRECT_URI should be this path. | ||
@app.get('/callback') | ||
async def callback(request: Request, code: str): | ||
user = await client.login(code) | ||
request.session['discord_user'] = user | ||
return RedirectResponse('/dash') | ||
|
||
|
||
@app.get('/dash') | ||
async def dash(request: Request): | ||
user = request.session.get('discord_user') | ||
if not user: | ||
raise HTTPException(401) | ||
return user | ||
|
||
|
||
app.add_middleware(SessionMiddleware, secret_key=secrets.token_urlsafe(64)) | ||
|
||
uvicorn.run(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.