Skip to content
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

Dodana funkcja change_password #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/src/auth/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from api.database import Base
from sqlalchemy import Column, Integer, String
from sqlalchemy.sql.expression import text
from sqlalchemy.sql.sqltypes import TIMESTAMP

from api.database import Base


class User(Base):
__tablename__ = "users"
Expand Down
33 changes: 31 additions & 2 deletions api/src/auth/router.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from api.database import get_db
from auth import models, schemas
from auth.crud import change_user_password, create_new_user, get_user_by_email
from auth.utils import create_access_token, verify, verify_reset_password_token
from auth.utils import (
create_access_token,
get_hashed_password,
verify,
verify_reset_password_token,
)
from fastapi import APIRouter, Body, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from sqlalchemy.orm import Session

from api.database import get_db

router = APIRouter(prefix="/api", tags=["auth"])


Expand Down Expand Up @@ -63,3 +69,26 @@ def reset_user_password(
)
change_user_password(db, user, new_password)
return {"message": "Password reset successfully"}


@router.post("/change-password/", response_model=schemas.Message)
def change_password(
user_email: str = Body(...),
old_password: str = Body(...),
new_password: str = Body(...),
db: Session = Depends(get_db),
):
user = get_user_by_email(db=db, user_email=user_email)
if not user:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
)
hashed_password = user.hashed_password
if verify(plain_password=old_password, hashed_password=hashed_password):
change_user_password(db, user, new_password)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Old password don't match with current password",
)
return {"message": "Password changed successfully"}
3 changes: 2 additions & 1 deletion api/src/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# root of the project, which inits the FastAPI app
from api.config import CORS_ORIGINS
from api.database import engine
from auth import models, router
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from api.database import engine

models.Base.metadata.create_all(bind=engine)

app = FastAPI()
Expand Down
Loading