u/src/routes/new_key.rs
2023-04-28 13:19:02 -04:00

18 lines
532 B
Rust

use crate::{auth::Auth, state::ReqState};
use axum::{extract::State, response::IntoResponse};
use hyper::StatusCode;
pub async fn post(State(state): ReqState, Auth(auth): Auth) -> impl IntoResponse {
if !auth.admin {
return Err(StatusCode::UNAUTHORIZED);
}
let key = crate::utils::gen_key();
sqlx::query!("insert into api_keys (key, admin) values ($1, false)", key)
.execute(&state.db)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok((StatusCode::CREATED, key))
}