|
1 | 1 | from quart import Quart, render_template, request, session
|
2 |
| -from .oauth import Oauth |
3 |
| - |
| 2 | +from quart_discord import DiscordOAuth2Session |
4 | 3 |
|
5 | 4 | app = Quart(__name__)
|
6 | 5 | app.config["SECRET_KEY"] = "test123"
|
| 6 | +app.config["DISCORD_CLIENT_ID"] = 1234567890 # Discord client ID. |
| 7 | +app.config["DISCORD_CLIENT_SECRET"] = "SHHHH" # Discord client secret. |
| 8 | +app.config["DISCORD_REDIRECT_URI"] = "" # URL to your callback endpoint. |
| 9 | + |
| 10 | +discord = DiscordOAuth2Session(app) |
7 | 11 |
|
8 | 12 | @app.route("/")
|
9 | 13 | async def home():
|
10 |
| - return await render_template("index.html",discord_url= Oauth.discord_login_url) |
| 14 | + return await render_template("index.html",discord_url="login url here") |
11 | 15 |
|
12 | 16 | @app.route("/login")
|
13 | 17 | async def login():
|
14 |
| - code = request.args.get("code") |
15 |
| - |
16 |
| - at = Oauth.get_access_token(code) |
17 |
| - session["token"] = at |
18 |
| - |
19 |
| - user = Oauth.get_user_json(at) |
20 |
| - user_name, user_id = user.get("username"), user.get("discriminator") |
21 |
| - |
22 |
| - return f"Success, logged in as {user_name}#{user_id}" |
23 |
| - |
| 18 | + """The function that logs the user in. |
| 19 | +
|
| 20 | + It will redirect them to your Discord OAuth2 Flow, |
| 21 | + and they will then be redirected back to your callback |
| 22 | + endpoint, or REDIRECT_URI. |
| 23 | + """ |
| 24 | + return await discord.create_session() |
| 25 | + |
| 26 | +@app.route("/callback") |
| 27 | +async def callback(): |
| 28 | + """Callback. |
| 29 | +
|
| 30 | + This will handle the authentication of |
| 31 | + the user, and create the session and cookies. |
| 32 | + """ |
| 33 | + await discord.callback() |
| 34 | + user = await discord.fetch_user() |
| 35 | + return f"Hello, {user.name}#{user.discriminator}! <img src='{user.avatar_url}'>" |
| 36 | + |
| 37 | +@app.route('/logout') |
| 38 | +async def logout(): |
| 39 | + """Logs out the user by REVOKING!!! their token.""" |
| 40 | + discord.revoke() |
24 | 41 |
|
25 | 42 | if __name__ == "__main__":
|
26 | 43 | app.run(debug=True)
|
0 commit comments