commit 40b75fdd33fae9d47d7e4b356c6f9c2d7bc1eb59
parent 6e662c2483db46baaf7efc68281004e97253dee7
Author: Silas Brack <silasbrack@gmail.com>
Date: Sun, 7 Jun 2026 18:03:11 +0200
fix: include user_id in profile creation and auto-create on login
- create_user_profile now sends user_id from the auth token
- Login handler auto-creates a profile if the user doesn't have one,
using the email prefix as the username
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Diffstat:
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/src/handlers/auth.rs b/src/handlers/auth.rs
@@ -70,6 +70,15 @@ pub async fn login(
match trailbase::login(&client, &form.email, &form.password).await {
Ok(tokens) => {
+ // Ensure user has a profile, create one if missing
+ if let Ok(auth_client) = trailbase::client_with_tokens(&state.trailbase_url, &tokens) {
+ if let Some(user_id) = trailbase::extract_user_id_from_token(&tokens.auth_token) {
+ if trailbase::get_user_profile_by_id(&auth_client, &user_id).await.ok().flatten().is_none() {
+ let username = form.email.split('@').next().unwrap_or("user");
+ let _ = trailbase::create_user_profile(&auth_client, username).await;
+ }
+ }
+ }
let jar = set_auth_cookie(jar, &tokens);
Ok((jar, Redirect::to("/")))
}
diff --git a/src/trailbase.rs b/src/trailbase.rs
@@ -493,17 +493,23 @@ pub async fn get_user_profile_by_id(
#[derive(Debug, Serialize)]
pub struct CreateUserProfile {
pub username: String,
+ pub user_id: String,
}
pub async fn create_user_profile(
client: &Client,
username: &str,
) -> Result<UserProfile, ClientError> {
+ let user_id = client
+ .tokens()
+ .and_then(|t| extract_user_id_from_token(&t.auth_token))
+ .ok_or_else(|| ClientError::Auth("Not authenticated".into()))?;
+
let profile = CreateUserProfile {
username: username.to_string(),
+ user_id,
};
client.records("user_profile").create(&profile).await?;
- // Fetch the created profile
get_user_profile_by_username(client, username).await?.ok_or_else(|| {
ClientError::Auth("Failed to retrieve created profile".to_string())
})