@@ -24,8 +24,8 @@ use mas_matrix::BoxHomeserverConnection;
24
24
use mas_policy:: Policy ;
25
25
use mas_router:: UrlBuilder ;
26
26
use mas_storage:: {
27
- queue:: { ProvisionUserJob , QueueJobRepositoryExt as _} ,
28
- user:: { BrowserSessionRepository , UserEmailRepository , UserPasswordRepository , UserRepository } ,
27
+ queue:: { QueueJobRepositoryExt as _, SendEmailAuthenticationCodeJob } ,
28
+ user:: { UserEmailRepository , UserRepository } ,
29
29
BoxClock , BoxRepository , BoxRng , RepositoryAccess ,
30
30
} ;
31
31
use mas_templates:: {
@@ -141,6 +141,8 @@ pub(crate) async fn post(
141
141
Form ( form) : Form < ProtectedForm < RegisterForm > > ,
142
142
) -> Result < Response , FancyError > {
143
143
let user_agent = user_agent. map ( |ua| UserAgent :: parse ( ua. as_str ( ) . to_owned ( ) ) ) ;
144
+
145
+ let ip_address = activity_tracker. ip ( ) ;
144
146
if !site_config. password_registration_enabled {
145
147
return Ok ( StatusCode :: METHOD_NOT_ALLOWED . into_response ( ) ) ;
146
148
}
@@ -296,49 +298,62 @@ pub(crate) async fn post(
296
298
return Ok ( ( cookie_jar, Html ( content) ) . into_response ( ) ) ;
297
299
}
298
300
299
- let user = repo. user ( ) . add ( & mut rng, & clock, form. username ) . await ?;
300
-
301
- if let Some ( tos_uri) = & site_config. tos_uri {
302
- repo. user_terms ( )
303
- . accept_terms ( & mut rng, & clock, & user, tos_uri. clone ( ) )
304
- . await ?;
305
- }
301
+ let post_auth_action = query
302
+ . post_auth_action
303
+ . map ( serde_json:: to_value)
304
+ . transpose ( ) ?;
305
+ let registration = repo
306
+ . user_registration ( )
307
+ . add ( & mut rng, & clock, ip_address, user_agent, post_auth_action)
308
+ . await ?;
306
309
307
- let password = Zeroizing :: new ( form. password . into_bytes ( ) ) ;
308
- let ( version, hashed_password) = password_manager. hash ( & mut rng, password) . await ?;
309
- let user_password = repo
310
- . user_password ( )
311
- . add ( & mut rng, & clock, & user, version, hashed_password, None )
310
+ let registration = repo
311
+ . user_registration ( )
312
+ . set_username ( registration, form. username )
312
313
. await ?;
313
314
314
- let user_email = repo
315
+ let registration = if let Some ( tos_uri) = & site_config. tos_uri {
316
+ repo. user_registration ( )
317
+ . set_terms_url ( registration, tos_uri. clone ( ) )
318
+ . await ?
319
+ } else {
320
+ registration
321
+ } ;
322
+
323
+ // Create a new user email authentication session
324
+ let user_email_authentication = repo
315
325
. user_email ( )
316
- . add ( & mut rng, & clock, & user , form. email )
326
+ . add_authentication_for_registration ( & mut rng, & clock, form. email , & registration )
317
327
. await ?;
318
328
319
- let next = mas_router:: AccountVerifyEmail :: new ( user_email. id ) . and_maybe ( query. post_auth_action ) ;
320
-
321
- let session = repo
322
- . browser_session ( )
323
- . add ( & mut rng, & clock, & user, user_agent)
329
+ // Schedule a job to verify the email
330
+ repo. queue_job ( )
331
+ . schedule_job (
332
+ & mut rng,
333
+ & clock,
334
+ SendEmailAuthenticationCodeJob :: new ( & user_email_authentication, locale. to_string ( ) ) ,
335
+ )
324
336
. await ?;
325
337
326
- repo. browser_session ( )
327
- . authenticate_with_password ( & mut rng, & clock, & session, & user_password)
338
+ let registration = repo
339
+ . user_registration ( )
340
+ . set_email_authentication ( registration, & user_email_authentication)
328
341
. await ?;
329
342
330
- repo. queue_job ( )
331
- . schedule_job ( & mut rng, & clock, ProvisionUserJob :: new ( & user) )
343
+ // Hash the password
344
+ let password = Zeroizing :: new ( form. password . into_bytes ( ) ) ;
345
+ let ( version, hashed_password) = password_manager. hash ( & mut rng, password) . await ?;
346
+
347
+ // Add the password to the registration
348
+ let registration = repo
349
+ . user_registration ( )
350
+ . set_password ( registration, hashed_password, version)
332
351
. await ?;
333
352
334
353
repo. save ( ) . await ?;
335
354
336
- activity_tracker
337
- . record_browser_session ( & clock, & session)
338
- . await ;
339
-
340
- let cookie_jar = cookie_jar. set_session ( & session) ;
341
- Ok ( ( cookie_jar, url_builder. redirect ( & next) ) . into_response ( ) )
355
+ // TODO: redirect to the next step on the registration
356
+ Ok ( format ! ( "{}" , registration. id) . into_response ( ) )
342
357
}
343
358
344
359
async fn render (
0 commit comments