forked from Chewing_Bever/rusty-bever
Added admin users route
parent
d7333373bb
commit
b45c93cdc9
|
@ -0,0 +1,8 @@
|
||||||
|
use crate::errors::RBError;
|
||||||
|
use crate::models::User;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use crate::schema::users::dsl as users;
|
||||||
|
|
||||||
|
pub fn get_users(conn: &PgConnection) -> crate::Result<Vec<User>> {
|
||||||
|
users::users.load::<User>(conn).map_err(|_| RBError::DBError)
|
||||||
|
}
|
|
@ -37,7 +37,9 @@ impl<'r> Responder<'r, 'static> for RBError {
|
||||||
RBError::JWTCreationError | RBError::MissingJWTKey => {
|
RBError::JWTCreationError | RBError::MissingJWTKey => {
|
||||||
(Status::InternalServerError, "Failed to create tokens.")
|
(Status::InternalServerError, "Failed to create tokens.")
|
||||||
}
|
}
|
||||||
RBError::InvalidRefreshToken | RBError::DuplicateRefreshToken => (Status::Unauthorized, "Invalid refresh token."),
|
RBError::InvalidRefreshToken | RBError::DuplicateRefreshToken => {
|
||||||
|
(Status::Unauthorized, "Invalid refresh token.")
|
||||||
|
}
|
||||||
_ => (Status::InternalServerError, "Internal server error"),
|
_ => (Status::InternalServerError, "Internal server error"),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,8 @@ extern crate diesel;
|
||||||
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
mod models;
|
pub mod admin;
|
||||||
|
pub mod models;
|
||||||
pub(crate) mod schema;
|
pub(crate) mod schema;
|
||||||
|
|
||||||
pub use errors::Result;
|
pub use errors::Result;
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
use crate::guards::Admin;
|
||||||
|
use crate::RbDbConn;
|
||||||
|
use rb::models::User;
|
||||||
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
|
pub fn routes() -> Vec<rocket::Route> {
|
||||||
|
routes![get_users]
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/users")]
|
||||||
|
async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<User>>> {
|
||||||
|
Ok(Json(conn.run(|c| rb::admin::get_users(c)).await?))
|
||||||
|
}
|
|
@ -45,6 +45,7 @@ async fn refresh_token(
|
||||||
let refresh_token = refresh_token_request.into_inner().refresh_token;
|
let refresh_token = refresh_token_request.into_inner().refresh_token;
|
||||||
|
|
||||||
Ok(Json(
|
Ok(Json(
|
||||||
conn.run(move |c| rb::auth::refresh_token(c, &refresh_token)).await?
|
conn.run(move |c| rb::auth::refresh_token(c, &refresh_token))
|
||||||
|
.await?,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ use rocket::{fairing::AdHoc, Build, Rocket};
|
||||||
use rocket_sync_db_pools::{database, diesel};
|
use rocket_sync_db_pools::{database, diesel};
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
|
mod admin;
|
||||||
pub(crate) mod guards;
|
pub(crate) mod guards;
|
||||||
|
|
||||||
embed_migrations!();
|
embed_migrations!();
|
||||||
|
@ -55,4 +56,5 @@ fn rocket() -> _ {
|
||||||
))
|
))
|
||||||
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
.attach(AdHoc::try_on_ignite("Create admin user", create_admin_user))
|
||||||
.mount("/api/auth", auth::routes())
|
.mount("/api/auth", auth::routes())
|
||||||
|
.mount("/api/admin", admin::routes())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue