@aiquants/auth-react-router 0.15.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,132 +0,0 @@
1
- /**
2
- * @module session
3
- * @description Cookie session storage factory with a DI test backdoor.
4
- * DI テストバックドア付きの cookie セッションストレージファクトリ。
5
- *
6
- * 原本 (quants-react-router session.server.ts) を挙動保存で移植。相違点は
7
- * (1) module-level の env throw をファクトリ引数検証へ移した、
8
- * (2) テストバックドア (mock_user cookie) をハードコードから `mockUser` ポート注入に変えた、
9
- * の 2 点のみ。cookie 形状 (`__session`/httpOnly/lax/30d、キー "user") は byte 互換。
10
- */
11
- import type { GoogleProfileBase, SessionProfile } from "@aiquants/auth-core"
12
- import { createCookieSessionStorage, redirect } from "react-router"
13
- import type { MockUserPort, SessionConfig } from "./ports"
14
-
15
- /** createAuthSessionStorage が返すセッション操作束。 */
16
- export type AuthSessionStorage = ReturnType<typeof createAuthSessionStorage>
17
-
18
- /**
19
- * Builds the cookie session storage and session helpers.
20
- * cookie セッションストレージとセッションヘルパー群を生成する。
21
- *
22
- * @throws secrets が空の場合 (fail-closed の設定ガード)。
23
- */
24
- export const createAuthSessionStorage = (config: SessionConfig, options?: { mockUser?: MockUserPort; loginPath?: string; postLoginRedirect?: string; isUserActive?: (profile: GoogleProfileBase) => boolean | Promise<boolean> }) => {
25
- // ファクトリ実行時検証 (import 時副作用の排除)
26
- if (!config.secrets || config.secrets.length === 0 || config.secrets.some((s) => !s)) {
27
- throw new Error("REMIX_SESSION_SECRET must be set")
28
- }
29
-
30
- const sessionKey = config.sessionKey ?? "user"
31
- const maxAge = config.maxAge ?? 60 * 60 * 24 * 30
32
- const loginPath = options?.loginPath ?? "/auth/login"
33
- const postLoginRedirect = options?.postLoginRedirect ?? "/"
34
- const mockUser = options?.mockUser
35
- const isUserActive = options?.isUserActive
36
-
37
- const sessionStorage = createCookieSessionStorage({
38
- cookie: {
39
- name: config.cookieName ?? "__session",
40
- httpOnly: true, // for security reasons, make this cookie http only
41
- path: "/", // remember to add this so the cookie will work in all routes
42
- sameSite: "lax", // this helps with CSRF
43
- secrets: config.secrets,
44
- secure: config.secure ?? process.env.NODE_ENV === "production",
45
- maxAge,
46
- },
47
- })
48
-
49
- /**
50
- * Reads the session from the request cookie.
51
- * リクエストの Cookie からセッションを取得する。
52
- */
53
- const getSession = async (request: Request) => {
54
- return await sessionStorage.getSession(request.headers.get("Cookie"))
55
- }
56
-
57
- /**
58
- * Reads the session user, honoring the injected test backdoor when enabled.
59
- * セッションからユーザー情報を取得する (注入されたテストバックドアが有効ならそれを返す)。
60
- */
61
- const getSessionUser = async (request: Request): Promise<SessionProfile | undefined> => {
62
- // テストバックドア (ポート注入時のみ有効。判定条件はアプリ側の enabled に委譲)
63
- if (mockUser?.enabled(request)) {
64
- return mockUser.resolve(request)
65
- }
66
-
67
- const session = await getSession(request)
68
- const user = session.get(sessionKey) as SessionProfile | undefined
69
- if (user === undefined) return undefined
70
- // 無効化済みアカウントは「未認証」として扱う。これにより、既に発行済みのセッション Cookie を
71
- // 持っていてもガードは通らず、無効化がその場で全ルートへ波及する。
72
- if (isUserActive && !(await isUserActive(user))) return undefined
73
- return user
74
- }
75
-
76
- /**
77
- * Saves the user into the session and returns the Set-Cookie value.
78
- * セッションへユーザー情報を保存し、Set-Cookie 値を返す。
79
- */
80
- const saveSession = async (request: Request, user: SessionProfile) => {
81
- const session = await getSession(request)
82
- session.set(sessionKey, user)
83
- return await sessionStorage.commitSession(session)
84
- }
85
-
86
- /**
87
- * Authenticated-user guard: redirects to the login page when absent.
88
- * 認証ユーザーガード。未認証ならログインページへリダイレクトする。
89
- */
90
- const requireUser = async (request: Request) => {
91
- const sessionUser = await getSessionUser(request)
92
- if (sessionUser === undefined) {
93
- throw redirect(loginPath)
94
- }
95
- return sessionUser as SessionProfile
96
- }
97
-
98
- /**
99
- * Admin-user guard: redirects to the app root when the role is not "admin".
100
- * 管理者ユーザーガード。role が "admin" でなければルートへリダイレクトする。
101
- */
102
- const requireAdminUser = async (request: Request) => {
103
- const sessionUser = await requireUser(request)
104
- if (sessionUser?.role !== "admin") {
105
- throw redirect(postLoginRedirect)
106
- }
107
- return sessionUser
108
- }
109
-
110
- /**
111
- * Destroys the session and returns the Set-Cookie value (logout primitive).
112
- * セッションを破棄して Set-Cookie 値を返す (ログアウトの基本操作)。
113
- */
114
- const logout = async (request: Request) => {
115
- const session = await getSession(request)
116
- const cookie = await sessionStorage.destroySession(session)
117
- return cookie
118
- }
119
-
120
- return {
121
- sessionStorage,
122
- sessionKey,
123
- getSession,
124
- getSessionUser,
125
- saveSession,
126
- requireUser,
127
- requireAdminUser,
128
- logout,
129
- commitSession: sessionStorage.commitSession,
130
- destroySession: sessionStorage.destroySession,
131
- }
132
- }
package/src/server.ts DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * @aiquants/auth-react-router/server — server-side auth adapter barrel.
3
- * サーバーサイド認証アダプタのバレル。
4
- */
5
-
6
- export type { AuthServer } from "./server/auth-server"
7
- export { createAuthServer } from "./server/auth-server"
8
- export { AuthRequiredError, createAuthenticate, GoogleInvalidGrantError, TokenRefreshError } from "./server/authenticate"
9
- export { createAuthenticateInLoader } from "./server/authenticate-in-loader"
10
- export { createGoogleAuthenticator } from "./server/google-strategy"
11
- export { createAuthHandlers } from "./server/handlers"
12
- export type { AuthServerConfig, BackendAuthPort, GoogleOAuthConfig, LoggerPort, MockUserPort, SessionConfig, UserPhotoPort, WarmupPort } from "./server/ports"
13
- export type { AuthSessionStorage } from "./server/session"
14
- export { createAuthSessionStorage } from "./server/session"
@@ -1,6 +0,0 @@
1
- /*! @aiquants/auth-react-router standalone CSS — 非 Tailwind ホスト専用。ホストの Tailwind ビルドと混在させないこと */
2
- @layer theme, base, components, utilities;
3
- @import "tailwindcss/theme.css" layer(theme);
4
- /* source(none): 自動ソース検出を無効化し、src 以外のクラス混入を防ぐ */
5
- @import "tailwindcss/utilities.css" layer(utilities) source(none);
6
- @source "../**/*.{ts,tsx}";