[Python] Flask application: Forgot Password route is not working

Discussão em 'Python' iniciado por Stack, Setembro 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am creating an authentication system and i want to send reset request using MailerSend API, the code has no errors. The entry point for the application is in app.py file. The structure of the data is correct also. The project folder includes login.html and login.js which holds both the login and the forgot password pages. Entry point for the application is on app.py

    The code doesn't even log the console out with print() used to

    authentication.py

    import secrets
    import bcrypt
    import sqlite3
    import datetime
    from flask import Blueprint, jsonify, request,session
    from mailersend import MailerSendClient, EmailBuilder
    import secrets

    auth_bp = Blueprint('auth', __name__, url_prefix='/api/auth')

    # creatin mail sender client
    mailer = MailerSendClient(api_key='mlsn.cc7po0a13bghjgjg061960a92b85e0676e762384ba8097993cc02ac8783052849baba75d')

    def get_db_connection():
    # Get database connection to the cyber-shield-linkguard database
    conn = sqlite3.connect('cyber-shield-linkguard.db')

    # return dictionary data structure from the columns of the database
    # e.g instead of column id data[2] use data['password']
    conn.row_factory = sqlite3.Row
    return conn

    @auth_bp.route('/forgot-password', methods=['POST'])
    def forgot_password():
    try:
    data = request.get_json()
    email = data.get('email', '').strip().lower()

    if not email:
    print('No email provided')
    return jsonify({'error': 'Email is required'}), 400
    try:
    conn = get_db_connection()
    cursor = conn.cursor()

    # Check if user exists
    cursor.execute('SELECT id FROM users WHERE email = ?', (email,))
    user = cursor.fetchone()
    print(f'Entries found: {user}')

    if not user:

    conn.close()
    print('Email not found in database')
    return jsonify({'error': 'Email not found'}), 404

    #generate token
    token = secrets.token_urlsafe(32)
    print(f'Generated token: {token}')

    cursor.execute('''INSERT INTO password_reset_tokens (email, token, is_used) VALUES (?, ?, 0)''', (email, token))

    conn.commit()
    conn.close()

    reset_link = f'http://localhost:5000/reset-password?token={token}'
    print(f'Reset link: {reset_link}')


    mail_msg = EmailBuilder().from_email("test@test-ywj2lpnwmmqg7oqz.mlsender.net","theArchive").to_many([{"email":email}]).subject("Password Reset Request").html(f"<p>Click <a href='{reset_link}'>here</a> to reset your password.</p>").text(f"Use the following link to reset your password: {reset_link}").build()

    try:
    response = mailer.emails.send(mail_msg)
    print(f'Email sent successsfully: {response.json()}')

    print(f'Password reset link sent to {email}: {reset_link}')
    return jsonify({'message': 'Password reset link sent'}), 200
    except Exception as e:
    print(f'Error sending email: {e}')
    return jsonify({'error': str(e)}), 500



    except sqlite3.Error as e:
    print(f"Database error: {e}")
    return jsonify({'error': 'Database error occurred'}), 500
    except Exception as e:
    print(f"Forgot password error: {e}")
    return jsonify({'error': 'Failed to process request'}), 500

    Continue reading...

Compartilhe esta Página