from flask import render_template, redirect, jsonify, session
from flask_login import login_user

from app.main import main
from app.main.dao import users_dao, verify_codes_dao
from app.main.forms import TwoFactorForm


@main.route("/two-factor", methods=['GET'])
def render_two_factor():
    return render_template('views/two-factor.html', form=TwoFactorForm())


@main.route('/two-factor', methods=['POST'])
def process_two_factor():
    form = TwoFactorForm()

    if form.validate_on_submit():
        user = users_dao.get_user_by_id(session['user_id'])
        verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='sms')
        login_user(user)
        return redirect('/dashboard')
    else:
        return jsonify(form.errors), 400

import traceback

from flask import render_template, redirect, jsonify, session
from flask_login import login_user

from app.main import main
from app.main.dao import users_dao, verify_codes_dao
from app.main.forms import TwoFactorForm


@main.route("/two-factor", methods=['GET'])
def render_two_factor():
    return render_template('views/two-factor.html', form=TwoFactorForm())


@main.route('/two-factor', methods=['POST'])
def process_two_factor():
    try:
        form = TwoFactorForm()

        if form.validate_on_submit():
            user = users_dao.get_user_by_id(session['user_id'])
            verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='sms')
            login_user(user)
            return redirect('/dashboard')
        else:
            return jsonify(form.errors), 400
    except:
        traceback.print_exc()
Add debugging to find issue.
Add debugging to find issue.

+ import traceback
+ 
  from flask import render_template, redirect, jsonify, session
  from flask_login import login_user
  
  from app.main import main
  from app.main.dao import users_dao, verify_codes_dao
  from app.main.forms import TwoFactorForm
  
  
  @main.route("/two-factor", methods=['GET'])
  def render_two_factor():
      return render_template('views/two-factor.html', form=TwoFactorForm())
  
  
  @main.route('/two-factor', methods=['POST'])
  def process_two_factor():
+     try:
-     form = TwoFactorForm()
+         form = TwoFactorForm()
  
-     if form.validate_on_submit():
+         if form.validate_on_submit():
-         user = users_dao.get_user_by_id(session['user_id'])
+             user = users_dao.get_user_by_id(session['user_id'])
-         verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='sms')
+             verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='sms')
-         login_user(user)
+             login_user(user)
-         return redirect('/dashboard')
+             return redirect('/dashboard')
-     else:
+         else:
-         return jsonify(form.errors), 400
+             return jsonify(form.errors), 400
- 
+     except:
+         traceback.print_exc()
Add debugging to find issue.
## Code Before:
from flask import render_template, redirect, jsonify, session
from flask_login import login_user

from app.main import main
from app.main.dao import users_dao, verify_codes_dao
from app.main.forms import TwoFactorForm


@main.route("/two-factor", methods=['GET'])
def render_two_factor():
    return render_template('views/two-factor.html', form=TwoFactorForm())


@main.route('/two-factor', methods=['POST'])
def process_two_factor():
    form = TwoFactorForm()

    if form.validate_on_submit():
        user = users_dao.get_user_by_id(session['user_id'])
        verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='sms')
        login_user(user)
        return redirect('/dashboard')
    else:
        return jsonify(form.errors), 400

## Instruction:
Add debugging to find issue.
## Code After:
import traceback

from flask import render_template, redirect, jsonify, session
from flask_login import login_user

from app.main import main
from app.main.dao import users_dao, verify_codes_dao
from app.main.forms import TwoFactorForm


@main.route("/two-factor", methods=['GET'])
def render_two_factor():
    return render_template('views/two-factor.html', form=TwoFactorForm())


@main.route('/two-factor', methods=['POST'])
def process_two_factor():
    try:
        form = TwoFactorForm()

        if form.validate_on_submit():
            user = users_dao.get_user_by_id(session['user_id'])
            verify_codes_dao.use_code_for_user_and_type(user_id=user.id, code_type='sms')
            login_user(user)
            return redirect('/dashboard')
        else:
            return jsonify(form.errors), 400
    except:
        traceback.print_exc()