[Python] Python, streamlit-authenticator user not authorised. Login unable

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    I fetch user id and password form access db. Then the data are used to login in streamlit-authenticator.

    import os
    from dbfread import DBF
    import configparser
    import streamlit as st
    import pandas as pd
    import matplotlib.pyplot as plt
    import datetime
    import time
    import pyodbc
    import streamlit_authenticator as stauth
    import bcrypt

    # Pobranie ścieżki do pliku konfiguracyjnego (w katalogu skryptu)
    config_path = os.path.join(os.getcwd(), "config.ini")

    # Sprawdzenie, czy plik istnieje
    if not os.path.exists(config_path):
    print(f"Błąd: Plik konfiguracyjny nie znaleziony! Sprawdź, czy istnieje: {config_path}")
    else:
    # Wczytanie pliku konfiguracji
    config = configparser.ConfigParser()
    config.read(config_path)

    # Wczytanie pliku konfiguracyjnego
    config = configparser.ConfigParser()
    config.read("config.ini")

    # Pobranie kodu firmy
    fkod = config["Kod"]["kodfirmy"]
    dbpath = config["Sciezki"]["dba"]

    print(dbpath)

    # Połączenie z bazą danych Access
    conn_str = (
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
    f'DBQ={dbpath};'
    )
    conn = pyodbc.connect(conn_str)
    cursor = conn.cursor()


    # Pobranie użytkowników
    cursor.execute("SELECT user_id, hashed_password FROM users")
    users = cursor.fetchall()

    # Przetworzenie danych do formatu Streamlit Authenticator
    credentials = {"usernames": {user[0]: {'password': user[1]} for user in users}}


    # Inicjalizacja Streamlit Authenticator
    authenticator = stauth.Authenticate(
    credentials,
    "some_cookie_name",
    "some_key",
    cookie_expiry_days=30
    )

    name, authentication_status, username = authenticator.login(
    fields={
    'Form name': 'Login',
    'Username': 'name',
    'Password': 'password',
    }
    )



    if authentication_status:
    st.write(f'Witaj *{name}*!')
    authenticator.logout('Wyloguj', 'main')
    elif authentication_status is False:
    st.error('Nieprawidłowa nazwa użytkownika/hasło')
    elif authentication_status is None:
    st.warning('Wprowadź nazwę użytkownika i hasło')


    What I get is following exception:

    streamlit_authenticator.utilities.exceptions.LoginError: User not authorized

    Traceback:
    File "C:\py\app.py", line 59, in <module>
    name, authentication_status, username = authenticator.login(
    ~~~~~~~~~~~~~~~~~~~^
    fields={
    ^^^^^^^^
    ...<3 lines>...
    }
    ^
    )
    ^
    File "C:\Users\fwoda\AppData\Local\Programs\Python\Python313\Lib\site-packages\streamlit_authenticator\views\authentication_view.py", line 333, in login
    self.authentication_controller.login(token=token)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
    File "C:\Users\fwoda\AppData\Local\Programs\Python\Python313\Lib\site-packages\streamlit_authenticator\controllers\authentication_controller.py", line 265, in login
    return self.authentication_model.login(username, password, max_concurrent_users,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    max_login_attempts, token, single_session,
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    callback)
    ^^^^^^^^^
    File "C:\Users\fwoda\AppData\Local\Programs\Python\Python313\Lib\site-packages\streamlit_authenticator\models\authentication_model.py", line 432, in login
    raise LoginError('User not authorized')


    In the access database there is an user: User1.

    I can't guess where is the issue with authenticator. Could you advise?

    Continue reading...

Compartilhe esta Página