Skip to content

Commit 5f7ff0b

Browse files
authored
change logic to get user by email first (#44)
1 parent 7641505 commit 5f7ff0b

File tree

1 file changed

+52
-41
lines changed
  • crates/handlers/src/upstream_oauth2

1 file changed

+52
-41
lines changed

crates/handlers/src/upstream_oauth2/link.rs

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -525,29 +525,41 @@ pub(crate) async fn get(
525525
// form, but this lead to poor UX. This is why we do
526526
// it ahead of time here.
527527
//:tchap:
528-
let mut maybe_existing_user =
529-
repo.user().find_by_username(&localpart).await?;
530-
//if not found by username, check by email
528+
let template = provider
529+
.claims_imports
530+
.email
531+
.template
532+
.as_deref()
533+
.unwrap_or(DEFAULT_EMAIL_TEMPLATE);
534+
535+
let maybe_email = render_attribute_template(
536+
&env,
537+
template,
538+
&context,
539+
provider.claims_imports.email.is_required(),
540+
);
541+
542+
let mut maybe_existing_user = if let Ok(Some(email)) = maybe_email {
543+
tchap::search_user_by_email(&mut repo, &email, &tchap_config).await?
544+
} else {
545+
None
546+
};
547+
531548
if maybe_existing_user.is_none() {
532549
let template = provider
533550
.claims_imports
534-
.email
551+
.localpart
535552
.template
536553
.as_deref()
537-
.unwrap_or(DEFAULT_EMAIL_TEMPLATE);
538-
539-
let maybe_email = render_attribute_template(
540-
&env,
541-
template,
542-
&context,
543-
provider.claims_imports.email.is_required(),
544-
);
545-
546-
if let Ok(Some(email)) = maybe_email {
547-
maybe_existing_user =
548-
tchap::search_user_by_email(&mut repo, &email, &tchap_config)
549-
.await?;
550-
}
554+
.unwrap_or(DEFAULT_LOCALPART_TEMPLATE);
555+
556+
let Some(localpart) =
557+
render_attribute_template(&env, template, &context, true)?
558+
else {
559+
// This should never be the case at this point
560+
return Err(RouteError::InvalidFormAction);
561+
};
562+
maybe_existing_user = repo.user().find_by_username(&localpart).await?;
551563
}
552564
//:tchap: end
553565
let is_available = homeserver
@@ -768,41 +780,40 @@ pub(crate) async fn post(
768780
return Err(RouteError::InvalidFormAction);
769781
}
770782

783+
//:tchap:
771784
let template = provider
772785
.claims_imports
773-
.localpart
786+
.email
774787
.template
775788
.as_deref()
776-
.unwrap_or(DEFAULT_LOCALPART_TEMPLATE);
789+
.unwrap_or(DEFAULT_EMAIL_TEMPLATE);
777790

778-
let Some(localpart) = render_attribute_template(&env, template, &context, true)? else {
779-
// This should never be the case at this point
780-
return Err(RouteError::InvalidFormAction);
781-
};
782-
783-
//:tchap:
784-
let mut maybe_user = repo.user().find_by_username(&localpart).await?;
791+
let maybe_email = render_attribute_template(
792+
&env,
793+
template,
794+
&context,
795+
provider.claims_imports.email.is_required(),
796+
);
785797

786-
//if not found by username, check by email
798+
let mut maybe_user = if let Ok(Some(email)) = maybe_email {
799+
tchap::search_user_by_email(&mut repo, &email, &tchap_config).await?
800+
} else {
801+
None
802+
};
787803
if maybe_user.is_none() {
788804
let template = provider
789805
.claims_imports
790-
.email
806+
.localpart
791807
.template
792808
.as_deref()
793-
.unwrap_or(DEFAULT_EMAIL_TEMPLATE);
794-
795-
let maybe_email = render_attribute_template(
796-
&env,
797-
template,
798-
&context,
799-
provider.claims_imports.email.is_required(),
800-
);
809+
.unwrap_or(DEFAULT_LOCALPART_TEMPLATE);
801810

802-
if let Ok(Some(email)) = maybe_email {
803-
maybe_user =
804-
tchap::search_user_by_email(&mut repo, &email, &tchap_config).await?;
805-
}
811+
let Some(localpart) = render_attribute_template(&env, template, &context, true)?
812+
else {
813+
// This should never be the case at this point
814+
return Err(RouteError::InvalidFormAction);
815+
};
816+
maybe_user = repo.user().find_by_username(&localpart).await?;
806817
}
807818

808819
let Some(user) = maybe_user else {

0 commit comments

Comments
 (0)