[Python] Python says it has inserted row into DB, but it hasn't

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a simple auth system for one of my projects.

    I have a PostgreSQL database with 2 tables, "public"."app_users" and "public"."active_logins"

    The app users table has an auto generated uuid column, an email, a SHA256 password hash and a username. I have 1 user with the email demo@example.com and the password Password1 which has a hash of 19513fdc9da4fb72a4a05eb66917548d3c90ff94d5419e1f2363eea89dfee1dd

    I am connecting to Postgres with psycopg2,

    This is my python code

    # Import dependencies
    import psycopg2
    import hashlib

    # Connect to the database
    conn = psycopg2.connect(
    dbname = "postgres",
    user = "postgres",
    password = "VotesPostgresAdmin1!73e796£4fJyTt!uTkdDmw£qo!xgt!",
    host = "localhost",
    port = "5432"
    )

    def getLoginToken(email, password):

    # Hash the provided password
    hashedPassword = str(hashlib.sha256(password.encode()).hexdigest())

    # Print the hashed password to the terminal
    print("")
    print("The hashed password is:")
    print(hashedPassword)

    # Create a cursor
    cursor = conn.cursor()

    # Get the uuid of the user with the corresponding login credentials
    cursor.execute("""
    SELECT uuid FROM "public"."app_users"
    WHERE email = %s AND password_hash = %s;
    """, (email, hashedPassword))
    userUuid = cursor.fetchone()[0]

    # Print the user uuid to the terminal
    print("")
    print("The user UUID is:")
    print(userUuid)

    # Create a new auth token
    cursor.execute("""
    INSERT INTO "public"."active_logins"
    (user_uuid) VALUES (%s)
    RETURNING token;
    """, (userUuid,))
    authToken = cursor.fetchone()[0]

    # Print the auth tokent to the terminal
    print("")
    print("The new auth token is:")
    print(authToken)
    print("")


    getLoginToken('demo@example.com', 'Password1')


    When I run this I get this response:


    The hashed password is:
    19513fdc9da4fb72a4a05eb66917548d3c90ff94d5419e1f2363eea89dfee1dd

    The user UUID is:
    5b0d019f-284a-45fb-aeea-c716728e756f

    The new auth token is:
    caad4fd2-6cbb-4d1a-8f52-ee250f7bff1a



    But in my database there is no row for the auth token in the "public"."active_logins" table

    Do you know what is wrong with my code?

    Continue reading...

Compartilhe esta Página