feat: check-token endpoint to verify token validity
This commit is contained in:
parent
af579811a9
commit
eb41bfb899
2 changed files with 25 additions and 0 deletions
|
|
@ -187,6 +187,7 @@ async fn run_server(db: Arc<DatabaseConnection>) {
|
|||
// Authentication
|
||||
.route_layer(middleware::from_fn_with_state(shared_state.clone(), routes::auth::auth_middleware))
|
||||
.routes(routes!(routes::auth::auth))
|
||||
.routes(routes!(routes::auth::check_token))
|
||||
|
||||
.route("/", get(index))
|
||||
.with_state(shared_state)
|
||||
|
|
|
|||
|
|
@ -57,6 +57,30 @@ pub async fn auth(State(state): State<Arc<AppState>>, Json(payload): Json<AuthPa
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, utoipa::ToSchema)]
|
||||
pub struct TokenPayload {
|
||||
token: String
|
||||
}
|
||||
|
||||
#[axum::debug_handler]
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/token-check",
|
||||
request_body = TokenPayload,
|
||||
responses(
|
||||
(status = OK, body = bool, description = "")
|
||||
),
|
||||
summary = "Check if specified token is valid",
|
||||
tag = "auth-api",
|
||||
)]
|
||||
pub async fn check_token(Json(payload): Json<TokenPayload>) -> Json<bool> {
|
||||
let token_data = decode::<Claims>(&payload.token, &KEYS.decoding, &Validation::default());
|
||||
match token_data {
|
||||
Ok(_claims) => Json(true),
|
||||
Err(_) => Json(false)
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthBody {
|
||||
fn new(access_token: String) -> Self {
|
||||
Self {
|
||||
|
|
|
|||
Reference in a new issue