diff --git a/Part 1/main.py b/Part 1/main.py index 6168f83..7b8b581 100644 --- a/Part 1/main.py +++ b/Part 1/main.py @@ -1,26 +1,43 @@ -from flask import Flask, render_template, request, session -from oauth import Oauth +from quart import Quart, render_template, request, session +from quart_discord import DiscordOAuth2Session - -app = Flask(__name__) +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("/") -def home(): - return render_template("index.html",discord_url= Oauth.discord_login_url) +async def home(): + return await render_template("index.html",discord_url="login url here") @app.route("/login") -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}" - +async def login(): + """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)