linuxrouter/docker/routlin-dash/app/pages/accountlogin/action.py

48 lines
1.5 KiB
Python
Raw Normal View History

2026-05-27 22:04:04 -04:00
from pathlib import Path
2026-05-17 03:26:01 -04:00
from flask import Blueprint, request, session, redirect, flash
2026-06-10 14:23:47 -04:00
import bcrypt
2026-06-07 00:21:08 -04:00
import auth
import config_utils
2026-05-17 03:26:01 -04:00
import sanitize
2026-06-10 14:23:47 -04:00
import settings
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
_PAGE = Path(__file__).parent.name
bp = Blueprint(_PAGE, __name__)
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
@bp.route('/action/accountlogin/form_login', methods=['POST'])
2026-06-07 00:21:08 -04:00
@auth.require_level('nothing')
2026-05-27 22:04:04 -04:00
def form_login():
2026-05-17 03:26:01 -04:00
if session.get('access_level', 'nothing') != 'nothing':
2026-05-27 22:04:04 -04:00
return redirect('/overview')
2026-05-17 03:26:01 -04:00
email = sanitize.email(request.form.get('email', ''))
password = request.form.get('password', '')
if not email or not password:
flash('Email address and password are required.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-06-10 14:23:47 -04:00
account = config_utils.get_account_by_email(email)
2026-05-17 03:26:01 -04:00
if account is None:
flash('Email address not recognised.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
if not account.get('hashed_password'):
flash('Account setup is not complete. Please use Create Account to set your password first.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
2026-06-10 14:23:47 -04:00
if not bcrypt.checkpw(password.encode('utf-8'), account['hashed_password'].encode('utf-8')):
2026-05-17 03:26:01 -04:00
flash('Invalid email address or password.', 'error')
2026-05-27 22:04:04 -04:00
return redirect(f'/{_PAGE}')
2026-05-17 03:26:01 -04:00
session.clear()
2026-06-10 14:23:47 -04:00
session['account_id'] = account['account_id']
session['tz_offset_seconds'] = settings.get_host_utc_offset()
2026-05-25 13:49:23 -04:00
session['apply_changes_immediately'] = False
2026-06-10 14:23:47 -04:00
session.permanent = True
2026-05-17 03:26:01 -04:00
2026-05-27 22:04:04 -04:00
return redirect('/overview')