question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble in use with Flask-Login

See original GitHub issue

As far as I can tell, Authomatic doesn’t handle the login/logout, but rather just the initial authorization. So I was presuming to use Flask-Login (+ Flask + SQLAlchemy) to handle this. I finally got it working after some initial issues with SSL (see my other question), and was able to push the creds into the database. The next step was to get the login working, so I was referencing this previous question here: https://github.com/peterhudec/authomatic/issues/1#issuecomment-48904643 . I ended up adding some complexity to it to give better error messages, but between this two things I broke it somehow. Below is my current code in all its glory. I have tried commenting everything out to discover the root of the problem, but I also just get “something went wrong captain”. Side note: is there a good way to debug this? I have it on heroku currently, but I can’t find logs or error messages anywhere.

logger = logging.getLogger('authomatic.core')
logger.addHandler(logging.StreamHandler())
authomatic = FlaskAuthomatic(config=AUTH_CONFIG, secret=SECRET_KEY, debug=True)

@app.route('/login/<provider_name>', methods=['GET', 'POST'])
@authomatic.login('g')
@requires_ssl
def login(provider_name):
    if g.user is not None and g.user.is_authenticated():
        return redirect(url_for('index'))
    if authomatic.result:
        if authomatic.result.error:
            return 'Something went wrong: {0}'.format(authomatic.result.error.message)
        elif authomatic.result.user:
            if not (authomatic.result.user.name and authomatic.result.user.id):
                update_response = authomatic.result.user.update()
                while update_response.status/100 not in [4, 5]:
                    update_response = authomatic.result.user.update()

                if update_response.status/100 in [4, 5]:
                    return 'Response status: {}'.format(authomatic.response.status)
            user = User.query.filter_by(email=authomatic.result.user.email).first()
            if user is None:
                nickname = authomatic.result.user.name
                if nickname is None or nickname == "":
                    nickname = authomatic.result.user.email.split('@')[0]
                nickname = User.make_unique_nickname(nickname)
                role = ROLE_USER
                if authomatic.result.user.email in PRESET_ADMINS:
                    role = ROLE_SUPERADMIN
                user = User(nickname=authomatic.result.user.name,
                            email=authomatic.result.user.email,
                            about_me=authomatic.result.user.id,
                            role=role,
                            join=datetime.utcnow(),
                            last_seen=datetime.utcnow())
                db.session.add(user)
                db.session.commit()
            g.user = user
            login_user(g.user, remember=True)
            return render_template('result.html', result=authomatic.result)
    return authomatic.response

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
jboldacommented, Oct 4, 2014

Here is a gist of my working code snippet. I have been busy, but I plan to make a pull request to add a working example using Flask, SQLA, and Flask-Login to the docs

https://gist.github.com/jbolda/b7d344f1f0d1a68f7be8

0reactions
webmavencommented, Oct 4, 2014

@jbolda A working example of Authomatic + Flask-Login would be most useful to me. Did you actually get it to work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Flask-login not working as expected - Stack Overflow
I tried your code and it does work for me (with changes) - you must have done something wrong outside what you have...
Read more >
Using Flask-Login for User Management with Flask
The function assumes, due to the decorator, the user has already been authenticated and is thus authorized to see this data. There's only...
Read more >
Problems with trying to implement flask-login into my multi ...
Page 1 has a nav menu along the left side. I would like to be able to show a different menu based on...
Read more >
Part One: The Flask Authentication Problem - Stormpath
Force Your Users to Use Social Login. One of the most popular authentication strategies in the Flask community is to force your users...
Read more >
Flask User Accounts & Authentication in with Flask-Login
Create an interactive Flask application by supporting user accounts! Handle account creation, log-ins, walled content, and user-specific features.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found