Update main.py
This commit is contained in:
parent
6521add891
commit
b88f69251f
12
main.py
12
main.py
|
|
@ -29,7 +29,7 @@ class ChangePasswordModel(BaseModel):
|
||||||
new_password: str
|
new_password: str
|
||||||
|
|
||||||
class NoteObject(BaseModel):
|
class NoteObject(BaseModel):
|
||||||
title: str # Добавили название
|
title: str
|
||||||
text: str
|
text: str
|
||||||
|
|
||||||
def read_json(filename):
|
def read_json(filename):
|
||||||
|
|
@ -51,6 +51,11 @@ def register(user: User):
|
||||||
db = read_json(DB_FILE)
|
db = read_json(DB_FILE)
|
||||||
if any(u["username"] == user.username for u in db):
|
if any(u["username"] == user.username for u in db):
|
||||||
raise HTTPException(status_code=400, detail="Такой юзер уже есть")
|
raise HTTPException(status_code=400, detail="Такой юзер уже есть")
|
||||||
|
|
||||||
|
# Валидация длины пароля на бэке
|
||||||
|
if len(user.password) < 6:
|
||||||
|
raise HTTPException(status_code=400, detail="Пароль должен быть не менее 6 символов")
|
||||||
|
|
||||||
db.append({"username": user.username, "password": ph.hash(user.password)})
|
db.append({"username": user.username, "password": ph.hash(user.password)})
|
||||||
write_json(DB_FILE, db)
|
write_json(DB_FILE, db)
|
||||||
return {"message": "Регистрация успешна"}
|
return {"message": "Регистрация успешна"}
|
||||||
|
|
@ -78,13 +83,14 @@ def get_profile(x_token: str = Header(None)):
|
||||||
return {"username": u["username"], "hash": u["password"]}
|
return {"username": u["username"], "hash": u["password"]}
|
||||||
raise HTTPException(status_code=404, detail="User not found")
|
raise HTTPException(status_code=404, detail="User not found")
|
||||||
|
|
||||||
# Эндпоинт для смены пароля (любой юзер меняет свой, root в админке меняет тоже свой, т.к. сессия его)
|
|
||||||
@app.post("/change-password")
|
@app.post("/change-password")
|
||||||
def change_password(data: ChangePasswordModel, x_token: str = Header(None)):
|
def change_password(data: ChangePasswordModel, x_token: str = Header(None)):
|
||||||
username = get_user_from_token(x_token)
|
username = get_user_from_token(x_token)
|
||||||
db = read_json(DB_FILE)
|
db = read_json(DB_FILE)
|
||||||
for u in db:
|
for u in db:
|
||||||
if u["username"] == username:
|
if u["username"] == username:
|
||||||
|
if len(data.new_password) < 6:
|
||||||
|
raise HTTPException(status_code=400, detail="Пароль должен быть не менее 6 символов")
|
||||||
u["password"] = ph.hash(data.new_password)
|
u["password"] = ph.hash(data.new_password)
|
||||||
write_json(DB_FILE, db)
|
write_json(DB_FILE, db)
|
||||||
return {"message": "Пароль успешно изменен"}
|
return {"message": "Пароль успешно изменен"}
|
||||||
|
|
@ -114,4 +120,4 @@ def admin_get_objects(x_token: str = Header(None)):
|
||||||
def admin_get_users(x_token: str = Header(None)):
|
def admin_get_users(x_token: str = Header(None)):
|
||||||
username = get_user_from_token(x_token)
|
username = get_user_from_token(x_token)
|
||||||
if username != "root": raise HTTPException(status_code=403)
|
if username != "root": raise HTTPException(status_code=403)
|
||||||
return read_json(DB_FILE)
|
return read_json(DB_FILE)
|
||||||
Loading…
Reference in New Issue