2016-01-05 17:08:50 +00:00
|
|
|
from flask import (
|
2018-02-20 11:22:17 +00:00
|
|
|
Markup,
|
2016-04-26 12:14:06 +01:00
|
|
|
abort,
|
2018-02-20 11:22:17 +00:00
|
|
|
flash,
|
|
|
|
|
redirect,
|
|
|
|
|
render_template,
|
|
|
|
|
request,
|
|
|
|
|
session,
|
|
|
|
|
url_for,
|
2016-01-21 11:33:53 +00:00
|
|
|
)
|
2018-02-20 11:22:17 +00:00
|
|
|
from flask_login import current_user
|
2016-01-21 11:33:53 +00:00
|
|
|
|
2019-05-23 15:27:35 +01:00
|
|
|
from app import login_manager
|
2015-11-27 09:47:29 +00:00
|
|
|
from app.main import main
|
2015-12-08 15:30:55 +00:00
|
|
|
from app.main.forms import LoginForm
|
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import InvitedUser, User
|
2015-11-27 09:47:29 +00:00
|
|
|
|
|
|
|
|
|
2016-01-05 14:30:06 +00:00
|
|
|
@main.route('/sign-in', methods=(['GET', 'POST']))
|
|
|
|
|
def sign_in():
|
2016-05-04 13:01:55 +01:00
|
|
|
if current_user and current_user.is_authenticated:
|
2018-11-15 15:38:43 +00:00
|
|
|
return redirect(url_for('main.show_accounts_or_dashboard'))
|
2016-02-23 15:45:19 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
form = LoginForm()
|
2017-12-06 20:24:25 +00:00
|
|
|
|
2016-01-27 12:22:32 +00:00
|
|
|
if form.validate_on_submit():
|
2016-03-30 16:16:34 +01:00
|
|
|
|
2019-05-23 15:27:35 +01:00
|
|
|
user = User.from_email_address_and_password_or_none(
|
|
|
|
|
form.email_address.data, form.password.data
|
|
|
|
|
)
|
|
|
|
|
|
2016-03-29 12:13:36 +01:00
|
|
|
if user and user.state == 'pending':
|
2016-09-06 15:44:33 +01:00
|
|
|
return redirect(url_for('main.resend_email_verification'))
|
2016-03-30 16:16:34 +01:00
|
|
|
|
|
|
|
|
if user and session.get('invited_user'):
|
2019-05-23 15:27:35 +01:00
|
|
|
invited_user = InvitedUser.from_session()
|
|
|
|
|
if user.email_address.lower() != invited_user.email_address.lower():
|
2019-09-13 09:44:04 +01:00
|
|
|
flash("You cannot accept an invite for another person.")
|
2016-03-30 16:16:34 +01:00
|
|
|
session.pop('invited_user', None)
|
|
|
|
|
abort(403)
|
|
|
|
|
else:
|
2019-05-23 15:27:35 +01:00
|
|
|
invited_user.accept_invite()
|
|
|
|
|
if user and user.sign_in():
|
|
|
|
|
if user.sms_auth:
|
|
|
|
|
return redirect(url_for('.two_factor', next=request.args.get('next')))
|
|
|
|
|
if user.email_auth:
|
|
|
|
|
return redirect(url_for('.two_factor_email_sent'))
|
2017-11-07 16:11:31 +00:00
|
|
|
|
2016-01-28 16:36:36 +00:00
|
|
|
# Vague error message for login in case of user not known, locked, inactive or password not verified
|
2017-10-18 14:51:26 +01:00
|
|
|
flash(Markup(
|
|
|
|
|
(
|
|
|
|
|
"The email address or password you entered is incorrect."
|
|
|
|
|
" <a href={password_reset}>Forgot your password</a>?"
|
2016-04-26 12:26:41 +01:00
|
|
|
).format(password_reset=url_for('.forgot_password'))
|
2016-04-26 12:14:06 +01:00
|
|
|
))
|
2016-01-05 14:30:06 +00:00
|
|
|
|
2017-02-17 14:06:09 +00:00
|
|
|
other_device = current_user.logged_in_elsewhere()
|
|
|
|
|
return render_template(
|
|
|
|
|
'views/signin.html',
|
|
|
|
|
form=form,
|
|
|
|
|
again=bool(request.args.get('next')),
|
|
|
|
|
other_device=other_device
|
|
|
|
|
)
|
2017-02-16 13:33:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@login_manager.unauthorized_handler
|
|
|
|
|
def sign_in_again():
|
|
|
|
|
return redirect(
|
|
|
|
|
url_for('main.sign_in', next=request.path)
|
|
|
|
|
)
|