second round of review
This commit is contained in:
parent
7140cf6f27
commit
bbc5625b2d
3 changed files with 24 additions and 27 deletions
|
|
@ -343,23 +343,21 @@ class ProfileUpdate(BaseModel):
|
||||||
nostr_npub: str | None = None
|
nostr_npub: str | None = None
|
||||||
|
|
||||||
|
|
||||||
def require_regular_user():
|
async def require_regular_user(
|
||||||
"""Dependency that requires the user to have the 'regular' role."""
|
|
||||||
async def checker(
|
|
||||||
current_user: User = Depends(get_current_user),
|
current_user: User = Depends(get_current_user),
|
||||||
) -> User:
|
) -> User:
|
||||||
|
"""Dependency that requires the user to have the 'regular' role."""
|
||||||
if ROLE_REGULAR not in current_user.role_names:
|
if ROLE_REGULAR not in current_user.role_names:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
detail="Profile access is only available to regular users",
|
detail="Profile access is only available to regular users",
|
||||||
)
|
)
|
||||||
return current_user
|
return current_user
|
||||||
return checker
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/api/profile", response_model=ProfileResponse)
|
@app.get("/api/profile", response_model=ProfileResponse)
|
||||||
async def get_profile(
|
async def get_profile(
|
||||||
current_user: User = Depends(require_regular_user()),
|
current_user: User = Depends(require_regular_user),
|
||||||
):
|
):
|
||||||
"""Get the current user's profile (contact details)."""
|
"""Get the current user's profile (contact details)."""
|
||||||
return ProfileResponse(
|
return ProfileResponse(
|
||||||
|
|
@ -374,7 +372,7 @@ async def get_profile(
|
||||||
async def update_profile(
|
async def update_profile(
|
||||||
data: ProfileUpdate,
|
data: ProfileUpdate,
|
||||||
db: AsyncSession = Depends(get_db),
|
db: AsyncSession = Depends(get_db),
|
||||||
current_user: User = Depends(require_regular_user()),
|
current_user: User = Depends(require_regular_user),
|
||||||
):
|
):
|
||||||
"""Update the current user's profile (contact details)."""
|
"""Update the current user's profile (contact details)."""
|
||||||
# Validate all fields
|
# Validate all fields
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,8 @@ def validate_telegram(value: str | None) -> str | None:
|
||||||
if not value.startswith("@"):
|
if not value.startswith("@"):
|
||||||
return "Telegram handle must start with @"
|
return "Telegram handle must start with @"
|
||||||
|
|
||||||
handle = value[1:] # Remove @
|
handle = value[1:]
|
||||||
if len(handle) < 1:
|
if not handle:
|
||||||
return "Telegram handle must have at least one character after @"
|
return "Telegram handle must have at least one character after @"
|
||||||
|
|
||||||
if len(handle) > 32:
|
if len(handle) > 32:
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,15 @@ function validateForm(data: FormData): FieldErrors {
|
||||||
return errors;
|
return errors;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toFormData(data: ProfileData): FormData {
|
||||||
|
return {
|
||||||
|
contact_email: data.contact_email || "",
|
||||||
|
telegram: data.telegram || "",
|
||||||
|
signal: data.signal || "",
|
||||||
|
nostr_npub: data.nostr_npub || "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export default function ProfilePage() {
|
export default function ProfilePage() {
|
||||||
const { user, isLoading, logout, hasRole } = useAuth();
|
const { user, isLoading, logout, hasRole } = useAuth();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
@ -151,12 +160,7 @@ export default function ProfilePage() {
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data: ProfileData = await res.json();
|
const data: ProfileData = await res.json();
|
||||||
const formValues: FormData = {
|
const formValues = toFormData(data);
|
||||||
contact_email: data.contact_email || "",
|
|
||||||
telegram: data.telegram || "",
|
|
||||||
signal: data.signal || "",
|
|
||||||
nostr_npub: data.nostr_npub || "",
|
|
||||||
};
|
|
||||||
setFormData(formValues);
|
setFormData(formValues);
|
||||||
setOriginalData(formValues);
|
setOriginalData(formValues);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -221,12 +225,7 @@ export default function ProfilePage() {
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
const data: ProfileData = await res.json();
|
const data: ProfileData = await res.json();
|
||||||
const formValues: FormData = {
|
const formValues = toFormData(data);
|
||||||
contact_email: data.contact_email || "",
|
|
||||||
telegram: data.telegram || "",
|
|
||||||
signal: data.signal || "",
|
|
||||||
nostr_npub: data.nostr_npub || "",
|
|
||||||
};
|
|
||||||
setFormData(formValues);
|
setFormData(formValues);
|
||||||
setOriginalData(formValues);
|
setOriginalData(formValues);
|
||||||
setToast({ message: "Profile saved successfully!", type: "success" });
|
setToast({ message: "Profile saved successfully!", type: "success" });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue