Conversation
sample_data.ts was a leftover placeholder predating real DB-backed task/answer fetching; nothing referenced it besides a commented-out import.
…e session guards module
Consolidates src/lib/server/{auth,session,password,random}.ts into
src/features/auth/server/ to keep auth-domain code colocated per the
feature-scoped structure. Renames services/session.ts to
session_guards.ts to reflect its actual responsibility (guard/redirect
helpers) and disambiguate from the new server/session.ts.
… document server/ convention Completes the src/lib/server -> src/features/auth/server migration by fixing the remaining consumers (hooks, app.d.ts, seed, auth routes, credentials service). Also documents the feature-scoped server/ subdirectory convention in architecture.md.
📝 WalkthroughWalkthrough認証のサーバー実装を Changes認証サーバー基盤
セッションガード
認証参照先の切り替え
配置規約と不要記述
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
.claude/rules/auth.md (1)
38-43: 📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy liftサービス層の責務と記載を一致させてください。
src/features/auth/services/配下の関数がredirect()/error()を実行する設計は、サービスを framework-agnostic な値/null返却に限定する規約と矛盾します。ガードは判定結果を返し、リダイレクトや HTTP エラー変換は各+page.server.ts/+server.tsで行う形に、実装とドキュメントを揃えてください。As per coding guidelines and path instructions, services must return values or
nulland must not callredirect()orerror().🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In @.claude/rules/auth.md around lines 38 - 43, Update the authentication service functions described in the auth documentation, including getLoggedInUser, ensureSessionOrRedirect, validateAdminAccess, and validateAdminAccessForApi, so they remain framework-agnostic and return users, authorization results, or null instead of calling redirect() or error(). Move redirect and HTTP error translation into the corresponding +page.server.ts and +server.ts callers, and revise the documented contracts to match.Sources: Coding guidelines, Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/features/auth/services/session_guards.ts`:
- Around line 24-49: Change getLoggedInUser in
src/features/auth/services/session_guards.ts (lines 24-49) to return
App.Locals['user'] | null without accepting url or redirecting, and remove
ensureSessionOrRedirect; update
src/features/auth/services/session_guards.test.ts (line 19) to remove its test
and cover the null result. In src/routes/problems/[slug]/+page.server.ts (lines
14 and 27), src/routes/users/edit/+page.server.ts (line 19), and
src/routes/workbooks/edit/[slug]/+page.server.ts (lines 24 and 82), add explicit
post-getLoggedInUser redirects for missing users with the required
buildLoginPath import. In src/routes/workbooks/create/+page.server.ts (lines
18-20), replace ensureSessionOrRedirect with getLoggedInUser, redirecting when
the returned author is null.
---
Outside diff comments:
In @.claude/rules/auth.md:
- Around line 38-43: Update the authentication service functions described in
the auth documentation, including getLoggedInUser, ensureSessionOrRedirect,
validateAdminAccess, and validateAdminAccessForApi, so they remain
framework-agnostic and return users, authorization results, or null instead of
calling redirect() or error(). Move redirect and HTTP error translation into the
corresponding +page.server.ts and +server.ts callers, and revise the documented
contracts to match.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 7ed40603-c80b-44b9-a784-f1edef606d3d
📒 Files selected for processing (29)
.claude/rules/auth.mddocs/guides/architecture.mdprisma/seed.tssrc/app.d.tssrc/features/auth/server/auth.test.tssrc/features/auth/server/auth.tssrc/features/auth/server/password.test.tssrc/features/auth/server/password.tssrc/features/auth/server/random.test.tssrc/features/auth/server/random.tssrc/features/auth/server/session.test.tssrc/features/auth/server/session.tssrc/features/auth/services/credentials.test.tssrc/features/auth/services/credentials.tssrc/features/auth/services/session_guards.test.tssrc/features/auth/services/session_guards.tssrc/hooks.server.tssrc/lib/components/TagListForEdit.sveltesrc/lib/server/sample_data.tssrc/routes/(auth)/login/+page.server.tssrc/routes/(auth)/logout/+page.server.tssrc/routes/(auth)/signup/+page.server.tssrc/routes/problems/[slug]/+page.server.tssrc/routes/users/[username]/+page.server.tssrc/routes/users/edit/+page.server.tssrc/routes/workbooks/+page.server.tssrc/routes/workbooks/[slug]/+page.server.tssrc/routes/workbooks/create/+page.server.tssrc/routes/workbooks/edit/[slug]/+page.server.ts
💤 Files with no reviewable changes (2)
- src/lib/server/sample_data.ts
- src/lib/components/TagListForEdit.svelte
There was a problem hiding this comment.
Caution
Inline review comments failed to post. This is likely due to GitHub's internal server error or limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
.claude/rules/auth.md (1)
38-43: 📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy liftサービス層の責務と記載を一致させてください。
src/features/auth/services/配下の関数がredirect()/error()を実行する設計は、サービスを framework-agnostic な値/null返却に限定する規約と矛盾します。ガードは判定結果を返し、リダイレクトや HTTP エラー変換は各+page.server.ts/+server.tsで行う形に、実装とドキュメントを揃えてください。As per coding guidelines and path instructions, services must return values or
nulland must not callredirect()orerror().🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In @.claude/rules/auth.md around lines 38 - 43, Update the authentication service functions described in the auth documentation, including getLoggedInUser, ensureSessionOrRedirect, validateAdminAccess, and validateAdminAccessForApi, so they remain framework-agnostic and return users, authorization results, or null instead of calling redirect() or error(). Move redirect and HTTP error translation into the corresponding +page.server.ts and +server.ts callers, and revise the documented contracts to match.Sources: Coding guidelines, Path instructions
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/features/auth/services/session_guards.ts`:
- Around line 24-49: Change getLoggedInUser in
src/features/auth/services/session_guards.ts (lines 24-49) to return
App.Locals['user'] | null without accepting url or redirecting, and remove
ensureSessionOrRedirect; update
src/features/auth/services/session_guards.test.ts (line 19) to remove its test
and cover the null result. In src/routes/problems/[slug]/+page.server.ts (lines
14 and 27), src/routes/users/edit/+page.server.ts (line 19), and
src/routes/workbooks/edit/[slug]/+page.server.ts (lines 24 and 82), add explicit
post-getLoggedInUser redirects for missing users with the required
buildLoginPath import. In src/routes/workbooks/create/+page.server.ts (lines
18-20), replace ensureSessionOrRedirect with getLoggedInUser, redirecting when
the returned author is null.
---
Outside diff comments:
In @.claude/rules/auth.md:
- Around line 38-43: Update the authentication service functions described in
the auth documentation, including getLoggedInUser, ensureSessionOrRedirect,
validateAdminAccess, and validateAdminAccessForApi, so they remain
framework-agnostic and return users, authorization results, or null instead of
calling redirect() or error(). Move redirect and HTTP error translation into the
corresponding +page.server.ts and +server.ts callers, and revise the documented
contracts to match.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 7ed40603-c80b-44b9-a784-f1edef606d3d
📒 Files selected for processing (29)
.claude/rules/auth.mddocs/guides/architecture.mdprisma/seed.tssrc/app.d.tssrc/features/auth/server/auth.test.tssrc/features/auth/server/auth.tssrc/features/auth/server/password.test.tssrc/features/auth/server/password.tssrc/features/auth/server/random.test.tssrc/features/auth/server/random.tssrc/features/auth/server/session.test.tssrc/features/auth/server/session.tssrc/features/auth/services/credentials.test.tssrc/features/auth/services/credentials.tssrc/features/auth/services/session_guards.test.tssrc/features/auth/services/session_guards.tssrc/hooks.server.tssrc/lib/components/TagListForEdit.sveltesrc/lib/server/sample_data.tssrc/routes/(auth)/login/+page.server.tssrc/routes/(auth)/logout/+page.server.tssrc/routes/(auth)/signup/+page.server.tssrc/routes/problems/[slug]/+page.server.tssrc/routes/users/[username]/+page.server.tssrc/routes/users/edit/+page.server.tssrc/routes/workbooks/+page.server.tssrc/routes/workbooks/[slug]/+page.server.tssrc/routes/workbooks/create/+page.server.tssrc/routes/workbooks/edit/[slug]/+page.server.ts
💤 Files with no reviewable changes (2)
- src/lib/server/sample_data.ts
- src/lib/components/TagListForEdit.svelte
🛑 Comments failed to post (1)
src/features/auth/services/session_guards.ts (1)
24-49: 🎯 Functional Correctness | 🔴 Critical | 🏗️ Heavy lift
サービスのガイドライン違反および未ログインアクセスの破損。
session_guards.ts内でredirect()を呼び出していることは、「サービス層ではerror()やredirect()を呼ばない(HTTPエラー変換はルートハンドラで行う)」というコーディングガイドラインに直接違反しています。
さらに、getLoggedInUserが一律でリダイレクトを投げるようになったことで、これまでnullを許容して未ログインユーザーにコンテンツを表示していたページ(/workbooks、/workbooks/[slug]、/users/[username]など)にアクセスできなくなる致命的なバグを引き起こしています。共有の根本原因を解決するために、サービス層では
nullを返すようにし、セッション必須のルートハンドラ側で明示的にリダイレクトを行ってください。
src/features/auth/services/session_guards.ts#L24-L49:getLoggedInUserをApp.Locals['user'] | nullを返すように変更し(引数urlおよび内部の redirect を削除)、ガイドライン違反となるensureSessionOrRedirectは削除してください。src/features/auth/services/session_guards.test.ts#L19-L19: 実装の変更に合わせて、ensureSessionOrRedirectのテストを削除し、getLoggedInUserがnullを返すケースのテストに修正してください。src/routes/problems/[slug]/+page.server.ts#L14-L14: セッション必須の処理であるため、getLoggedInUserの直後にif (!loggedInUser) redirect(TEMPORARY_REDIRECT, buildLoginPath(url));を追加してください(buildLoginPathの import が必要です)。src/routes/problems/[slug]/+page.server.ts#L27-L27: 同上(action内)。src/routes/users/edit/+page.server.ts#L19-L19: セッション必須のため、getLoggedInUserの直後にif (!loggedInUser) redirect(TEMPORARY_REDIRECT, buildLoginPath(url));を追加してください(要import)。src/routes/workbooks/create/+page.server.ts#L18-L20:ensureSessionOrRedirectを削除し、代わりにconst author = await getLoggedInUser(locals); if (!author) redirect(TEMPORARY_REDIRECT, buildLoginPath(url));としてください(要import)。src/routes/workbooks/edit/[slug]/+page.server.ts#L24-L24: 編集画面にはセッションが必要なため、getLoggedInUserの直後にif (!loggedInUser) redirect(TEMPORARY_REDIRECT, buildLoginPath(url));を追加してください(要import)。src/routes/workbooks/edit/[slug]/+page.server.ts#L82-L82: 同上(action内)。📍 Affects 6 files
src/features/auth/services/session_guards.ts#L24-L49(this comment)src/features/auth/services/session_guards.test.ts#L19-L19src/routes/problems/[slug]/+page.server.ts#L14-L14src/routes/problems/[slug]/+page.server.ts#L27-L27src/routes/users/edit/+page.server.ts#L19-L19src/routes/workbooks/create/+page.server.ts#L18-L20src/routes/workbooks/edit/[slug]/+page.server.ts#L24-L24src/routes/workbooks/edit/[slug]/+page.server.ts#L82-L82🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/features/auth/services/session_guards.ts` around lines 24 - 49, Change getLoggedInUser in src/features/auth/services/session_guards.ts (lines 24-49) to return App.Locals['user'] | null without accepting url or redirecting, and remove ensureSessionOrRedirect; update src/features/auth/services/session_guards.test.ts (line 19) to remove its test and cover the null result. In src/routes/problems/[slug]/+page.server.ts (lines 14 and 27), src/routes/users/edit/+page.server.ts (line 19), and src/routes/workbooks/edit/[slug]/+page.server.ts (lines 24 and 82), add explicit post-getLoggedInUser redirects for missing users with the required buildLoginPath import. In src/routes/workbooks/create/+page.server.ts (lines 18-20), replace ensureSessionOrRedirect with getLoggedInUser, redirecting when the returned author is null.Source: Coding guidelines
close #3870
Summary by CodeRabbit