Code: Select all
oauth = OAuth()
oauth.register(
name='discord',
client_id=os.getenv("DISCORD_CLIENT_ID"),
client_secret=os.getenv("DISCORD_CLIENT_SECRET"),
authorize_url='https://discord.com/api/oauth2/authorize',
access_token_url='https://discord.com/api/oauth2/token',
api_base_url='https://discord.com/api/',
client_kwargs={'scope': 'identify email'},
state=None
)
@app.get("/auth/discord")
async def login_with_discord(request: Request):
redirect_uri = os.getenv("DISCORD_REDIRECT_URI")
return await oauth.discord.authorize_redirect(request, redirect_uri, state=None)
@app.get("/auth/discord/callback")
async def discord_callback(request: Request):
try:
token = await oauth.discord.authorize_access_token(request, state=None)
except Exception as e:
print(f"/auth/discord/callback Error: {e}")
Code: Select all
document.addEventListener("DOMContentLoaded", () => {
const discordLoginButton = document.getElementById("discord-login");
discordLoginButton.addEventListener("click", () => {
// Redirect to the backend's Discord OAuth login endpoint
const { shell } = require('electron');
shell.openExternal("http://127.0.0.1:8000/auth/discord");
});
});