handle errors w/ twitter when logging in

This commit is contained in:
codl 2017-08-08 16:18:39 +02:00
parent 1039da665b
commit 8a8d2cb0d3
No known key found for this signature in database
GPG Key ID: 6CD7C8891ED1233A
2 changed files with 32 additions and 19 deletions

View File

@ -36,33 +36,40 @@ def index():
settings_error = 'settings_error' in request.args settings_error = 'settings_error' in request.args
) )
else: else:
return render_template('index.html') return render_template('index.html',
twitter_login_error = 'twitter_login_error' in request.args)
@app.route('/login/twitter') @app.route('/login/twitter')
def twitter_login_step1(): def twitter_login_step1():
return redirect(lib.twitter.get_login_url( try:
callback = url_for('twitter_login_step2', _external=True), return redirect(lib.twitter.get_login_url(
**app.config.get_namespace("TWITTER_") callback = url_for('twitter_login_step2', _external=True),
)) **app.config.get_namespace("TWITTER_")
))
except (TwitterError, URLError):
return redirect(url_for('index', twitter_login_error='', _anchor='log_in'))
@app.route('/login/twitter/callback') @app.route('/login/twitter/callback')
def twitter_login_step2(): def twitter_login_step2():
oauth_token = request.args['oauth_token'] try:
oauth_verifier = request.args['oauth_verifier'] oauth_token = request.args['oauth_token']
token = lib.twitter.receive_verifier(oauth_token, oauth_verifier, **app.config.get_namespace("TWITTER_")) oauth_verifier = request.args['oauth_verifier']
token = lib.twitter.receive_verifier(oauth_token, oauth_verifier, **app.config.get_namespace("TWITTER_"))
session = Session(account_id = token.account_id) session = Session(account_id = token.account_id)
db.session.add(session) db.session.add(session)
db.session.commit() db.session.commit()
tasks.fetch_acc.s(token.account_id).apply_async(routing_key='high') tasks.fetch_acc.s(token.account_id).apply_async(routing_key='high')
resp = Response(status=302, headers={"location": url_for('index')}) resp = Response(status=302, headers={"location": url_for('index')})
resp.set_cookie('forget_sid', session.id, resp.set_cookie('forget_sid', session.id,
max_age=60*60*48, max_age=60*60*48,
httponly=True, httponly=True,
secure=app.config.get("HTTPS")) secure=app.config.get("HTTPS"))
return resp return resp
except (TwitterError, URLError):
return redirect(url_for('index', twitter_login_error='', _anchor='log_in'))
@app.route('/upload_tweet_archive', methods=('POST',)) @app.route('/upload_tweet_archive', methods=('POST',))
@require_auth @require_auth

View File

@ -20,7 +20,13 @@
</section> </section>
<section> <section>
<h2>Sign in</h2> <h2 id="sign_in">Sign in</h2>
{% if twitter_login_error %}
<div class="banner error">
Sorry, something went wrong when communicating with Twitter. Give it another shot, maybe?
</div>
{% endif %}
<p>Sound good to you? <a href="/login/twitter">Log in with Twitter</a></p> <p>Sound good to you? <a href="/login/twitter">Log in with Twitter</a></p>
</section> </section>
<section class='policy'> <section class='policy'>