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:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/LoginSuccess'
|
$ref: '#/components/schemas/TokenResponse'
|
||||||
default:
|
default:
|
||||||
$ref: '#/components/responses/Default'
|
$ref: '#/components/responses/Default'
|
||||||
/api/register:
|
/api/register:
|
||||||
@ -44,7 +44,7 @@ paths:
|
|||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/RegisterSuccess'
|
$ref: '#/components/schemas/TokenResponse'
|
||||||
409:
|
409:
|
||||||
description: User already exists
|
description: User already exists
|
||||||
content:
|
content:
|
||||||
|
|||||||
@ -30,7 +30,7 @@ pub(super) fn router() -> Router<Arc<AppState>> {
|
|||||||
.route("/login", post(login))
|
.route("/login", post(login))
|
||||||
}
|
}
|
||||||
|
|
||||||
make_schemas!((Credentials); (RegisterSuccess, LoginSuccess));
|
make_schemas!((Credentials); (TokenResponse));
|
||||||
|
|
||||||
#[derive(Deserialize, Validate)]
|
#[derive(Deserialize, Validate)]
|
||||||
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
|
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
|
||||||
@ -54,18 +54,18 @@ impl std::fmt::Debug for Credentials {
|
|||||||
async fn register(
|
async fn register(
|
||||||
EState(state): State,
|
EState(state): State,
|
||||||
Json(credentials): Json<Credentials>,
|
Json(credentials): Json<Credentials>,
|
||||||
) -> Result<(StatusCode, Json<RegisterSuccess>), Error> {
|
) -> Result<(StatusCode, Json<TokenResponse>), Error> {
|
||||||
let mut conn = state.conn().await?;
|
let mut conn = state.conn().await?;
|
||||||
let id = User::create(&mut conn, &credentials.name, &credentials.password).await?;
|
let id = User::create(&mut conn, &credentials.name, &credentials.password).await?;
|
||||||
let token = Claims::new(id).encode(&state.encoding_key).unwrap();
|
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))]
|
#[instrument(skip(state))]
|
||||||
async fn login(
|
async fn login(
|
||||||
EState(state): State,
|
EState(state): State,
|
||||||
Json(credentials): Json<Credentials>,
|
Json(credentials): Json<Credentials>,
|
||||||
) -> Result<Json<LoginSuccess>, Error> {
|
) -> Result<Json<TokenResponse>, Error> {
|
||||||
let conn = state.conn().await?;
|
let conn = state.conn().await?;
|
||||||
let Some((hash, info)) =
|
let Some((hash, info)) =
|
||||||
User::get_password_and_info_by_username(&conn, &credentials.name).await?
|
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(),
|
password_auth::VerifyError::PasswordInvalid => invalid_username_or_password().into(),
|
||||||
})?;
|
})?;
|
||||||
let jwt = Claims::new(info.id).encode(&state.encoding_key).unwrap();
|
let jwt = Claims::new(info.id).encode(&state.encoding_key).unwrap();
|
||||||
Ok(Json(LoginSuccess { token: jwt }))
|
Ok(Json(TokenResponse { token: jwt }))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
|
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
|
||||||
pub struct LoginSuccess {
|
pub struct TokenResponse {
|
||||||
token: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
|
||||||
#[cfg_attr(feature = "schemas", derive(schemars::JsonSchema))]
|
|
||||||
pub struct RegisterSuccess {
|
|
||||||
token: String,
|
token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user