@@ -24,8 +24,8 @@ use mas_matrix::BoxHomeserverConnection;
2424use mas_policy:: Policy ;
2525use mas_router:: UrlBuilder ;
2626use mas_storage:: {
27- queue:: { ProvisionUserJob , QueueJobRepositoryExt as _} ,
28- user:: { BrowserSessionRepository , UserEmailRepository , UserPasswordRepository , UserRepository } ,
27+ queue:: { QueueJobRepositoryExt as _, SendEmailAuthenticationCodeJob } ,
28+ user:: { UserEmailRepository , UserRepository } ,
2929 BoxClock , BoxRepository , BoxRng , RepositoryAccess ,
3030} ;
3131use mas_templates:: {
@@ -141,6 +141,8 @@ pub(crate) async fn post(
141141 Form ( form) : Form < ProtectedForm < RegisterForm > > ,
142142) -> Result < Response , FancyError > {
143143 let user_agent = user_agent. map ( |ua| UserAgent :: parse ( ua. as_str ( ) . to_owned ( ) ) ) ;
144+
145+ let ip_address = activity_tracker. ip ( ) ;
144146 if !site_config. password_registration_enabled {
145147 return Ok ( StatusCode :: METHOD_NOT_ALLOWED . into_response ( ) ) ;
146148 }
@@ -296,49 +298,62 @@ pub(crate) async fn post(
296298 return Ok ( ( cookie_jar, Html ( content) ) . into_response ( ) ) ;
297299 }
298300
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 ?;
306309
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 )
312313 . await ?;
313314
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
315325 . user_email ( )
316- . add ( & mut rng, & clock, & user , form. email )
326+ . add_authentication_for_registration ( & mut rng, & clock, form. email , & registration )
317327 . await ?;
318328
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+ )
324336 . await ?;
325337
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)
328341 . await ?;
329342
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)
332351 . await ?;
333352
334353 repo. save ( ) . await ?;
335354
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 ( ) )
342357}
343358
344359async fn render (
0 commit comments