feat(user): add auto-verification option for user creation
This commit is contained in:
parent
93ad731f40
commit
442369505a
@ -52,7 +52,7 @@ async def register_user(user_data: UserCreate, db: Session = Depends(get_db)):
|
|||||||
Raises:
|
Raises:
|
||||||
HTTPException: If there is an error in registration
|
HTTPException: If there is an error in registration
|
||||||
"""
|
"""
|
||||||
user, message = create_user(db, user_data, is_admin=False)
|
user, message = create_user(db, user_data, is_admin=False, auto_verify=False)
|
||||||
if not user:
|
if not user:
|
||||||
logger.error(f"Error registering user: {message}")
|
logger.error(f"Error registering user: {message}")
|
||||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message)
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message)
|
||||||
|
@ -124,7 +124,9 @@ def create_client_with_user(
|
|||||||
db.flush() # Get client ID without committing the transaction
|
db.flush() # Get client ID without committing the transaction
|
||||||
|
|
||||||
# Use client ID to create the associated user
|
# Use client ID to create the associated user
|
||||||
user, message = create_user(db, user_data, is_admin=False, client_id=client.id)
|
user, message = create_user(
|
||||||
|
db, user_data, is_admin=False, client_id=client.id, auto_verify=True
|
||||||
|
)
|
||||||
|
|
||||||
if not user:
|
if not user:
|
||||||
# If there was an error creating the user, rollback
|
# If there was an error creating the user, rollback
|
||||||
|
@ -20,6 +20,7 @@ def create_user(
|
|||||||
user_data: UserCreate,
|
user_data: UserCreate,
|
||||||
is_admin: bool = False,
|
is_admin: bool = False,
|
||||||
client_id: Optional[uuid.UUID] = None,
|
client_id: Optional[uuid.UUID] = None,
|
||||||
|
auto_verify: bool = False,
|
||||||
) -> Tuple[Optional[User], str]:
|
) -> Tuple[Optional[User], str]:
|
||||||
"""
|
"""
|
||||||
Creates a new user in the system
|
Creates a new user in the system
|
||||||
@ -29,6 +30,7 @@ def create_user(
|
|||||||
user_data: User data to be created
|
user_data: User data to be created
|
||||||
is_admin: If the user is an administrator
|
is_admin: If the user is an administrator
|
||||||
client_id: Associated client ID (optional, a new one will be created if not provided)
|
client_id: Associated client ID (optional, a new one will be created if not provided)
|
||||||
|
auto_verify: If True, user is created with email already verified and active
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Tuple[Optional[User], str]: Tuple with the created user (or None in case of error) and status message
|
Tuple[Optional[User], str]: Tuple with the created user (or None in case of error) and status message
|
||||||
@ -42,7 +44,10 @@ def create_user(
|
|||||||
)
|
)
|
||||||
return None, "Email already registered"
|
return None, "Email already registered"
|
||||||
|
|
||||||
# Create verification token
|
# Create verification token if needed
|
||||||
|
verification_token = None
|
||||||
|
token_expiry = None
|
||||||
|
if not auto_verify:
|
||||||
verification_token = generate_token()
|
verification_token = generate_token()
|
||||||
token_expiry = datetime.utcnow() + timedelta(hours=24)
|
token_expiry = datetime.utcnow() + timedelta(hours=24)
|
||||||
|
|
||||||
@ -64,15 +69,16 @@ def create_user(
|
|||||||
password_hash=get_password_hash(user_data.password),
|
password_hash=get_password_hash(user_data.password),
|
||||||
client_id=local_client_id,
|
client_id=local_client_id,
|
||||||
is_admin=is_admin,
|
is_admin=is_admin,
|
||||||
is_active=False, # Inactive until email is verified
|
is_active=auto_verify,
|
||||||
email_verified=False,
|
email_verified=auto_verify,
|
||||||
verification_token=verification_token,
|
verification_token=verification_token,
|
||||||
verification_token_expiry=token_expiry,
|
verification_token_expiry=token_expiry,
|
||||||
)
|
)
|
||||||
db.add(user)
|
db.add(user)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
# Send verification email
|
# Send verification email if not auto-verified
|
||||||
|
if not auto_verify:
|
||||||
email_sent = send_verification_email(user.email, verification_token)
|
email_sent = send_verification_email(user.email, verification_token)
|
||||||
if not email_sent:
|
if not email_sent:
|
||||||
logger.error(f"Failed to send verification email to {user.email}")
|
logger.error(f"Failed to send verification email to {user.email}")
|
||||||
@ -83,6 +89,12 @@ def create_user(
|
|||||||
user,
|
user,
|
||||||
"User created successfully. Check your email to activate your account.",
|
"User created successfully. Check your email to activate your account.",
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
logger.info(f"User created and auto-verified: {user.email}")
|
||||||
|
return (
|
||||||
|
user,
|
||||||
|
"User created successfully.",
|
||||||
|
)
|
||||||
|
|
||||||
except SQLAlchemyError as e:
|
except SQLAlchemyError as e:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
@ -388,7 +400,7 @@ def create_admin_user(db: Session, user_data: UserCreate) -> Tuple[Optional[User
|
|||||||
Returns:
|
Returns:
|
||||||
Tuple[Optional[User], str]: Tuple with the created user (or None in case of error) and status message
|
Tuple[Optional[User], str]: Tuple with the created user (or None in case of error) and status message
|
||||||
"""
|
"""
|
||||||
return create_user(db, user_data, is_admin=True)
|
return create_user(db, user_data, is_admin=True, auto_verify=True)
|
||||||
|
|
||||||
|
|
||||||
def deactivate_user(db: Session, user_id: uuid.UUID) -> Tuple[bool, str]:
|
def deactivate_user(db: Session, user_id: uuid.UUID) -> Tuple[bool, str]:
|
||||||
|
Loading…
Reference in New Issue
Block a user