Add signup endpoint with token generation
This commit is contained in:
parent
b8141097dc
commit
f4b9c49106
@ -10,6 +10,7 @@ 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,
|
||||||
@ -17,7 +18,8 @@ from app.services.auth import (
|
|||||||
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()
|
||||||
|
|
||||||
@ -178,3 +180,32 @@ def change_password(
|
|||||||
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