fix(backend): Split public and protected routes to fix 401 errors

This commit is contained in:
goose 2026-02-15 15:44:01 -03:00
parent e5d0ae4fd1
commit 26f0df58ef
3 changed files with 138 additions and 6 deletions

View file

@ -75,27 +75,35 @@ async fn main() -> anyhow::Result<()> {
};
eprintln!("Building router...");
let app = Router::new()
// Public endpoints (no auth required)
// Create separate routers for public and protected routes
let public_routes = Router::new()
.route("/health", get(handlers::health_check))
.route("/ready", get(handlers::ready_check))
.route("/api/auth/register", post(handlers::register))
.route("/api/auth/login", post(handlers::login))
.route("/api/auth/refresh", post(handlers::refresh_token))
.route("/api/auth/logout", post(handlers::logout))
// Protected endpoints (auth required)
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(CorsLayer::new())
);
let protected_routes = Router::new()
.route("/api/users/me", get(handlers::get_profile))
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
.layer(CorsLayer::new())
)
// Apply auth middleware to all routes
.route_layer(axum_middleware::from_fn_with_state(
app_state.clone(),
crate::middleware::auth::jwt_auth_middleware
))
.with_state(app_state);
));
// Merge public and protected routes
let app = public_routes.merge(protected_routes).with_state(app_state);
eprintln!("Binding to {}:{}...", config.server.host, config.server.port);
let listener = tokio::net::TcpListener::bind(&format!("{}:{}", config.server.host, config.server.port))