2

I'm using Flask-Login, MongoEngine, OAuth with Rauth for Flask.

I need to save my user's session in MongoEngine but I don't know how to do it.

My code is that:

@app.route('/callback/<provider>')
def oauth_callback(provider):
    if not current_user.is_anonymous:
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email = oauth.callback()
    if social_id is None:
        flash('Authentication failed.')
        return redirect(url_for('index'))
    user = User.objects(social_id=social_id).first()
    if not user:
        user = User(social_id=social_id, nickname=username, email=email)
        user.save()
        db.session.add(user)
        db.session.commit()
    login_user(user, True)
    return redirect(url_for('index'))

In code, this lines db.session.add(user) and db.session.commit() is for SQL but I dont know how do it in MongoEngine.

Is there any way to do it in MongoEngine in a simple way?

Thanks!!

laurajaime
  • 379
  • 1
  • 3
  • 14
  • Did you find a solution to this? I have a very similar situation. – robster Dec 15 '20 at 01:02
  • @robster I solved it a long time ago but I used the flask session with `from flask import session` and the usage would be simply like `session[username] = username` for example. If you need more help don't hesitate to answer me. – laurajaime Dec 16 '20 at 19:24

1 Answers1

3

The line user.save() is doing the job. Can use current_user.save() depending on code.