Working signout button
This commit is contained in:
@@ -5,7 +5,7 @@ from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
||||
from pydantic import BaseModel
|
||||
|
||||
from backend.app.api.dependencies import get_current_user
|
||||
from backend.app.database import AVATARS_DIR, get_connection
|
||||
from backend.app.database import AVATARS_DIR, get_connection, hash_password
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -15,6 +15,11 @@ class UserConfigUpdate(BaseModel):
|
||||
bio: str | None = None
|
||||
|
||||
|
||||
class PasswordUpdate(BaseModel):
|
||||
current_password: str
|
||||
new_password: str
|
||||
|
||||
|
||||
class UserPluginConfigUpdate(BaseModel):
|
||||
instance: str | None = None
|
||||
access_token: str | None = None
|
||||
@@ -60,6 +65,22 @@ def update_current_user_profile(
|
||||
return {'status': 'updated'}
|
||||
|
||||
|
||||
@router.put('/password')
|
||||
def update_password(payload: PasswordUpdate, user: dict = Depends(get_current_user)):
|
||||
if len(payload.new_password) < 8:
|
||||
raise HTTPException(status_code=422, detail='New password must be at least 8 characters')
|
||||
if hash_password(payload.current_password) != user['password_hash']:
|
||||
raise HTTPException(status_code=400, detail='Current password is incorrect')
|
||||
|
||||
with get_connection() as conn:
|
||||
conn.execute(
|
||||
'UPDATE users SET password_hash = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?',
|
||||
(hash_password(payload.new_password), user['id']),
|
||||
)
|
||||
conn.commit()
|
||||
return {'status': 'password_updated'}
|
||||
|
||||
|
||||
@router.post('/avatar')
|
||||
async def upload_avatar(
|
||||
avatar: UploadFile = File(...),
|
||||
|
||||
Reference in New Issue
Block a user