Skip to content

Commit 018cf5d

Browse files
authored
Merge pull request #2 from masenf/user-rename
rename User to LocalUser
2 parents 4b3be8b + 1e0bafd commit 018cf5d

File tree

15 files changed

+167
-35
lines changed

15 files changed

+167
-35
lines changed

.github/workflows/publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Publish Component to PyPI
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
publish:
10+
name: Publish Component to PyPI
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@master
14+
- name: Set up Python 3.12
15+
uses: actions/setup-python@v3
16+
with:
17+
python-version: '3.12'
18+
- name: Install package
19+
run: pip install .
20+
- name: Publish to PyPI
21+
run: reflex component publish -t ${{ secrets.PYPI_TOKEN }} --no-share --no-validate-project-info

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def protected_page():
9090

9191
## Customization
9292

93-
The basic `reflex_local_auth.User` model provides password hashing and
93+
The basic `reflex_local_auth.LocalUser` model provides password hashing and
9494
verification, and an enabled flag. Additional functionality can be added by
9595
creating a new `UserInfo` model and creating a foreign key relationship to the
9696
`user.id` field.
@@ -146,7 +146,7 @@ def register_error() -> rx.Component:
146146
reflex_local_auth.RegistrationState.error_message != "",
147147
rx.callout(
148148
reflex_local_auth.RegistrationState.error_message,
149-
icon="alert_triangle",
149+
icon="triangle_alert",
150150
color_scheme="red",
151151
role="alert",
152152
width="100%",
@@ -234,4 +234,21 @@ def user_info():
234234
),
235235
),
236236
)
237+
```
238+
239+
## Migrating from 0.0.x to 0.1.x
240+
241+
The `User` model has been renamed to `LocalUser` and the `AuthSession` model has
242+
been renamed to `LocalAuthSession`. If your app was using reflex-local-auth 0.0.x,
243+
then you will need to make manual changes to migration script to copy existing user
244+
data into the new tables _after_ running `reflex db makemigrations`.
245+
246+
See [`local_auth_demo/alembic/version/cb01e050df85_.py`](local_auth_demo/alembic/version/cb01e050df85_.py) for an example migration script.
247+
248+
Importantly, your `upgrade` function should include the following lines, after creating
249+
the new tables and before dropping the old tables:
250+
251+
```python
252+
op.execute("INSERT INTO localuser SELECT * FROM user;")
253+
op.execute("INSERT INTO localauthsession SELECT * FROM authsession;")
237254
```

custom_components/reflex_local_auth/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from .login import require_login, LoginState
55
from .registration import RegistrationState
66
from .routes import set_login_route, set_register_route
7-
from .user import User
7+
from .user import LocalUser
88

99
__all__ = [
1010
"LocalAuthState",
11+
"LocalUser",
1112
"LoginState",
1213
"RegistrationState",
13-
"User",
1414
"pages",
1515
"routes",
1616
"require_login",

custom_components/reflex_local_auth/auth_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import reflex as rx
66

77

8-
class AuthSession(
8+
class LocalAuthSession(
99
rx.Model,
1010
table=True, # type: ignore
1111
):

custom_components/reflex_local_auth/local_auth.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
import reflex as rx
1414

15-
from .auth_session import AuthSession
16-
from .user import User
15+
from .auth_session import LocalAuthSession
16+
from .user import LocalUser
1717

1818

1919
AUTH_TOKEN_LOCAL_STORAGE_KEY = "_auth_token"
@@ -25,26 +25,26 @@ class LocalAuthState(rx.State):
2525
auth_token: str = rx.LocalStorage(name=AUTH_TOKEN_LOCAL_STORAGE_KEY)
2626

2727
@rx.cached_var
28-
def authenticated_user(self) -> User:
28+
def authenticated_user(self) -> LocalUser:
2929
"""The currently authenticated user, or a dummy user if not authenticated.
3030

3131
Returns:
32-
A User instance with id=-1 if not authenticated, or the User instance
32+
A LocalUser instance with id=-1 if not authenticated, or the LocalUser instance
3333
corresponding to the currently authenticated user.
3434
"""
3535
with rx.session() as session:
3636
result = session.exec(
37-
select(User, AuthSession).where(
38-
AuthSession.session_id == self.auth_token,
39-
AuthSession.expiration
37+
select(LocalUser, LocalAuthSession).where(
38+
LocalAuthSession.session_id == self.auth_token,
39+
LocalAuthSession.expiration
4040
>= datetime.datetime.now(datetime.timezone.utc),
41-
User.id == AuthSession.user_id,
41+
LocalUser.id == LocalAuthSession.user_id,
4242
),
4343
).first()
4444
if result:
4545
user, session = result
4646
return user
47-
return User(id=-1) # type: ignore
47+
return LocalUser(id=-1) # type: ignore
4848

4949
@rx.cached_var
5050
def is_authenticated(self) -> bool:
@@ -56,10 +56,10 @@ def is_authenticated(self) -> bool:
5656
return self.authenticated_user.id >= 0
5757

5858
def do_logout(self) -> None:
59-
"""Destroy AuthSessions associated with the auth_token."""
59+
"""Destroy LocalAuthSessions associated with the auth_token."""
6060
with rx.session() as session:
6161
for auth_session in session.exec(
62-
select(AuthSession).where(AuthSession.session_id == self.auth_token)
62+
select(LocalAuthSession).where(LocalAuthSession.session_id == self.auth_token)
6363
).all():
6464
session.delete(auth_session)
6565
session.commit()
@@ -70,14 +70,14 @@ def _login(
7070
user_id: int,
7171
expiration_delta: datetime.timedelta = DEFAULT_AUTH_SESSION_EXPIRATION_DELTA,
7272
) -> None:
73-
"""Create an AuthSession for the given user_id.
73+
"""Create an LocalAuthSession for the given user_id.
7474

75-
If the auth_token is already associated with an AuthSession, it will be
75+
If the auth_token is already associated with an LocalAuthSession, it will be
7676
logged out first.
7777

7878
Args:
79-
user_id: The user ID to associate with the AuthSession.
80-
expiration_delta: The amount of time before the AuthSession expires.
79+
user_id: The user ID to associate with the LocalAuthSession.
80+
expiration_delta: The amount of time before the LocalAuthSession expires.
8181
"""
8282
if self.is_authenticated:
8383
self.do_logout()
@@ -86,7 +86,7 @@ def _login(
8686
self.auth_token = self.auth_token or self.router.session.client_token
8787
with rx.session() as session:
8888
session.add(
89-
AuthSession( # type: ignore
89+
LocalAuthSession( # type: ignore
9090
user_id=user_id,
9191
session_id=self.auth_token,
9292
expiration=datetime.datetime.now(datetime.timezone.utc)

custom_components/reflex_local_auth/login.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from . import routes
88
from .local_auth import LocalAuthState
9-
from .user import User
9+
from .user import LocalUser
1010

1111

1212
class LoginState(LocalAuthState):
@@ -26,7 +26,7 @@ def on_submit(self, form_data) -> rx.event.EventSpec:
2626
password = form_data["password"]
2727
with rx.session() as session:
2828
user = session.exec(
29-
select(User).where(User.username == username)
29+
select(LocalUser).where(LocalUser.username == username)
3030
).one_or_none()
3131
if user is not None and not user.enabled:
3232
self.error_message = "This account is disabled."

custom_components/reflex_local_auth/pages/login.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def login_error() -> rx.Component:
1919
LoginState.error_message != "",
2020
rx.callout(
2121
LoginState.error_message,
22-
icon="alert_triangle",
22+
icon="triangle_alert",
2323
color_scheme="red",
2424
role="alert",
2525
width="100%",

custom_components/reflex_local_auth/pages/registration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def register_error() -> rx.Component:
1919
RegistrationState.error_message != "",
2020
rx.callout(
2121
RegistrationState.error_message,
22-
icon="alert_triangle",
22+
icon="triangle_alert",
2323
color_scheme="red",
2424
role="alert",
2525
width="100%",

custom_components/reflex_local_auth/registration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from . import routes
1111
from .local_auth import LocalAuthState
12-
from .user import User
12+
from .user import LocalUser
1313

1414

1515
POST_REGISTRATION_DELAY = 0.5
@@ -30,7 +30,7 @@ def _validate_fields(
3030
return rx.set_focus("username")
3131
with rx.session() as session:
3232
existing_user = session.exec(
33-
select(User).where(User.username == username)
33+
select(LocalUser).where(LocalUser.username == username)
3434
).one_or_none()
3535
if existing_user is not None:
3636
self.error_message = (
@@ -50,9 +50,9 @@ def _validate_fields(
5050
def _register_user(self, username, password) -> None:
5151
with rx.session() as session:
5252
# Create the new user and add it to the database.
53-
new_user = User() # type: ignore
53+
new_user = LocalUser() # type: ignore
5454
new_user.username = username
55-
new_user.password_hash = User.hash_password(password)
55+
new_user.password_hash = LocalUser.hash_password(password)
5656
new_user.enabled = True
5757
session.add(new_user)
5858
session.commit()

custom_components/reflex_local_auth/user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import reflex as rx
77

88

9-
class User(
9+
class LocalUser(
1010
rx.Model,
1111
table=True, # type: ignore
1212
):

0 commit comments

Comments
 (0)