From 6783bd22a73cd86b905ac8690c83a8c4ef19a48e Mon Sep 17 00:00:00 2001 From: Radical Date: Sat, 31 May 2025 17:11:14 +0200 Subject: [PATCH] feat: add backend_url config option Required for refresh_token cookie to work properly --- src/api/v1/auth/login.rs | 2 +- src/api/v1/auth/refresh.rs | 2 +- src/api/v1/auth/register.rs | 2 +- src/config.rs | 8 ++++---- src/main.rs | 2 +- src/utils.rs | 10 ++++------ 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/api/v1/auth/login.rs b/src/api/v1/auth/login.rs index 04d6b4f..e190c2f 100644 --- a/src/api/v1/auth/login.rs +++ b/src/api/v1/auth/login.rs @@ -89,6 +89,6 @@ pub async fn response( .await?; Ok(HttpResponse::Ok() - .cookie(new_refresh_token_cookie(refresh_token)) + .cookie(new_refresh_token_cookie(&data.config, refresh_token)) .json(Response { access_token })) } diff --git a/src/api/v1/auth/refresh.rs b/src/api/v1/auth/refresh.rs index cc3bbe9..69bf248 100644 --- a/src/api/v1/auth/refresh.rs +++ b/src/api/v1/auth/refresh.rs @@ -85,7 +85,7 @@ pub async fn res(req: HttpRequest, data: web::Data) -> Result, port: Option, - base_path: Option, frontend_url: Url, + backend_url: Option, _ssl: Option, } @@ -86,8 +86,8 @@ impl ConfigBuilder { let web = Web { ip: self.web.ip.unwrap_or(String::from("0.0.0.0")), port: self.web.port.unwrap_or(8080), - base_path: self.web.base_path.unwrap_or("".to_string()), - frontend_url: self.web.frontend_url, + frontend_url: self.web.frontend_url.clone(), + backend_url: self.web.backend_url.or_else(|| self.web.frontend_url.join("/api").ok()).unwrap(), }; let endpoint = match &*self.bunny.endpoint { @@ -148,8 +148,8 @@ pub struct Config { pub struct Web { pub ip: String, pub port: u16, - pub base_path: String, pub frontend_url: Url, + pub backend_url: Url, } #[derive(Debug, Clone)] diff --git a/src/main.rs b/src/main.rs index 892c79d..d026f55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -155,7 +155,7 @@ async fn main() -> Result<(), Error> { App::new() .app_data(web::Data::new(data.clone())) .wrap(cors) - .service(api::web(&data.config.web.base_path)) + .service(api::web(&data.config.web.backend_url.path())) }) .bind((web.ip, web.port))? .run() diff --git a/src/utils.rs b/src/utils.rs index 3241dee..f393bf5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -16,10 +16,7 @@ use serde::Serialize; use uuid::Uuid; use crate::{ - Conn, Data, - error::Error, - schema::users, - structs::{HasIsAbove, HasUuid}, + config::Config, error::Error, schema::users, structs::{HasIsAbove, HasUuid}, Conn, Data }; pub static EMAIL_REGEX: LazyLock = LazyLock::new(|| { @@ -100,12 +97,13 @@ pub fn get_ws_protocol_header(headers: &HeaderMap) -> Result<&str, Error> { Ok(auth_value.unwrap()) } -pub fn new_refresh_token_cookie(refresh_token: String) -> Cookie<'static> { +pub fn new_refresh_token_cookie(config: &Config, refresh_token: String) -> Cookie<'static> { Cookie::build("refresh_token", refresh_token) .http_only(true) .secure(true) .same_site(SameSite::None) - .path("/api") + .domain(config.web.backend_url.domain().unwrap().to_string()) + .path(config.web.backend_url.path().to_string()) .max_age(Duration::days(30)) .finish() }