Simple local authentication, in reflex 0.6.5

Hello to all,

Where can I find a simple example of login using a local database? I need to prevent any page from being displayed, if the user is not logged in. The user can login against a database, or against a hashed key in a local file. I will only need a couple of users or three in my application.
My lack of knowledge about login management in web pages is causing me to have a finished application without managing user authentication, and not being able to deploy it because of that.

The documentation I can find is old, or perhaps a bit complex, or authenticates against other services.

Thank you for your help

check this library out GitHub - masenf/reflex-local-auth

1 Like

Hi,

Great contribution, @iameli .

I also took a closer look at this example today:

reflex-examples/twitter at main ¡ reflex-dev/reflex-examples ¡ GitHub.

I think my lack of knowledge about cookie management and user persistence led me to over-document.

I’m going to take both examples, clone them, and start transferring the concepts I really need to perform a simple but secure validation of a couple of users, which is what I need for my project.

Thank you very much for the contribution! It’s very concise and accurate!

If I want to use Reflex hosting, which currently doesn’t support databases, I need to direct reflex-local-auth to an external database, like SQL Server. Is it possible to do this? I can’t find how to do it.

I need to direct reflex-local-auth to an external database, like SQL Server. Is it possible to do this?

Install the appropriate db driver and set DB_URL environment variable for your database: Engine Configuration — SQLAlchemy 2.0 Documentation

Yes!
It is enough to add:
db_url=f"mssql+pymssql://{username}:{password}@{host}:{port}/{database}?charset=utf8",

to rxconfyg.py, as the example:
config = rx.Config(
app_name=“web_app”,
db_url=f"mssql+pymssql://{username}:{password}@{host}:{port}/{database}?charset=utf8",
)

But when I change from the local database SQLite to SQL Server, and I do:
reflex db makemigrations

I obtain this error:
alembic.util.exc.CommandError: Can’t locate revision identified by ‘9f88a9f52875’

How I can solve this?

I answer myself. The reason was that the alembic table already existed in the target database in SQL Server, from previous tests. Simply drop it.

I hope this information will be useful to someone

The problem comes now with reflex-local-auth , which generates this error when deploying the database in SQL Server:

File ‘src_pymssql.pyx’, line 465, in pymssql._pymssql.Cursor.execute
sqlalchemy.exc.OperationalError:
(pymssql.exceptions.OperationalError) (1919, b ‘Column “session_id” in table “localauthsession” is of a type that is invalid for use as a key column in an index.DB-Lib error message 20018, severity 16:General SQL Server error: Check messages from the SQL Server’)
[SQL: CREATE UNIQUE INDEX ix_localauthsession_session_id ON localauthsession (session_id)]

It seems that reflex-local-auth is not compatible with SQL Server.

PRs definitely welcome if you come up with a solution in the schema or otherwise :pray:

@Petricor does the error disappear if you set a maximum column size something like

class LocalAuthSession(
    rx.Model,
    table=True,  # type: ignore
):
    """Correlate a session_id with an arbitrary user_id."""

    user_id: int = Field(index=True, nullable=False)
    session_id: str = Field(unique=True, index=True, nullable=False, sa_type=String(25))
    expiration: datetime.datetime = Field(
        sa_column=Column(
            DateTime(timezone=True), server_default=func.now(), nullable=False
        ),
    )
1 Like

Done!

This is my first PR to a public repo. Sorry if I messed something up.

Any mistakes or corrections on my part would be useful to know so I can continue learning.

Thanks to @mistamun for leading the way.