From ac34b7e3b392b99e6a4db403776e868c57ec0595 Mon Sep 17 00:00:00 2001 From: Mikey Date: Tue, 9 Feb 2021 10:05:54 -0500 Subject: [PATCH 1/2] Made the switch to quart, an async version of flask --- Part 1/main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Part 1/main.py b/Part 1/main.py index 6168f83..a836c6a 100644 --- a/Part 1/main.py +++ b/Part 1/main.py @@ -1,16 +1,16 @@ -from flask import Flask, render_template, request, session -from oauth import Oauth +from quart import Quart, render_template, request, session +from .oauth import Oauth -app = Flask(__name__) +app = Quart(__name__) app.config["SECRET_KEY"] = "test123" @app.route("/") -def home(): - return render_template("index.html",discord_url= Oauth.discord_login_url) +async def home(): + return await render_template("index.html",discord_url= Oauth.discord_login_url) @app.route("/login") -def login(): +async def login(): code = request.args.get("code") at = Oauth.get_access_token(code) From f5e1d8d3ff65fd136fe953088f99e8815fd57e40 Mon Sep 17 00:00:00 2001 From: Mikey Date: Tue, 9 Feb 2021 10:28:49 -0500 Subject: [PATCH 2/2] Removed OAuth import --- Part 1/main.py | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/Part 1/main.py b/Part 1/main.py index a836c6a..7b8b581 100644 --- a/Part 1/main.py +++ b/Part 1/main.py @@ -1,26 +1,43 @@ from quart import Quart, render_template, request, session -from .oauth import Oauth - +from quart_discord import DiscordOAuth2Session app = Quart(__name__) app.config["SECRET_KEY"] = "test123" +app.config["DISCORD_CLIENT_ID"] = 1234567890 # Discord client ID. +app.config["DISCORD_CLIENT_SECRET"] = "SHHHH" # Discord client secret. +app.config["DISCORD_REDIRECT_URI"] = "" # URL to your callback endpoint. + +discord = DiscordOAuth2Session(app) @app.route("/") async def home(): - return await render_template("index.html",discord_url= Oauth.discord_login_url) + return await render_template("index.html",discord_url="login url here") @app.route("/login") async def login(): - code = request.args.get("code") - - at = Oauth.get_access_token(code) - session["token"] = at - - user = Oauth.get_user_json(at) - user_name, user_id = user.get("username"), user.get("discriminator") - - return f"Success, logged in as {user_name}#{user_id}" - + """The function that logs the user in. + + It will redirect them to your Discord OAuth2 Flow, + and they will then be redirected back to your callback + endpoint, or REDIRECT_URI. + """ + return await discord.create_session() + +@app.route("/callback") +async def callback(): + """Callback. + + This will handle the authentication of + the user, and create the session and cookies. + """ + await discord.callback() + user = await discord.fetch_user() + return f"Hello, {user.name}#{user.discriminator}! " + +@app.route('/logout') +async def logout(): + """Logs out the user by REVOKING!!! their token.""" + discord.revoke() if __name__ == "__main__": app.run(debug=True)