Skip to content

Commit 7d0c8e8

Browse files
committed
add cors
1 parent da02858 commit 7d0c8e8

4 files changed

Lines changed: 13 additions & 2 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bigdecimal = { version = "0.4", features = ["serde"] }
3131
utoipa = { version = "5.4.0", features = ["macros", "axum_extras", "chrono", "uuid", "decimal"] }
3232
utoipa-swagger-ui = { version = "9", features = ["axum"] }
3333
tower-cookies = "0.5"
34+
tower-http = { version = "0.6.2", features = ["cors"] }
3435
r2d2 = "0.8.10"
3536
resend-rs = "0.19.0"
3637
hex = "0.4.3"

src/auth/socials.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub fn login_or_register_social_user(
1111
conn: &mut PgConnection,
1212
profile: SocialProfile,
1313
provider: &str,
14+
role: Option<Role>,
1415
) -> Result<User, AppError> {
1516
use crate::schema::{social_accounts, users};
1617

@@ -50,7 +51,7 @@ pub fn login_or_register_social_user(
5051
dob: None,
5152
image_url: None,
5253
password_hash: "", // IMPORTANT: no password
53-
role: Role::Patient,
54+
role: role.unwrap_or(Role::Patient),
5455
consultation_preference: None,
5556
};
5657

src/handlers/socials.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
auth::{socials, service as auth},
88
error::AppError,
99
handlers::auth::AuthResponse,
10-
utils::response::ApiResponse,
10+
utils::{response::ApiResponse, enums::Role},
1111
};
1212

1313
#[derive(Deserialize)]
@@ -23,6 +23,7 @@ pub struct GoogleTokenInfo {
2323
pub struct SocialLoginRequest {
2424
pub provider: String,
2525
pub access_token: String,
26+
pub role: Option<Role>,
2627
}
2728

2829
#[derive(Serialize, ToSchema)]
@@ -62,6 +63,7 @@ pub async fn social_login(
6263
&mut conn,
6364
profile,
6465
&payload.provider,
66+
payload.role,
6567
)?;
6668

6769
let refresh_token = auth::create_refresh_token(&mut conn, user.id)?;

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod utils;
1919
use axum::{Router, http::StatusCode, routing::get};
2020
use std::net::SocketAddr;
2121
use tracing_subscriber;
22+
use tower_http::cors::{Any, CorsLayer};
2223
use utoipa::OpenApi;
2324
use utoipa_swagger_ui::SwaggerUi;
2425

@@ -50,6 +51,11 @@ async fn main() -> anyhow::Result<()> {
5051
cloudinary_service,
5152
};
5253

54+
let cors = CorsLayer::new()
55+
.allow_origin(Any) // For development, we can allow any origin. For production, specify origins.
56+
.allow_methods(Any)
57+
.allow_headers(Any);
58+
5359
let app = Router::new()
5460
.merge(SwaggerUi::new("/docs").url("/api-docs/openapi.json", ApiDoc::openapi()))
5561
.nest("/api", routes::create_router())
@@ -58,6 +64,7 @@ async fn main() -> anyhow::Result<()> {
5864
get(|| async { (StatusCode::OK, "The health is healthing! ...") }),
5965
)
6066
.layer(axum::Extension(state.clone()))
67+
.layer(cors)
6168
.with_state(state);
6269

6370
let addr: SocketAddr = cfg.bind_addr.parse()?;

0 commit comments

Comments
 (0)