Skip to content

Commit

Permalink
LOcal Auth to radix namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ElijahAhianyo committed Feb 14, 2024
1 parent 4c9b7d4 commit f7bf39d
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 56 deletions.
4 changes: 1 addition & 3 deletions local_auth/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def run_migrations_online() -> None:
)

with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
context.configure(connection=connection, target_metadata=target_metadata)

with context.begin_transaction():
context.run_migrations()
Expand Down
53 changes: 32 additions & 21 deletions local_auth/alembic/versions/e50402348eab_.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,50 @@
import sqlmodel

# revision identifiers, used by Alembic.
revision: str = 'e50402348eab'
revision: str = "e50402348eab"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('authsession',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('user_id', sa.Integer(), nullable=False),
sa.Column('session_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('expiration', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
sa.PrimaryKeyConstraint('id')
op.create_table(
"authsession",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("session_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column(
"expiration",
sa.DateTime(timezone=True),
server_default=sa.text("(CURRENT_TIMESTAMP)"),
nullable=False,
),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f('ix_authsession_session_id'), 'authsession', ['session_id'], unique=True)
op.create_index(op.f('ix_authsession_user_id'), 'authsession', ['user_id'], unique=False)
op.create_table('user',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('username', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('password_hash', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column('enabled', sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint('id')
op.create_index(
op.f("ix_authsession_session_id"), "authsession", ["session_id"], unique=True
)
op.create_index(op.f('ix_user_username'), 'user', ['username'], unique=True)
op.create_index(
op.f("ix_authsession_user_id"), "authsession", ["user_id"], unique=False
)
op.create_table(
"user",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("username", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("password_hash", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
sa.Column("enabled", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(op.f("ix_user_username"), "user", ["username"], unique=True)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_user_username'), table_name='user')
op.drop_table('user')
op.drop_index(op.f('ix_authsession_user_id'), table_name='authsession')
op.drop_index(op.f('ix_authsession_session_id'), table_name='authsession')
op.drop_table('authsession')
op.drop_index(op.f("ix_user_username"), table_name="user")
op.drop_table("user")
op.drop_index(op.f("ix_authsession_user_id"), table_name="authsession")
op.drop_index(op.f("ix_authsession_session_id"), table_name="authsession")
op.drop_table("authsession")
# ### end Alembic commands ###
4 changes: 3 additions & 1 deletion local_auth/local_auth/auth_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ class AuthSession(
user_id: int = Field(index=True, nullable=False)
session_id: str = Field(unique=True, index=True, nullable=False)
expiration: datetime.datetime = Field(
sa_column=Column(DateTime(timezone=True), server_default=func.now(), nullable=False),
sa_column=Column(
DateTime(timezone=True), server_default=func.now(), nullable=False
),
)
18 changes: 9 additions & 9 deletions local_auth/local_auth/local_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def index() -> rx.Component:
"""
return rx.fragment(
rx.chakra.color_mode_button(rx.chakra.color_mode_icon(), float="right"),
rx.chakra.vstack(
rx.chakra.heading("Welcome to my homepage!", font_size="2em"),
rx.chakra.link("Protected Page", href="/protected"),
spacing="1.5em",
rx.vstack(
rx.heading("Welcome to my homepage!", font_size="2em"),
rx.link("Protected Page", href="/protected"),
spacing="2",
padding_top="10%",
),
)
Expand All @@ -33,15 +33,15 @@ def protected() -> rx.Component:
Returns:
A reflex component.
"""
return rx.chakra.vstack(
rx.chakra.heading(
return rx.vstack(
rx.heading(
"Protected Page for ", State.authenticated_user.username, font_size="2em"
),
rx.chakra.link("Home", href="/"),
rx.chakra.link("Logout", href="/", on_click=State.do_logout),
rx.link("Home", href="/"),
rx.link("Logout", href="/", on_click=State.do_logout),
)


app = rx.App()
app = rx.App(theme=rx.theme(has_background=True, accent_color="orange"))
app.add_page(index)
app.add_page(protected)
73 changes: 63 additions & 10 deletions local_auth/local_auth/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,77 @@ def login_page() -> rx.Component:
Returns:
A reflex component.
"""
login_form = rx.chakra.form(
rx.chakra.input(placeholder="username", id="username"),
rx.chakra.password(placeholder="password", id="password"),
rx.chakra.button("Login", type_="submit"),
width="80vw",
on_submit=LoginState.on_submit,

login_form = rx.box(
rx.vstack(
rx.form.root(
rx.fragment(
rx.heading(
"Login into your Account", size="7", margin_bottom="2rem"
),
rx.text(
"Username",
color="hsl(240, 5%, 64.9%)",
margin_top="2px",
margin_bottom="4px",
),
rx.input.input(
placeholder="username",
id="username",
border_color="hsl(240,3.7%,15.9%)",
justify_content="center",
),
rx.text(
"Password",
color="hsl(240, 5%, 64.9%)",
margin_top="2px",
margin_bottom="4px",
),
rx.input.input(
placeholder="password",
id="password",
border_color="hsl(240,3.7%,15.9%)",
justify_content="center",
type="password",
),
rx.box(
rx.form.submit(
rx.button(
"Sign in",
type="submit",
width="100%",
),
as_child=True,
),
padding_top="14px",
),
),
on_submit=LoginState.on_submit,
),
rx.link("Register", href=REGISTER_ROUTE),
),
padding="8rem 10rem",
margin_top="10vh",
margin_x="auto",
border="2px solid black",
border_color="gray.300",
border_radius=10,
)

return rx.fragment(
rx.cond(
LoginState.is_hydrated, # type: ignore
rx.chakra.vstack(
rx.vstack(
rx.cond( # conditionally show error messages
LoginState.error_message != "",
rx.chakra.text(LoginState.error_message),
rx.callout(
LoginState.error_message,
icon="alert_triangle",
color_scheme="red",
role="alert",
),
),
login_form,
rx.chakra.link("Register", href=REGISTER_ROUTE),
padding_top="10vh",
),
)
Expand All @@ -107,7 +160,7 @@ def protected_page():
rx.cond(
State.is_hydrated & State.is_authenticated, # type: ignore
page(),
rx.chakra.center(
rx.center(
# When this spinner mounts, it will redirect to the login page
rx.chakra.spinner(on_mount=LoginState.redir),
),
Expand Down
88 changes: 77 additions & 11 deletions local_auth/local_auth/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import AsyncGenerator

import reflex as rx

from sqlmodel import select

from .base_state import State
Expand Down Expand Up @@ -77,25 +78,90 @@ def registration_page() -> rx.Component:
Returns:
A reflex component.
"""
register_form = rx.chakra.form(
rx.chakra.input(placeholder="username", id="username"),
rx.chakra.password(placeholder="password", id="password"),
rx.chakra.password(placeholder="confirm", id="confirm_password"),
rx.chakra.button("Register", type_="submit"),
width="80vw",
on_submit=RegistrationState.handle_registration,

register_form = rx.box(
rx.vstack(
rx.form.root(
rx.fragment(
rx.heading("Create an account", size="7", margin_bottom="2rem"),
rx.text(
"Username",
color="hsl(240, 5%, 64.9%)",
margin_top="2px",
margin_bottom="4px",
),
rx.input.input(
placeholder="username",
id="username",
border_color="hsl(240,3.7%,15.9%)",
justify_content="center",
),
rx.text(
"Password",
color="hsl(240, 5%, 64.9%)",
margin_top="2px",
margin_bottom="4px",
),
rx.input.input(
placeholder="password",
id="password",
border_color="hsl(240,3.7%,15.9%)",
justify_content="center",
type="password",
),
rx.text(
"Confirm Password",
color="hsl(240, 5%, 64.9%)",
margin_top="2px",
margin_bottom="4px",
),
rx.input.input(
placeholder="confirm",
id="confirm_password",
border_color="hsl(240,3.7%,15.9%)",
justify_content="center",
type="password",
),
rx.box(
rx.form.submit(
rx.button(
"Sign up",
type="submit",
width="100%",
),
as_child=True,
),
padding_top="14px",
),
),
on_submit=RegistrationState.handle_registration,
),
rx.link("Login", href=LOGIN_ROUTE),
),
padding="8rem 10rem",
margin_top="10vh",
margin_x="auto",
border="2px solid black",
border_color="gray.300",
border_radius=10,
)

return rx.fragment(
rx.cond(
RegistrationState.success,
rx.chakra.vstack(
rx.chakra.text("Registration successful!"),
rx.vstack(
rx.text("Registration successful!"),
rx.chakra.spinner(),
),
rx.chakra.vstack(
rx.vstack(
rx.cond( # conditionally show error messages
RegistrationState.error_message != "",
rx.chakra.text(RegistrationState.error_message),
rx.callout(
RegistrationState.error_message,
icon="alert_triangle",
color_scheme="red",
role="alert",
),
),
register_form,
padding_top="10vh",
Expand Down
4 changes: 3 additions & 1 deletion local_auth/rxconfig.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import reflex as rx


class LocalauthConfig(rx.Config):
pass


config = LocalauthConfig(
app_name="local_auth",
)
)

0 comments on commit f7bf39d

Please sign in to comment.