unify LoginSuccess and RegisterSucccess into TokenResponse

This commit is contained in:
DSeeLP 2025-03-11 11:12:56 +01:00
parent 83c5768935
commit 2988cc09aa
2 changed files with 8 additions and 14 deletions

View File

@ -25,7 +25,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/LoginSuccess'
$ref: '#/components/schemas/TokenResponse'
default:
$ref: '#/components/responses/Default'
/api/register:
@ -44,7 +44,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/RegisterSuccess'
$ref: '#/components/schemas/TokenResponse'
409:
description: User already exists
content:

View File

@ -30,7 +30,7 @@ pub(super) fn router() -> Router<Arc<AppState>> {
.route("/login", post(login))
}
make_schemas!((Credentials); (RegisterSuccess, LoginSuccess));
make_schemas!((Credentials); (TokenResponse));
#[derive(Deserialize, Validate)]
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
@ -54,18 +54,18 @@ impl std::fmt::Debug for Credentials {
async fn register(
EState(state): State,
Json(credentials): Json<Credentials>,
) -> Result<(StatusCode, Json<RegisterSuccess>), Error> {
) -> Result<(StatusCode, Json<TokenResponse>), Error> {
let mut conn = state.conn().await?;
let id = User::create(&mut conn, &credentials.name, &credentials.password).await?;
let token = Claims::new(id).encode(&state.encoding_key).unwrap();
Ok((StatusCode::CREATED, Json(RegisterSuccess { token })))
Ok((StatusCode::CREATED, Json(TokenResponse { token })))
}
#[instrument(skip(state))]
async fn login(
EState(state): State,
Json(credentials): Json<Credentials>,
) -> Result<Json<LoginSuccess>, Error> {
) -> Result<Json<TokenResponse>, Error> {
let conn = state.conn().await?;
let Some((hash, info)) =
User::get_password_and_info_by_username(&conn, &credentials.name).await?
@ -79,18 +79,12 @@ async fn login(
password_auth::VerifyError::PasswordInvalid => invalid_username_or_password().into(),
})?;
let jwt = Claims::new(info.id).encode(&state.encoding_key).unwrap();
Ok(Json(LoginSuccess { token: jwt }))
Ok(Json(TokenResponse { token: jwt }))
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
pub struct LoginSuccess {
token: String,
}
#[derive(Serialize)]
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
pub struct RegisterSuccess {
pub struct TokenResponse {
token: String,
}