weibeu / Flask-Discord

Discord OAuth2 extension for Flask. An Easier implementation of "Log In With Discord".
https://flask-discord.readthedocs.io/en/latest/
MIT License
182 stars 47 forks source link

Blueprint Support #64

Open Artucuno opened 2 years ago

Artucuno commented 2 years ago

Hey, I need to use Flask-Discord in a blueprint but I am not sure if that is possible.

I get the error AttributeError: 'Flask' object has no attribute 'discord' and I think its because it uses current_app instead of the blueprint itself.

Thanks

xybl3 commented 2 years ago

Hello, I had the same problem with quart_discord. All you need to do is move

discord = DiscordOAuth2Session(app)

to your main file, and then import discord from main in your blueprint file. Also to prevent circular imports remember to wrap import of blueprint and app.run() in if statement, something like that

if __name__ == "__main__":
    from blueprints.api import api
    from blueprints.dashboard import dashboard

    app.register_blueprint(api)
    app.register_blueprint(dashboard)

    app.run(host="localhost", port=5000, debug=True)

For me it works perfectly

Your blueprint should look like that

...
from flask.blueprints import Blueprint
from main import app, discord

dashboard = Blueprint('dashboard', __name__, url_prefix='/dashboard', template_folder="../templates/dashboard/")
...

And your main

app = Flask(__name__)

...

discord = DiscordOAuth2Session(app)

if __name__ == "__main__":
    from blueprints.api import api
    from blueprints.dashboard import dashboard

    app.register_blueprint(api)
    app.register_blueprint(dashboard)

    app.run(host="localhost", port=5000, debug=True)
Artucuno commented 2 years ago

Hey, thanks for your reply. I fixed the issue in the end by just using oauthlib and doing it from scratch just because for my case, I can't have discord imported in the main file.