forked from KayvanShah1/taskaza
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add user profile update #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KayvanShah1
wants to merge
4
commits into
taskaza-agent
Choose a base branch
from
codex/implement-full-stack-web-application
base: taskaza-agent
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from typing import List | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, status | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.core.dependencies import get_current_user, get_db | ||
| from app.crud import apikey as crud_apikey | ||
| from app.models.user import User | ||
| from app.schemas.apikey import APIKeyCreated, APIKeyOut | ||
|
|
||
| router = APIRouter(prefix="/apikeys", tags=["API Keys"]) | ||
|
|
||
|
|
||
| @router.post("", response_model=APIKeyCreated, status_code=status.HTTP_201_CREATED) | ||
| async def create_api_key( | ||
| current_user: User = Depends(get_current_user), | ||
| db: AsyncSession = Depends(get_db), | ||
| ): | ||
| key, db_key = await crud_apikey.create_api_key(db, current_user) | ||
| return APIKeyCreated(id=db_key.id, prefix=db_key.prefix, created_at=db_key.created_at, revoked=db_key.revoked, key=key) | ||
|
|
||
|
|
||
| @router.get("", response_model=List[APIKeyOut]) | ||
| async def list_api_keys( | ||
| current_user: User = Depends(get_current_user), | ||
| db: AsyncSession = Depends(get_db), | ||
| ): | ||
| keys = await crud_apikey.list_api_keys(db, current_user) | ||
| return keys | ||
|
|
||
|
|
||
| @router.delete("/{api_key_id}", status_code=status.HTTP_204_NO_CONTENT) | ||
| async def delete_api_key( | ||
| api_key_id: int, | ||
| current_user: User = Depends(get_current_user), | ||
| db: AsyncSession = Depends(get_db), | ||
| ): | ||
| success = await crud_apikey.revoke_api_key(db, current_user, api_key_id) | ||
| if not success: | ||
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="API key not found") | ||
| return None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from datetime import datetime, timedelta | ||
| import secrets | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, status | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.core.dependencies import get_current_user, get_db, verify_api_key | ||
| from app.crud import user as crud_user | ||
| from app.models.user import User | ||
| from app.schemas.auth import Message, VerificationToken | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.post( | ||
| "/auth/request-verification", | ||
| response_model=Message, | ||
| dependencies=[Depends(verify_api_key)], | ||
| ) | ||
| async def request_verification( | ||
| current_user: User = Depends(get_current_user), | ||
| db: AsyncSession = Depends(get_db), | ||
| ): | ||
| if current_user.email_verified: | ||
| return {"detail": "Email already verified"} | ||
| token = secrets.token_urlsafe(16) | ||
| expires = datetime.utcnow() + timedelta(hours=1) | ||
| await crud_user.set_verification_token(db, current_user, token, expires) | ||
| # In a real app, send the token via email | ||
| return {"detail": token} | ||
|
|
||
|
|
||
| @router.post("/auth/verify", response_model=Message) | ||
| async def verify_email(token_in: VerificationToken, db: AsyncSession = Depends(get_db)): | ||
| user = await crud_user.verify_user_email(db, token_in.token) | ||
| if not user: | ||
| raise HTTPException( | ||
| status_code=status.HTTP_400_BAD_REQUEST, | ||
| detail="Invalid or expired token", | ||
| ) | ||
| return {"detail": "Email verified"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from datetime import datetime | ||
| from secrets import token_urlsafe | ||
| from typing import List, Optional | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from app.core.security import hash_password, verify_password | ||
| from app.models.apikey import APIKey | ||
| from app.models.user import User | ||
|
|
||
|
|
||
| async def create_api_key(db: AsyncSession, user: User) -> tuple[str, APIKey]: | ||
| key = token_urlsafe(32) | ||
| hashed = hash_password(key) | ||
| db_key = APIKey(user_id=user.id, hashed_key=hashed, prefix=key[:8]) | ||
| db.add(db_key) | ||
| await db.commit() | ||
| await db.refresh(db_key) | ||
| return key, db_key | ||
|
|
||
|
|
||
| async def list_api_keys(db: AsyncSession, user: User) -> List[APIKey]: | ||
| result = await db.execute(select(APIKey).where(APIKey.user_id == user.id)) | ||
| return result.scalars().all() | ||
|
|
||
|
|
||
| async def verify_api_key(db: AsyncSession, user: User, raw_key: str) -> Optional[APIKey]: | ||
| result = await db.execute( | ||
| select(APIKey).where(APIKey.user_id == user.id, APIKey.revoked.is_(False)) | ||
| ) | ||
| for api_key in result.scalars().all(): | ||
| if verify_password(raw_key, api_key.hashed_key): | ||
| api_key.last_used_at = datetime.utcnow() | ||
| await db.commit() | ||
| return api_key | ||
| return None | ||
|
|
||
|
|
||
| async def revoke_api_key(db: AsyncSession, user: User, api_key_id: int) -> bool: | ||
| api_key = await db.get(APIKey, api_key_id) | ||
| if not api_key or api_key.user_id != user.id: | ||
| return False | ||
| api_key.revoked = True | ||
| await db.commit() | ||
| return True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Skip email uniqueness check when clearing address
Both
update_meandpatch_merun a uniqueness check whenever the payload contains anemailkey, even when the value isnull. Becauseget_user_by_emailis then called withNone, the query matches the first user that already has a NULL email (which is common because signups default to no email). Theexisting.id != current_user.idbranch fires and returns400 "Email already taken", making it impossible for a user to remove their email address while any other user also lacks one. The check should only run when a non-NULL email is supplied.Useful? React with 👍 / 👎.