-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd35b0b
commit 5ecb656
Showing
7 changed files
with
158 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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,42 @@ | ||
from fastapi import Depends, HTTPException, status | ||
from fastapi.security import OAuth2PasswordBearer | ||
from jose import JWTError, jwt | ||
from app.models import User | ||
import os | ||
from dotenv import load_dotenv | ||
|
||
# Load environment variables from .env file | ||
load_dotenv() | ||
|
||
SECRET_KEY = os.getenv("SECRET_KEY", "default_secret_key") | ||
ALGORITHM = "HS256" | ||
|
||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") | ||
|
||
import logging | ||
|
||
# Configure logging | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
def get_current_user(token: str = Depends(oauth2_scheme)): | ||
logging.info("Decoding JWT token") | ||
credentials_exception = HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Could not validate credentials", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
try: | ||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | ||
wallet_address: str = payload.get("sub") | ||
if wallet_address is None: | ||
logging.error("Wallet address not found in token") | ||
raise credentials_exception | ||
except JWTError as e: | ||
logging.error(f"JWT decoding error: {e}") | ||
raise credentials_exception | ||
user = User.objects(wallet_address=wallet_address).first() | ||
if user is None: | ||
logging.error("User not found for wallet address") | ||
raise credentials_exception | ||
logging.info(f"User {user.wallet_address} authenticated successfully") | ||
return user |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,16 +1,41 @@ | ||
import os | ||
from fastapi import APIRouter, HTTPException | ||
from app.models import User | ||
from app.schemas import UserCreate, UserResponse | ||
from app.models import User, Post | ||
from app.schemas import UserCreate, UserResponse, UserProfileResponse | ||
from uuid import UUID | ||
from datetime import datetime, timedelta | ||
from fastapi.encoders import jsonable_encoder | ||
|
||
user_router = APIRouter() | ||
|
||
@user_router.get("/test") | ||
def test_route(): | ||
return {"message": "Test route is working"} | ||
|
||
@user_router.get("/{user_id}", response_model=UserResponse) | ||
def read_user(user_id: UUID): | ||
db_user = User.objects(id=user_id).first() | ||
if db_user is None: | ||
raise HTTPException(status_code=404, detail="User not found") | ||
return db_user | ||
|
||
@user_router.get("/profile-from-post/{post_id}", response_model=UserProfileResponse) | ||
def get_user_profile_from_post(post_id: UUID): | ||
# Retrieve all posts | ||
all_posts = Post.objects.all() | ||
post = next((p for p in all_posts if p.id == post_id), None) | ||
if post is None: | ||
raise HTTPException(status_code=404, detail="Post not found") | ||
|
||
# Retrieve all users | ||
all_users = User.objects.all() | ||
user = next((u for u in all_users if u.id == post.user_id), None) | ||
if user is None: | ||
raise HTTPException(status_code=404, detail="User not found") | ||
|
||
# Return the user's profile photo URL, wallet address, and display name | ||
return { | ||
"profile_photo_url": user.profile_photo_url, | ||
"wallet_address": user.wallet_address, | ||
"display_name": user.display_name | ||
} |
This file contains 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