Search code examples
pythonmongodbflaskmongoengineflask-security

How to confirm user created by create_user in flask security mongoengine app?


I have a python flask app using mongoengine and flask-security built up from the examples to expose all of the confirmation, registration, tracking, and recovery functionality.

Everything works except that a user created imperatively in the code using:

MongoEngineUserDatastore.create_user(...)

cannot login. That is, when you try to login with this user, you get an error message:

"Email requires confirmation"

Since the email with a hashed URL has not been sent, there is no way to confirm. Is there a parameter I can pass somewhere to confirm this user on creation or set the confirmed flag somewhere?

Here's my code:


Solution

  • I figured it out by confirming a newly registered user and examining mongodb to see what fields were added. Turns out that the required field for confirmation is confirmed_at, which must have a datetime so:

    import datetime
    
    # Create a user to test with
    @app.before_first_request
    def create_user():
        user_datastore.create_user(
            email='me@mydomain.com',
            password=utils.encrypt_password('password'),
            confirmed_at=datetime.datetime.now())
    

    I've updated the gist here:

    https://gist.github.com/davidthewatson/327776905ef30815c138