users: add a route to fetch the user from cookie

Signed-off-by: ATechnoHazard <amolele@gmail.com>
This commit is contained in:
Amogh Lele 2020-09-26 16:17:37 +05:30
parent beaca24246
commit 28fe55b8db
No known key found for this signature in database
GPG Key ID: F475143EDEDEBA3C

View File

@ -4,6 +4,7 @@ use rocket::{
response::status,
Rocket,
};
use rocket::http::Cookies;
use rocket::response::status::Custom;
use rocket_contrib::json::Json;
use serde_json::Value;
@ -12,6 +13,7 @@ use crate::api::catchers::{internal_server_error, unprocessable_entity};
use crate::api::guards::db::DbConn;
use crate::core::users::entity::User;
use crate::core::users::service::{activate_user, create_or_fetch_user, fetch_user};
use crate::utils::users::get_session_id;
#[post("/", data = "<user>")]
fn activate(mut user: Json<User>, conn: DbConn) -> Custom<Json<Value>> {
@ -45,6 +47,12 @@ fn fetch(id: String, conn: DbConn) -> Result<Json<User>> {
Ok(Json(fetch_user(id, &conn)?))
}
pub fn fuel(rocket: Rocket) -> Rocket {
rocket.mount("/api/user", routes![activate, fetch])
#[get("/me")]
fn fetch_me(conn: DbConn, mut ck: Cookies) -> Result<Json<User>> {
let id = get_session_id(&mut ck);
Ok(Json(fetch_user(id, &conn)?))
}
pub fn fuel(rocket: Rocket) -> Rocket {
rocket.mount("/api/user", routes![activate, fetch, fetch_me])
}