Skip to content

Commit b024dba

Browse files
committed
feat: Add JWT authentication and authorization module, and comment API handlers.
1 parent 3ad9470 commit b024dba

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

backend/src/auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub fn build_auth_cookie(token: &str) -> Cookie<'static> {
301301
.path("/")
302302
.http_only(true)
303303
.same_site(SameSite::Lax)
304-
.max_age(Duration::seconds(AUTH_COOKIE_TTL_SECONDS));
304+
.max_age(cookie::time::Duration::seconds(AUTH_COOKIE_TTL_SECONDS));
305305

306306
// Add Secure flag in production (HTTPS only)
307307
if cookies_should_be_secure() {
@@ -331,7 +331,7 @@ pub fn build_cookie_removal() -> Cookie<'static> {
331331
.http_only(true)
332332
.same_site(SameSite::Lax)
333333
.expires(OffsetDateTime::UNIX_EPOCH)
334-
.max_age(Duration::seconds(0));
334+
.max_age(cookie::time::Duration::seconds(0));
335335

336336
// Match security settings of auth cookie
337337
if cookies_should_be_secure() {

backend/src/handlers/comments.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,18 @@ pub async fn list_comments(
146146
)
147147
})?;
148148

149-
Ok(Json(comments))
149+
let response_comments: Vec<Comment> = comments.into_iter().map(|c| Comment {
150+
id: c.id,
151+
tutorial_id: c.tutorial_id,
152+
post_id: c.post_id,
153+
author: c.author,
154+
content: c.content,
155+
created_at: c.created_at,
156+
votes: c.votes,
157+
is_admin: c.is_admin,
158+
}).collect();
159+
160+
Ok(Json(response_comments))
150161
}
151162

152163
pub async fn create_comment(
@@ -202,7 +213,18 @@ pub async fn list_post_comments(
202213
(StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: "Failed to fetch comments".to_string() }))
203214
})?;
204215

205-
Ok(Json(comments))
216+
let response_comments: Vec<Comment> = comments.into_iter().map(|c| Comment {
217+
id: c.id,
218+
tutorial_id: c.tutorial_id,
219+
post_id: c.post_id,
220+
author: c.author,
221+
content: c.content,
222+
created_at: c.created_at,
223+
votes: c.votes,
224+
is_admin: c.is_admin,
225+
}).collect();
226+
227+
Ok(Json(response_comments))
206228
}
207229

208230
pub async fn create_post_comment(
@@ -301,7 +323,18 @@ async fn create_comment_internal(
301323
(StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: "Failed to create comment".to_string() }))
302324
})?;
303325

304-
Ok(Json(comment))
326+
let response_comment = Comment {
327+
id: comment.id,
328+
tutorial_id: comment.tutorial_id,
329+
post_id: comment.post_id,
330+
author: comment.author,
331+
content: comment.content,
332+
created_at: comment.created_at,
333+
votes: comment.votes,
334+
is_admin: comment.is_admin,
335+
};
336+
337+
Ok(Json(response_comment))
305338
}
306339

307340
pub async fn delete_comment(

0 commit comments

Comments
 (0)