Skip to content

Instantly share code, notes, and snippets.

@JMSwag
Forked from jbolda/Authomatic and Flask-Login
Created February 16, 2016 05:13
Show Gist options
  • Save JMSwag/f3435a7b9ddfc4508c4e to your computer and use it in GitHub Desktop.
Save JMSwag/f3435a7b9ddfc4508c4e to your computer and use it in GitHub Desktop.
@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)
if authomatic.result.user:
if not (authomatic.result.user.name and authomatic.result.user.id):
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_valid_nickname(nickname)
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)
flash('You are now logged in!')
return render_template('result.html', result=authomatic.result)
return authomatic.response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment