mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-24 12:35:31 +00:00
feat: set cookies on ok login request
This commit is contained in:
parent
d65b92e3be
commit
6f608fc8df
3 changed files with 29 additions and 6 deletions
|
|
@ -7,6 +7,7 @@ publish = false
|
||||||
[dependencies]
|
[dependencies]
|
||||||
rocket = { version = "^0.5.0-rc.2", features = [
|
rocket = { version = "^0.5.0-rc.2", features = [
|
||||||
"json",
|
"json",
|
||||||
|
"secrets",
|
||||||
] }
|
] }
|
||||||
|
|
||||||
async-stream = { version = "^0.3" }
|
async-stream = { version = "^0.3" }
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ use portfolio_core::{
|
||||||
services::{admin_service::AdminService, candidate_service::CandidateService},
|
services::{admin_service::AdminService, candidate_service::CandidateService},
|
||||||
};
|
};
|
||||||
use requests::{AdminLoginRequest, RegisterRequest};
|
use requests::{AdminLoginRequest, RegisterRequest};
|
||||||
use rocket::http::Status;
|
use rocket::http::{Cookie, Status, CookieJar};
|
||||||
use rocket::response::status::Custom;
|
use rocket::response::status::Custom;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
|
|
@ -18,6 +18,7 @@ pub async fn login(
|
||||||
conn: Connection<'_, Db>,
|
conn: Connection<'_, Db>,
|
||||||
login_form: Json<AdminLoginRequest>,
|
login_form: Json<AdminLoginRequest>,
|
||||||
ip_addr: SocketAddr,
|
ip_addr: SocketAddr,
|
||||||
|
cookies: &CookieJar<'_>,
|
||||||
) -> Result<String, Custom<String>> {
|
) -> Result<String, Custom<String>> {
|
||||||
let db = conn.into_inner();
|
let db = conn.into_inner();
|
||||||
println!("{} {}", login_form.admin_id, login_form.password);
|
println!("{} {}", login_form.admin_id, login_form.password);
|
||||||
|
|
@ -30,7 +31,18 @@ pub async fn login(
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
session_token.map_err(|e| Custom(Status::from_code(e.code()).unwrap_or_default(), e.message()))
|
if let Err(e) = session_token {
|
||||||
|
return Err(Custom(
|
||||||
|
Status::from_code(e.code()).unwrap_or(Status::InternalServerError),
|
||||||
|
e.to_string(),
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
let session_token = session_token.unwrap();
|
||||||
|
// Todo: Add private?
|
||||||
|
cookies.add(Cookie::new("id", session_token.clone()));
|
||||||
|
|
||||||
|
return Ok(session_token);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/whoami")]
|
#[get("/whoami")]
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::net::SocketAddr;
|
||||||
|
|
||||||
use portfolio_core::services::candidate_service::{CandidateService, UserDetails};
|
use portfolio_core::services::candidate_service::{CandidateService, UserDetails};
|
||||||
use requests::LoginRequest;
|
use requests::LoginRequest;
|
||||||
use rocket::http::Status;
|
use rocket::http::{Cookie, CookieJar, Status};
|
||||||
use rocket::response::status::Custom;
|
use rocket::response::status::Custom;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
|
|
@ -15,6 +15,7 @@ pub async fn login(
|
||||||
conn: Connection<'_, Db>,
|
conn: Connection<'_, Db>,
|
||||||
login_form: Json<LoginRequest>,
|
login_form: Json<LoginRequest>,
|
||||||
ip_addr: SocketAddr,
|
ip_addr: SocketAddr,
|
||||||
|
cookies: &CookieJar<'_>,
|
||||||
) -> Result<String, Custom<String>> {
|
) -> Result<String, Custom<String>> {
|
||||||
let db = conn.into_inner();
|
let db = conn.into_inner();
|
||||||
println!("{} {}", login_form.application_id, login_form.password);
|
println!("{} {}", login_form.application_id, login_form.password);
|
||||||
|
|
@ -27,9 +28,19 @@ pub async fn login(
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
session_token.map_err(|e| Custom(Status::from_code(e.code()).unwrap_or_default(), e.message()))
|
if let Err(e) = session_token {
|
||||||
}
|
return Err(Custom(
|
||||||
|
Status::from_code(e.code()).unwrap_or(Status::InternalServerError),
|
||||||
|
e.to_string(),
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
let session_token = session_token.unwrap();
|
||||||
|
// Todo: Add private?
|
||||||
|
cookies.add(Cookie::new("id", session_token.clone()));
|
||||||
|
|
||||||
|
return Ok(session_token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[get("/whoami")]
|
#[get("/whoami")]
|
||||||
pub async fn whoami(session: CandidateAuth) -> Result<String, Custom<String>> {
|
pub async fn whoami(session: CandidateAuth) -> Result<String, Custom<String>> {
|
||||||
|
|
@ -60,4 +71,3 @@ pub async fn fill_details(
|
||||||
|
|
||||||
Ok("Details added".to_string())
|
Ok("Details added".to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue