fix(auth): add logging for role assignment debugging

- Enhanced logging for role assignment to track intent/profession
- Better error handling for role insertion
This commit is contained in:
Ashwin Kumar Sivakumar 2026-07-03 17:40:19 +05:30
parent eaae3d470f
commit fda72c8698

View file

@ -316,12 +316,25 @@ async fn register(
payload.intent.as_deref(),
payload.profession.as_deref(),
);
// Log role assignment for debugging
tracing::info!(
user_id = %user.id,
email = %email,
intent = ?payload.intent,
profession = ?payload.profession,
candidates = ?role_candidates,
"Assigning roles after registration"
);
for role_key in role_candidates {
let role_id = ensure_role_exists(&state.pool, &role_key).await;
if let Some(role_id) = role_id {
// For demo accounts, auto-approve the role immediately
let status = if is_demo_account { "APPROVED" } else { "PENDING" };
let _ = sqlx::query(
// Try to update existing assignment first
let update_result = sqlx::query(
r#"
UPDATE user_role_assignments
SET status = $3
@ -333,8 +346,13 @@ async fn register(
.bind(status)
.execute(&state.pool)
.await;
if let Err(e) = &update_result {
tracing::warn!(error = %e, "Failed to update role assignment");
}
let _ = sqlx::query(
// Insert if not exists (UPSERT pattern)
let insert_result = sqlx::query(
r#"
INSERT INTO user_role_assignments (user_id, role_id, status)
SELECT $1, $2, $3
@ -348,7 +366,14 @@ async fn register(
.bind(status)
.execute(&state.pool)
.await;
break;
if let Err(e) = &insert_result {
tracing::error!(error = %e, "Failed to insert role assignment");
} else {
tracing::info!(role_key = %role_key, status = %status, "Role assigned successfully");
}
} else {
tracing::warn!(role_key = %role_key, "Role not found in database");
}
}