3

I am trying to make a call to an outside API that authenticates the user to some domain and returns an object B. This B object should be accessible across multiple view functions, but I don't want to instantiate it every request in before_request or store the auth information in the session or database because it is sensitive.

Based on this SO post I thought I might be able to use the g object, but I get AttributeError: '_AppCtxGlobals' object has no attribute 'B' when I try to access the object in another function.

The only thing that has worked so far is initializing B as a global variable, but that doesn't seem like good practice or secure.

How can I create and store this object so that it is accessible across multiple view functions?

@app.route('/login', methods=['GET', 'POST'])
def login():
    if g.user is not None and g.user.is_authenticated():
        return redirect(url_for('index'))

    form = LoginForm()

    if form.validate_on_submit():
        email = form.email.data
        pw = form.password.data
        g.B = Bindings(domain='somewebsite.com', user=email, auth=pw)

    return render_template('login.html', form=form)

@app.route('/devices')
def devices():
    devices = g.B.get_all_devices()
    return render_template('home/devices.html', devices=devices)
Community
  • 1
  • 1

0 Answers0