add the ability to register from inside the user-ui
This commit is contained in:
parent
31e01292f9
commit
ed1a1f10f9
12 changed files with 655 additions and 5 deletions
|
|
@ -9,7 +9,7 @@ use chattyness_db::{
|
|||
AccountStatus, AuthenticatedUser, CurrentUserResponse, GuestLoginRequest,
|
||||
GuestLoginResponse, JoinRealmRequest, JoinRealmResponse, LoginRequest, LoginResponse,
|
||||
LoginType, PasswordResetRequest, PasswordResetResponse, RealmRole, RealmSummary,
|
||||
SignupRequest, SignupResponse, UserSummary,
|
||||
RegisterGuestRequest, RegisterGuestResponse, SignupRequest, SignupResponse, UserSummary,
|
||||
},
|
||||
queries::{guests, memberships, realms, users},
|
||||
};
|
||||
|
|
@ -471,3 +471,62 @@ pub async fn reset_password(
|
|||
redirect_url,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Register guest handler.
|
||||
///
|
||||
/// Upgrades a guest user to a full user account with username and password.
|
||||
pub async fn register_guest(
|
||||
rls_conn: crate::auth::RlsConn,
|
||||
State(pool): State<PgPool>,
|
||||
session: Session,
|
||||
AuthUser(user): AuthUser,
|
||||
Json(req): Json<RegisterGuestRequest>,
|
||||
) -> Result<Json<RegisterGuestResponse>, AppError> {
|
||||
// Validate the request
|
||||
req.validate()?;
|
||||
|
||||
// Verify user is a guest
|
||||
if !user.is_guest() {
|
||||
return Err(AppError::Forbidden(
|
||||
"Only guests can register for an account".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
// Check username availability
|
||||
if users::username_exists(&pool, &req.username).await? {
|
||||
return Err(AppError::Conflict("Username already taken".to_string()));
|
||||
}
|
||||
|
||||
// Check email availability if provided
|
||||
if let Some(ref email) = req.email {
|
||||
let email_trimmed = email.trim();
|
||||
if !email_trimmed.is_empty() && users::email_exists(&pool, email_trimmed).await? {
|
||||
return Err(AppError::Conflict("Email already registered".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
// Get email as Option<&str>
|
||||
let email_opt = req.email.as_ref().and_then(|e| {
|
||||
let trimmed = e.trim();
|
||||
if trimmed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(trimmed)
|
||||
}
|
||||
});
|
||||
|
||||
// Upgrade the guest to a full user using RLS connection
|
||||
let mut conn = rls_conn.acquire().await;
|
||||
users::upgrade_guest_to_user_conn(&mut *conn, user.id, &req.username, &req.password, email_opt).await?;
|
||||
|
||||
// Update session login type from 'guest' to 'realm'
|
||||
session
|
||||
.insert(SESSION_LOGIN_TYPE_KEY, "realm")
|
||||
.await
|
||||
.map_err(|e| AppError::Internal(format!("Session error: {}", e)))?;
|
||||
|
||||
Ok(Json(RegisterGuestResponse {
|
||||
success: true,
|
||||
username: req.username,
|
||||
}))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue