1
0
Fork 0
forked from gorb/backend

feat: add backend_url config option

Required for refresh_token cookie to work properly
This commit is contained in:
Radical 2025-05-31 17:11:14 +02:00
parent 4fce262551
commit 6783bd22a7
6 changed files with 12 additions and 14 deletions

View file

@ -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 }))
}

View file

@ -85,7 +85,7 @@ pub async fn res(req: HttpRequest, data: web::Data<Data>) -> Result<HttpResponse
.await?;
return Ok(HttpResponse::Ok()
.cookie(new_refresh_token_cookie(refresh_token))
.cookie(new_refresh_token_cookie(&data.config, refresh_token))
.json(Response { access_token }));
}

View file

@ -146,7 +146,7 @@ pub async fn res(
.await?;
return Ok(HttpResponse::Ok()
.cookie(new_refresh_token_cookie(refresh_token))
.cookie(new_refresh_token_cookie(&data.config, refresh_token))
.json(Response { access_token }));
}

View file

@ -38,8 +38,8 @@ pub struct CacheDatabase {
struct WebBuilder {
ip: Option<String>,
port: Option<u16>,
base_path: Option<String>,
frontend_url: Url,
backend_url: Option<Url>,
_ssl: Option<bool>,
}
@ -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)]

View file

@ -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()

View file

@ -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<Regex> = 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()
}