application.commands.handlers.register_user_handler¶
src.application.commands.handlers.register_user_handler
¶
Registration handler for User Authentication (F1.1).
Implements the registration flow as specified in: docs/architecture/authentication-architecture.md lines 250-261
Flow: 1. Emit UserRegistrationAttempted event 2. Validate email/password (handled by Annotated types) 3. Check email uniqueness 4. Hash password 5. Create User entity 6. Generate verification token (via repository) 7. Save user and token 8. Emit UserRegistrationSucceeded event (triggers email via EmailEventHandler) 9. Return Success(user_id)
On failure: - Emit UserRegistrationFailed event - Return Failure(error)
Architecture: - Application layer ONLY imports from domain layer (entities, protocols, events) - NO infrastructure imports (repositories are injected via protocols) - Handler orchestrates business logic without knowing persistence details
Classes¶
RegistrationError
¶
Registration-specific errors.
Source code in src/application/commands/handlers/register_user_handler.py
RegisterUserHandler
¶
Handler for user registration command.
Follows hexagonal architecture: - Application layer (this handler) - Domain layer (User entity, protocols) - Infrastructure layer (repositories, services via dependency injection)
Source code in src/application/commands/handlers/register_user_handler.py
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
Functions¶
__init__
¶
__init__(
user_repo: UserRepository,
verification_token_repo: EmailVerificationTokenRepository,
password_service: PasswordHashingProtocol,
event_bus: EventBusProtocol,
) -> None
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_repo
|
UserRepository
|
User repository for persistence |
required |
verification_token_repo
|
EmailVerificationTokenRepository
|
Email verification token repository |
required |
password_service
|
PasswordHashingProtocol
|
Password hashing service |
required |
event_bus
|
EventBusProtocol
|
Event bus for publishing domain events |
required |
Source code in src/application/commands/handlers/register_user_handler.py
handle
async
¶
Handle user registration command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
RegisterUser
|
RegisterUser command (email and password validated by Annotated types) |
required |
request
|
Request | None
|
Optional FastAPI Request for IP/user agent tracking (PCI-DSS 10.2.7). |
None
|
Returns:
| Type | Description |
|---|---|
Result[UUID, str]
|
Success(user_id) on successful registration |
Result[UUID, str]
|
Failure(error_message) on failure |
Side Effects
- Publishes UserRegistrationAttempted event (always)
- Publishes UserRegistrationSucceeded event (on success, triggers email)
- Publishes UserRegistrationFailed event (on failure)
- Creates User in database
- Creates EmailVerificationToken in database
Source code in src/application/commands/handlers/register_user_handler.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |