mirror of
https://git.dirksys.ovh/dirk/bankserver.git
synced 2025-12-20 19:19:21 +01:00
unify LoginSuccess and RegisterSucccess into TokenResponse
This commit is contained in:
parent
83c5768935
commit
2988cc09aa
@ -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:
|
||||
|
||||
@ -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,
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user