Skip to content

Commit 86d8284

Browse files
committed
refactor(api): rename web module to middlewares and separate route groups
- Fix critical issue regarding auth - Rename web module to middlewares for better semantic clarity - Split routes into public and protected groups - Move /search endpoint to public routes (no auth required) - Group /user, /favorites, and /playlist under protected routes - Update all controller imports to reference new module path
1 parent d71eaa9 commit 86d8284

File tree

9 files changed

+35
-34
lines changed

9 files changed

+35
-34
lines changed

src/controllers/album_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
models::album::{AlbumWithArtists, AlbumWithRelations, AlbumsMetaResponse},
44
models::database_helpers::CountResult,
55
services::album_service::AlbumService,
6-
web::mw_auth::Ctx,
6+
middlewares::mw_auth::Ctx,
77
AppState,
88
Result,
99
};

src/controllers/favorite_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{error::Error, web::mw_auth::Ctx, AppState};
1+
use crate::{error::Error, middlewares::mw_auth::Ctx, AppState};
22
use axum::{
33
extract::{Path, Query, State},
44
Extension, Json,

src/controllers/playlist_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use surrealdb::sql::Thing;
99
use crate::{
1010
models::playlist::{CreatePlaylistRequest, Playlist, PlaylistWithSongs},
1111
services::playlist_service::PlaylistService,
12-
web::mw_auth::Ctx,
12+
middlewares::mw_auth::Ctx,
1313
AppState, Error,
1414
};
1515

src/controllers/song_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
song::SongWithRelations,
77
},
88
services::song_service::{SongService, ListenResult},
9-
web::mw_auth::Ctx,
9+
middlewares::mw_auth::Ctx,
1010
AppState, Error,
1111
};
1212
use axum::{

src/controllers/user_controller.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use axum::{
44
};
55

66
use crate::{
7-
models::user::UserProfile, services::user_service::UserService, web::mw_auth::Ctx, AppState,
7+
models::user::UserProfile, services::user_service::UserService, middlewares::mw_auth::Ctx, AppState,
88
Error,
99
};
1010

src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ mod helpers;
3636
mod models;
3737
mod routes;
3838
mod services;
39-
mod web;
39+
mod middlewares;
4040

4141
#[derive(Clone)]
4242
struct AppState {
@@ -87,17 +87,20 @@ async fn main() -> Result<()> {
8787
.nest("/albums", AlbumRoutes::routes())
8888
.nest("/artists", ArtistRoutes::routes())
8989
.nest("/song", SongRoutes::routes())
90+
.nest("/search", SearchRoutes::routes());
91+
92+
let protected_routes = Router::new()
9093
.nest("/user", UserRoutes::routes())
9194
.nest("/favorites", FavoriteRoutes::routes())
92-
.nest("/search", SearchRoutes::routes())
9395
.nest("/playlist", PlaylistRoutes::routes())
9496
.route_layer(middleware::from_fn_with_state(
9597
app_state.clone(),
96-
web::mw_auth::mw_auth,
98+
middlewares::mw_auth::mw_auth,
9799
));
98100

99101
let routes_all = Router::new()
100102
.nest("/api", routes_api)
103+
.nest("/api", protected_routes)
101104
.with_state(app_state)
102105
.layer(
103106
TraceLayer::new_for_http()
File renamed without changes.
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,28 @@ pub async fn mw_auth(
3131
.headers()
3232
.get(header::AUTHORIZATION)
3333
.and_then(|value| value.to_str().ok())
34-
.and_then(|str| str.strip_prefix("Bearer "));
35-
36-
if let Some(token) = token {
37-
let claims: Claims = TokenService::validate_token(token, &app_state.auth_config)?;
38-
39-
let sub_str = claims.sub.clone();
40-
let user_id = parse_id_part(&sub_str);
41-
let user_thing = create_user_thing(user_id);
42-
43-
let mut result = app_state
44-
.db
45-
.query("SELECT * FROM $user_thing")
46-
.bind(("user_thing", user_thing))
47-
.await?;
48-
let user: Option<UserRecord> = result.take(0)?;
49-
50-
if let Some(user) = user {
51-
let ctx = Ctx::new(claims.sub.clone(), claims.exp as usize, user);
52-
req.extensions_mut().insert(ctx);
53-
} else {
54-
return Err(Error::UserNotFound {
55-
username: claims.sub,
56-
});
57-
}
58-
}
34+
.and_then(|str| str.strip_prefix("Bearer "))
35+
.ok_or(Error::AuthFailNoAuthTokenCookie)?;
36+
37+
let claims: Claims = TokenService::validate_token(token, &app_state.auth_config)?;
38+
39+
let sub_str = claims.sub.clone();
40+
let user_id = parse_id_part(&sub_str);
41+
let user_thing = create_user_thing(user_id);
42+
43+
let mut result = app_state
44+
.db
45+
.query("SELECT * FROM $user_thing")
46+
.bind(("user_thing", user_thing))
47+
.await?;
48+
let user: Option<UserRecord> = result.take(0)?;
49+
50+
let user = user.ok_or(Error::UserNotFound {
51+
username: claims.sub.clone(),
52+
})?;
53+
54+
let ctx = Ctx::new(claims.sub, claims.exp as usize, user);
55+
req.extensions_mut().insert(ctx);
56+
5957
Ok(next.run(req).await)
6058
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use axum::{
88
response::Response,
99
};
1010

11-
use crate::{web::mw_auth::Ctx, AppState};
11+
use crate::{middlewares::mw_auth::Ctx, AppState};
1212

1313
pub async fn rate_limit_middleware(
1414
State(app_state): State<AppState>,

0 commit comments

Comments
 (0)