Files
notifications-admin/app/main/views/sign_in.py
T

73 lines
2.2 KiB
Python
Raw Normal View History

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
from app import login_manager
from app.main import main
from app.main.forms import LoginForm
from app.models.user import InvitedUser, User
from app.utils import hide_from_search_engines
2016-01-05 14:30:06 +00:00
@main.route('/sign-in', methods=(['GET', 'POST']))
@hide_from_search_engines
2016-01-05 14:30:06 +00:00
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
form = LoginForm()
if form.validate_on_submit():
user = User.from_email_address_and_password_or_none(
form.email_address.data, form.password.data
)
if user and user.state == 'pending':
return redirect(url_for('main.resend_email_verification'))
if user and session.get('invited_user'):
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.")
session.pop('invited_user', None)
abort(403)
else:
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'))
# 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."
2020-02-24 11:59:07 +00:00
" <a href={password_reset}>Forgotten 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
)
@login_manager.unauthorized_handler
def sign_in_again():
return redirect(
url_for('main.sign_in', next=request.path)
)