Files
notifications-admin/app/main/views/two_factor.py
Chris Hill-Scott 939954cd64 Skip ‘choose service’ page if user has one service
We used to do this by redirecting on the choose service page. However when we
lost the dropdown and this page also became the page for adding a new service
(in 3617f2e936) the redirect was removed.

This commit re-adds the redirect on the two factor page, so that it only happens
on first login.

So the flows are:

**Multiple services**
```
`Sign in` → `Enter two factor code` → `Choose service` → `Service dashboard`
```

**One service**
```
`Sign in` → `Enter two factor code` → `Service dashboard`
```

**No services (you’ve deleted all your services)**
`Sign in` → `Enter two factor code` → `Choose service` → `Add new service`
2016-02-05 14:50:55 +00:00

39 lines
1.2 KiB
Python

from flask import (
render_template, redirect, jsonify, session, url_for)
from flask_login import login_user
from app.main import main
from app.main.dao import users_dao, services_dao
from app.main.forms import TwoFactorForm
@main.route('/two-factor', methods=['GET', 'POST'])
def two_factor():
# TODO handle user_email not in session
user_id = session['user_details']['id']
def _check_code(code):
return users_dao.check_verify_code(user_id, code, "sms")
form = TwoFactorForm(_check_code)
if form.validate_on_submit():
try:
user = users_dao.get_user_by_id(user_id)
services = services_dao.get_services(user_id).get('data', [])
# Check if coming from new password page
if 'password' in session['user_details']:
user.set_password(session['user_details']['password'])
users_dao.update_user(user)
login_user(user)
finally:
del session['user_details']
if (len(services) == 1):
return redirect(url_for('main.service_dashboard', service_id=services[0]['id']))
else:
return redirect(url_for('main.choose_service'))
return render_template('views/two-factor.html', form=form)