Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble in use with Flask-Login #54

Closed
jbolda opened this issue Sep 11, 2014 · 5 comments
Closed

Trouble in use with Flask-Login #54

jbolda opened this issue Sep 11, 2014 · 5 comments

Comments

@jbolda
Copy link

jbolda commented Sep 11, 2014

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: #1 (comment) . 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
@jbolda
Copy link
Author

jbolda commented Sep 18, 2014

I seemed to have figured it out. I will likely add this as a gist or do a pull request and add it as an example.

@jbolda jbolda closed this as completed Sep 18, 2014
@DonDebonair
Copy link

@jbolda I'd be interested to know how you've done it, because I'm working on something similar right now, and also want to combine authomatic and Flask-Login :)

@spinningD20
Copy link

I too am interested in a good example to mix authomatic in with flask-login, which my app is already using...

@webmaven
Copy link

webmaven commented Oct 4, 2014

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

@jbolda
Copy link
Author

jbolda commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants