Connecting via a flask factory

Hi all,

New to the forums and to Auth0, but it looks perfect for our needs.

I’m struggling when trying to integrate with our Flask application, as the documents assume that you’ve got a very basic single-file application as opposed to a nested setup using Blueprints.

I’m configuring my application using the factory approach, however no matter what I try I can’t seem to get the factory to initialise.

My understanding is that I should be able to do something like:

sess = Session()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    CORS(app)
    oauth = OAuth(app)

    auth0 = oauth.register(
        'auth0',
        client_id='YOUR_CLIENT_ID',
        client_secret='YOUR_CLIENT_SECRET',
        api_base_url='https://YOUR_DOMAIN',
        access_token_url='https://YOUR_DOMAIN/oauth/token',
        authorize_url='https://YOUR_DOMAIN/authorize',
        client_kwargs={
             'scope': 'openid profile email',
        },
    )

    return app

and then reference app.auth0 throughout the rest of my application, however this seems to throw an error about application context regardless of how I set it up.

I’ve switched over to using flask-dance instead and authentication works fine, but for some reason I can’t get email addresses etc. back via the scope:

from flask import Flask, current_app
from flask_cors import CORS
from flask_session import Session
from flask_dance.consumer import OAuth2ConsumerBlueprint
from werkzeug.middleware.proxy_fix import ProxyFix

from config import Config

from app.dashboard import bp as dash

sess = Session()

def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    app.wsgi_app = ProxyFix(app.wsgi_app)

    CORS(app)
    sess.init_app(app)

    auth0 = OAuth2ConsumerBlueprint(
        'auth0', __name__,
        client_id=app.config["AUTH0_CLIENT_ID"],
        client_secret=app.config["AUTH0_CLIENT_SECRET"],
        base_url="https://%s/" % app.config["AUTH0_CLIENT_DOMAIN"],
        token_url="https://%s/oauth/token" % app.config["AUTH0_CLIENT_DOMAIN"],
        authorization_url="https://%s/authorize" % app.config["AUTH0_CLIENT_DOMAIN"],
        scope = ["openid", "email", "profile"]
    )
    # Blueprints
    app.register_blueprint(dash)
    app.register_blueprint(auth0, url_prefix="/login")

    return app

Am I the only one working in this way?! :slight_smile: