Skip to content

Auth#23

Closed
AnkushMalaker wants to merge 9 commits intomainfrom
auth
Closed

Auth#23
AnkushMalaker wants to merge 9 commits intomainfrom
auth

Conversation

@AnkushMalaker
Copy link
Collaborator

Solving #16 via fastapi-users[oauth,beanie]

@AnkushMalaker AnkushMalaker marked this pull request as draft July 9, 2025 11:33
MONGODB_URI= No newline at end of file
MONGODB_URI=
DEBUG_DIR=./debug_dir
ADMIN_PASSWORD=
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't want to set this in the env file. We can have a default one which we put in the docs, and then prompt to change on first start.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's a good point. Lets land this and add a todo for that.

logger = logging.getLogger(__name__)

# Configuration from environment variables
SECRET_KEY = os.getenv("AUTH_SECRET_KEY", "CHANGE_ME_IN_PRODUCTION")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't set a default for a password. If a default is needed, place it in the .env.template, not in the code. This reduces chances that it will be forgotten to change

# User dependencies for protecting endpoints
current_active_user = fastapi_users.current_user(active=True)
current_superuser = fastapi_users.current_user(active=True, superuser=True)
optional_current_user = fastapi_users.current_user(optional=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the optional_current_user?

optional_current_user = fastapi_users.current_user(optional=True)


def can_access_all_data(user: User) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think this method is needed as you can do User.is_supervisor

Returns None for superusers (can access all), or [user.id] for regular users.
"""
if user.is_superuser:
return None # Can access all data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returning none when you can access all is a little unorthodox. Also unclear as to the purpose, as one assumes you can only access your data unless you're the admin,.


async def create_admin_user_if_needed():
"""Create admin user during startup if it doesn't exist and credentials are provided."""
if not ADMIN_PASSWORD:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this down, as if you already have an admin account, this doens't need to be set

@@ -0,0 +1,51 @@
"""User models for fastapi-users integration with Beanie and MongoDB."""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename file models/user.py

profile_picture: Optional[str] = None


class UserCreate(BaseUserCreate):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need seperate classes for different operations?

Copy link
Contributor

@thestumonkey thestumonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments, generally looks fine, although i am not an expert with auth

* updated auth to remove custom user-id

* stale data protection

* udpate
@AnkushMalaker
Copy link
Collaborator Author

The PR work is continued #27
Closing this.

@AnkushMalaker AnkushMalaker deleted the auth branch July 17, 2025 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants