Add signup endpoint with token generation
This commit is contained in:
parent
b8141097dc
commit
f4b9c49106
@ -10,14 +10,16 @@ from app.models.user import User
|
|||||||
from app.schemas.auth import ChangePassword, Login
|
from app.schemas.auth import ChangePassword, Login
|
||||||
from app.schemas.password import PasswordReset, PasswordResetConfirm
|
from app.schemas.password import PasswordReset, PasswordResetConfirm
|
||||||
from app.schemas.token import Token, TokenPayload, TokenRefresh
|
from app.schemas.token import Token, TokenPayload, TokenRefresh
|
||||||
|
from app.schemas.user import User as UserSchema, UserCreate
|
||||||
from app.services.auth import (
|
from app.services.auth import (
|
||||||
authenticate_user,
|
authenticate_user,
|
||||||
create_tokens_for_user,
|
create_tokens_for_user,
|
||||||
generate_password_reset_token,
|
generate_password_reset_token,
|
||||||
reset_password,
|
reset_password,
|
||||||
verify_password_reset_token
|
verify_password_reset_token
|
||||||
)
|
)
|
||||||
from app.utils.security import verify_password
|
from app.services.user import create_user
|
||||||
|
from app.utils.security import get_password_hash, verify_password
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@ -169,12 +171,41 @@ def change_password(
|
|||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="Incorrect password"
|
detail="Incorrect password"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update password
|
# Update password
|
||||||
current_user.hashed_password = get_password_hash(password_change.new_password)
|
current_user.hashed_password = get_password_hash(password_change.new_password)
|
||||||
db.add(current_user)
|
db.add(current_user)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"message": "Password has been changed successfully"
|
"message": "Password has been changed successfully"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/signup", response_model=Token, status_code=status.HTTP_201_CREATED)
|
||||||
|
def signup(
|
||||||
|
user_in: UserCreate,
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Create new user account and return access token.
|
||||||
|
"""
|
||||||
|
# Check if the user already exists
|
||||||
|
user = db.query(User).filter(User.email == user_in.email).first()
|
||||||
|
if user:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Email already registered"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create new user
|
||||||
|
user = create_user(db, user_in=user_in)
|
||||||
|
if not user:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail="Error creating user account"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Generate tokens for the newly created user
|
||||||
|
tokens = create_tokens_for_user(user.id)
|
||||||
|
return tokens
|
Loading…
x
Reference in New Issue
Block a user