[Python] Python - Password reset through mail server

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to create a password reset feature in python using mail server. So the process is the same as every reset-password feature:

    1. User clicks reset
    2. User is asked to write the email that will be used to reset the password
    3. An email arriving in their inbox to click a link in the format of http://127.0.0.1:5000/reset/eyJ1c2VybmFtZSI6ImN. The token is refering to the exact email
    4. And when they click the link they are redirected to the reset page where they enter their new password 2 times before pressing submit.

    Where im having the problem is in the 4th step at the reset function where i receive the token and trying to update the database with the new credentials. the username = user_data['username'] successfully gets the email coming from the token.

    The problem is that the code doesn't continuing at the if statement to update the database. It just bypasses it and goes directly to return render_template('reset.html')

    @application.route('/reset/<token>', methods=['GET', 'POST'])
    def reset(token):
    try:
    print(f"Received token: {token}")
    print(f"Token type: {type(token)}")

    try:
    user_data = s.loads(token, salt='password-reset', max_age=600)
    print(f"Decoded data type: {type(user_data)}")
    print(f"Decoded data: {user_data}")

    if not isinstance(user_data, dict) or 'username' not in user_data:
    raise ValueError("Decoded data is not a valid dictionary or missing 'username' key.")

    username = user_data['username']
    print(username)
    except Exception as e:
    print(f"Error decoding token: {e}")
    flash("The reset link is invalid or has expired.")
    return redirect(url_for('forget'))

    if request.method == 'POST':
    password = request.form['pass']
    confirm_password = request.form['pass2']

    if password != confirm_password:
    print('Both passwords have to be the same')
    else:

    conn = connect_db()
    cursor = conn.cursor()
    cursor.execute(f'UPDATE {USERS_TABLE} SET pass = %s WHERE username = %s', (password, username))
    conn.commit()

    cursor.close()
    conn.close()
    print("Your password has been reset successfully.")
    return redirect(url_for('login'))

    except Exception as e:
    print(f"Unexpected error: {e}")
    flash("An error occurred. Please try again.")
    return redirect(url_for('forget'))

    return render_template('reset.html')


    reset.html

    <form name ="pythonProject1" action="{{ url_for('reset', token=value) }}" id="pp1" method="post">
    <h4 class="fw-bold mb-4" style="color: #92aad0;">Password reset</h4>
    <p class="mb-4" style="color: #45526e;">To reset your password , please fill in the text fields with your new password .</p>
    <p>{{msg}}</p>
    <div class="form-outline mb-4">
    <input value={{value}} type="email" id="mail" name="mail" class="form-control" hidden/>

    </div>

    <div class="form-outline mb-4">
    <input type="password" id="pass" name="pass" class="form-control" required />
    <label class="form-label" >new password</label>
    </div>

    <div class="form-outline mb-4">
    <input type="password" id="pass2" name="pass2" class="form-control" required />
    <label class="form-label" >repeat new password</label>
    </div>



    <div class="d-flex justify-content-end pt-1 mb-4">
    <button class="btn btn-primary btn-rounded" type="submit" style="background-color: #92aad0;">Submit</button>
    </div>

    <hr>

    </form>

    Continue reading...

Compartilhe esta Página