@civic/auth 0.0.1-beta.22 → 0.0.1-beta.24
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.
- package/dist/chunk-2OZJONNO.js +172 -0
- package/dist/chunk-2OZJONNO.js.map +1 -0
- package/dist/chunk-3YV5NEM4.js +173 -0
- package/dist/chunk-3YV5NEM4.js.map +1 -0
- package/dist/chunk-5UQQYXCX.js.map +1 -1
- package/dist/chunk-5XL2ST72.mjs +226 -0
- package/dist/chunk-5XL2ST72.mjs.map +1 -0
- package/dist/chunk-6RFRDWIP.js.map +1 -1
- package/dist/chunk-6RJHOVY6.mjs +712 -0
- package/dist/chunk-6RJHOVY6.mjs.map +1 -0
- package/dist/chunk-74J7HX36.mjs +712 -0
- package/dist/chunk-74J7HX36.mjs.map +1 -0
- package/dist/chunk-7K3QN2AT.js.map +1 -1
- package/dist/chunk-A43GY6C3.mjs +173 -0
- package/dist/chunk-A43GY6C3.mjs.map +1 -0
- package/dist/{chunk-NLRREFOX.mjs → chunk-AMCR45Y5.mjs} +2 -2
- package/dist/chunk-AMCR45Y5.mjs.map +1 -0
- package/dist/chunk-COWYPS3A.js +712 -0
- package/dist/chunk-COWYPS3A.js.map +1 -0
- package/dist/chunk-CRTRMMJ7.js.map +1 -1
- package/dist/chunk-CTVJJBBA.js.map +1 -1
- package/dist/chunk-ELO3M4DA.js +175 -0
- package/dist/chunk-ELO3M4DA.js.map +1 -0
- package/dist/chunk-FZ4INHWE.mjs +707 -0
- package/dist/chunk-FZ4INHWE.mjs.map +1 -0
- package/dist/chunk-G3P5TIO2.mjs +708 -0
- package/dist/chunk-G3P5TIO2.mjs.map +1 -0
- package/dist/chunk-JCLIMTK5.js +712 -0
- package/dist/chunk-JCLIMTK5.js.map +1 -0
- package/dist/chunk-JDZPCA3P.js.map +1 -1
- package/dist/chunk-NTJWPNOZ.mjs +175 -0
- package/dist/chunk-NTJWPNOZ.mjs.map +1 -0
- package/dist/chunk-ON4OH5OM.mjs +172 -0
- package/dist/chunk-ON4OH5OM.mjs.map +1 -0
- package/dist/chunk-QHE3SPKQ.js +712 -0
- package/dist/chunk-QHE3SPKQ.js.map +1 -0
- package/dist/chunk-RCFPLIWS.js +707 -0
- package/dist/chunk-RCFPLIWS.js.map +1 -0
- package/dist/chunk-RF23Q4V6.js +708 -0
- package/dist/chunk-RF23Q4V6.js.map +1 -0
- package/dist/chunk-SEKF2WZX.js +226 -0
- package/dist/chunk-SEKF2WZX.js.map +1 -0
- package/dist/chunk-SN7YDQQH.js.map +1 -1
- package/dist/chunk-WXSUVTI4.mjs +712 -0
- package/dist/chunk-WXSUVTI4.mjs.map +1 -0
- package/dist/{chunk-FHRZSX3C.js → chunk-XNSHSKGI.js} +2 -2
- package/dist/chunk-XNSHSKGI.js.map +1 -0
- package/dist/index-DTimUlkB.d.ts +17 -0
- package/dist/index-DvjkKpkk.d.mts +17 -0
- package/dist/index.css +10 -2
- package/dist/index.css.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/nextjs/client.css +10 -2
- package/dist/nextjs/client.css.map +1 -1
- package/dist/nextjs/client.js +16 -8
- package/dist/nextjs/client.js.map +1 -1
- package/dist/nextjs/client.mjs +13 -5
- package/dist/nextjs/client.mjs.map +1 -1
- package/dist/nextjs.d.mts +2 -4
- package/dist/nextjs.d.ts +2 -4
- package/dist/nextjs.js +9 -9
- package/dist/nextjs.js.map +1 -1
- package/dist/nextjs.mjs +1 -1
- package/dist/react.js +7 -7
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +2 -2
- package/dist/server.js.map +1 -1
- package/dist/types-b4c1koXj.d.mts +19 -0
- package/dist/types-b4c1koXj.d.ts +19 -0
- package/package.json +14 -14
- package/dist/chunk-FHRZSX3C.js.map +0 -1
- package/dist/chunk-NLRREFOX.mjs.map +0 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk7K3QN2ATjs = require('./chunk-7K3QN2AT.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunkCRTRMMJ7js = require('./chunk-CRTRMMJ7.js');
|
|
9
|
+
|
|
10
|
+
// src/lib/logger.ts
|
|
11
|
+
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
12
|
+
var PACKAGE_NAME = "@civic/auth";
|
|
13
|
+
var DebugLogger = class {
|
|
14
|
+
constructor(namespace) {
|
|
15
|
+
this.debugLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:debug`);
|
|
16
|
+
this.infoLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:info`);
|
|
17
|
+
this.warnLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:warn`);
|
|
18
|
+
this.errorLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:error`);
|
|
19
|
+
this.debugLogger.color = "4";
|
|
20
|
+
this.infoLogger.color = "2";
|
|
21
|
+
this.warnLogger.color = "3";
|
|
22
|
+
this.errorLogger.color = "1";
|
|
23
|
+
}
|
|
24
|
+
debug(message, ...args) {
|
|
25
|
+
this.debugLogger(message, ...args);
|
|
26
|
+
}
|
|
27
|
+
info(message, ...args) {
|
|
28
|
+
this.infoLogger(message, ...args);
|
|
29
|
+
}
|
|
30
|
+
warn(message, ...args) {
|
|
31
|
+
this.warnLogger(message, ...args);
|
|
32
|
+
}
|
|
33
|
+
error(message, ...args) {
|
|
34
|
+
this.errorLogger(message, ...args);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var createLogger = (namespace) => new DebugLogger(namespace);
|
|
38
|
+
var loggers = {
|
|
39
|
+
// Next.js specific loggers
|
|
40
|
+
nextjs: {
|
|
41
|
+
routes: createLogger("api:routes"),
|
|
42
|
+
middleware: createLogger("api:middleware"),
|
|
43
|
+
handlers: {
|
|
44
|
+
auth: createLogger("api:handlers:auth")
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
// React specific loggers
|
|
48
|
+
react: {
|
|
49
|
+
components: createLogger("react:components"),
|
|
50
|
+
hooks: createLogger("react:hooks"),
|
|
51
|
+
context: createLogger("react:context")
|
|
52
|
+
},
|
|
53
|
+
// Shared utilities loggers
|
|
54
|
+
services: {
|
|
55
|
+
validation: createLogger("utils:validation"),
|
|
56
|
+
network: createLogger("utils:network")
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/nextjs/config.ts
|
|
61
|
+
var logger = loggers.nextjs.handlers.auth;
|
|
62
|
+
var _a;
|
|
63
|
+
var appUrl = typeof process !== "undefined" ? ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NEXT_PUBLIC_APP_URL) || "" : "";
|
|
64
|
+
var defaultServerSecure = !appUrl.startsWith("http://localhost");
|
|
65
|
+
var defaultAuthConfig = {
|
|
66
|
+
oauthServer: _chunk7K3QN2ATjs.DEFAULT_AUTH_SERVER,
|
|
67
|
+
callbackUrl: "/api/auth/callback",
|
|
68
|
+
challengeUrl: "/api/auth/challenge",
|
|
69
|
+
logoutUrl: "/api/auth/logout",
|
|
70
|
+
loginUrl: "/",
|
|
71
|
+
include: ["/*"],
|
|
72
|
+
exclude: [],
|
|
73
|
+
cookies: {
|
|
74
|
+
tokens: {
|
|
75
|
+
["id_token" /* ID_TOKEN */]: {
|
|
76
|
+
secure: defaultServerSecure,
|
|
77
|
+
httpOnly: true,
|
|
78
|
+
sameSite: "strict",
|
|
79
|
+
path: "/"
|
|
80
|
+
},
|
|
81
|
+
["access_token" /* ACCESS_TOKEN */]: {
|
|
82
|
+
secure: defaultServerSecure,
|
|
83
|
+
httpOnly: true,
|
|
84
|
+
sameSite: "strict",
|
|
85
|
+
path: "/"
|
|
86
|
+
},
|
|
87
|
+
["refresh_token" /* REFRESH_TOKEN */]: {
|
|
88
|
+
secure: defaultServerSecure,
|
|
89
|
+
httpOnly: true,
|
|
90
|
+
sameSite: "strict",
|
|
91
|
+
path: "/"
|
|
92
|
+
},
|
|
93
|
+
["code_verifier" /* COOKIE_NAME */]: {
|
|
94
|
+
secure: defaultServerSecure,
|
|
95
|
+
httpOnly: true,
|
|
96
|
+
sameSite: "strict",
|
|
97
|
+
path: "/"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
user: {
|
|
101
|
+
secure: defaultServerSecure,
|
|
102
|
+
httpOnly: false,
|
|
103
|
+
sameSite: "strict",
|
|
104
|
+
path: "/",
|
|
105
|
+
maxAge: 60 * 60
|
|
106
|
+
// 1 hour
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var resolveAuthConfig = (config = {}) => {
|
|
111
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
112
|
+
const configFromEnv = _chunk7K3QN2ATjs.withoutUndefined.call(void 0, {
|
|
113
|
+
clientId: process.env._civic_auth_client_id,
|
|
114
|
+
oauthServer: process.env._civic_oauth_server,
|
|
115
|
+
callbackUrl: process.env._civic_auth_callback_url,
|
|
116
|
+
challengeUrl: process.env._civic_auth_challenge_url,
|
|
117
|
+
loginUrl: process.env._civic_auth_login_url,
|
|
118
|
+
appUrl: process.env._civic_auth_app_url,
|
|
119
|
+
logoutUrl: process.env._civic_auth_logout_url,
|
|
120
|
+
include: (_a2 = process.env._civic_auth_includes) == null ? void 0 : _a2.split(","),
|
|
121
|
+
exclude: (_b = process.env._civic_auth_excludes) == null ? void 0 : _b.split(","),
|
|
122
|
+
cookies: process.env._civic_auth_cookie_config ? JSON.parse(process.env._civic_auth_cookie_config) : void 0
|
|
123
|
+
});
|
|
124
|
+
const mergedConfig = _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig), configFromEnv), config), {
|
|
125
|
+
// Override with directly passed config
|
|
126
|
+
cookies: {
|
|
127
|
+
tokens: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.tokens), ((_c = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _c.tokens) || {}), ((_d = config.cookies) == null ? void 0 : _d.tokens) || {}),
|
|
128
|
+
user: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.user), ((_e = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _e.user) || {}), ((_f = config.cookies) == null ? void 0 : _f.user) || {})
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
logger.debug(
|
|
132
|
+
"Config from environment:",
|
|
133
|
+
JSON.stringify(configFromEnv, null, 2)
|
|
134
|
+
);
|
|
135
|
+
logger.debug("Resolved config:", JSON.stringify(mergedConfig, null, 2));
|
|
136
|
+
if (mergedConfig.clientId === void 0) {
|
|
137
|
+
throw new Error("Civic Auth client ID is required");
|
|
138
|
+
}
|
|
139
|
+
return mergedConfig;
|
|
140
|
+
};
|
|
141
|
+
var createCivicAuthPlugin = (authConfig) => {
|
|
142
|
+
return (nextConfig) => {
|
|
143
|
+
logger.debug(
|
|
144
|
+
"createCivicAuthPlugin nextConfig",
|
|
145
|
+
JSON.stringify(nextConfig, null, 2)
|
|
146
|
+
);
|
|
147
|
+
const resolvedConfig = resolveAuthConfig(_chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, authConfig));
|
|
148
|
+
return _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig), {
|
|
149
|
+
env: _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig == null ? void 0 : nextConfig.env), {
|
|
150
|
+
// Internal environment variables - do not set these manually
|
|
151
|
+
_civic_auth_client_id: resolvedConfig.clientId,
|
|
152
|
+
_civic_oauth_server: resolvedConfig.oauthServer,
|
|
153
|
+
_civic_auth_callback_url: resolvedConfig.callbackUrl,
|
|
154
|
+
_civic_auth_challenge_url: resolvedConfig.challengeUrl,
|
|
155
|
+
_civic_auth_login_url: resolvedConfig.loginUrl,
|
|
156
|
+
_civic_auth_logout_url: resolvedConfig.logoutUrl,
|
|
157
|
+
_civic_auth_app_url: resolvedConfig.appUrl,
|
|
158
|
+
_civic_auth_includes: resolvedConfig.include.join(","),
|
|
159
|
+
_civic_auth_excludes: resolvedConfig.exclude.join(","),
|
|
160
|
+
_civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies)
|
|
161
|
+
})
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
exports.loggers = loggers; exports.defaultAuthConfig = defaultAuthConfig; exports.resolveAuthConfig = resolveAuthConfig; exports.createCivicAuthPlugin = createCivicAuthPlugin;
|
|
172
|
+
//# sourceMappingURL=chunk-2OZJONNO.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/pedroapfilho/dev/civic-auth/packages/civic-auth-client/dist/chunk-2OZJONNO.js","../src/lib/logger.ts","../src/nextjs/config.ts"],"names":[],"mappings":"AAAA;AACE;AACA;AACF,sDAA4B;AAC5B;AACE;AACA;AACF,sDAA4B;AAC5B;AACA;ACTA,4EAAkB;AAElB,IAAM,aAAA,EAAe,aAAA;AASrB,IAAM,YAAA,EAAN,MAAoC;AAAA,EAMlC,WAAA,CAAY,SAAA,EAAmB;AAE7B,IAAA,IAAA,CAAK,YAAA,EAAc,6BAAA,CAAM,EAAA;AACD,IAAA;AACA,IAAA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IAAA;AACjB,EAAA;AACa,EAAA;AACI,EAAA;AACC,IAAA;AAClB,EAAA;AACO,EAAA;AACT;AA0BE;AAEoC,EAAA;AAC3B,IAAA;AACL,MAAA;AACe,MAAA;AACjB,IAAA;AACuB,IAAA;AAChB,IAAA;AAEA,MAAA;AAAA;AAGH,QAAA;AACqB,QAAA;AACrB,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACqB,QAAA;AACC,QAAA;AACA,QAAA;AACtB,QAAA;AACF,MAAA;AACF,IAAA;AACF,EAAA;AACF;AFtC8B;AACA;AACA;AACA;AACA;AACA;AACA","file":"/Users/pedroapfilho/dev/civic-auth/packages/civic-auth-client/dist/chunk-2OZJONNO.js","sourcesContent":[null,"import debug from \"debug\";\n\nconst PACKAGE_NAME = \"@civic/auth\";\n\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n}\n\nclass DebugLogger implements Logger {\n private debugLogger: debug.Debugger;\n private infoLogger: debug.Debugger;\n private warnLogger: debug.Debugger;\n private errorLogger: debug.Debugger;\n\n constructor(namespace: string) {\n // Format: @org/package:library:component:level\n this.debugLogger = debug(`${PACKAGE_NAME}:${namespace}:debug`);\n this.infoLogger = debug(`${PACKAGE_NAME}:${namespace}:info`);\n this.warnLogger = debug(`${PACKAGE_NAME}:${namespace}:warn`);\n this.errorLogger = debug(`${PACKAGE_NAME}:${namespace}:error`);\n\n this.debugLogger.color = \"4\";\n this.infoLogger.color = \"2\";\n this.warnLogger.color = \"3\";\n this.errorLogger.color = \"1\";\n }\n\n debug(message: string, ...args: unknown[]): void {\n this.debugLogger(message, ...args);\n }\n\n info(message: string, ...args: unknown[]): void {\n this.infoLogger(message, ...args);\n }\n\n warn(message: string, ...args: unknown[]): void {\n this.warnLogger(message, ...args);\n }\n\n error(message: string, ...args: unknown[]): void {\n this.errorLogger(message, ...args);\n }\n}\n\nexport const createLogger = (namespace: string): Logger =>\n new DebugLogger(namespace);\n\n// Pre-configured loggers for different parts of your package\nexport const loggers = {\n // Next.js specific loggers\n nextjs: {\n routes: createLogger(\"api:routes\"),\n middleware: createLogger(\"api:middleware\"),\n handlers: {\n auth: createLogger(\"api:handlers:auth\"),\n },\n },\n // React specific loggers\n react: {\n components: createLogger(\"react:components\"),\n hooks: createLogger(\"react:hooks\"),\n context: createLogger(\"react:context\"),\n },\n // Shared utilities loggers\n services: {\n validation: createLogger(\"utils:validation\"),\n network: createLogger(\"utils:network\"),\n },\n} as const;\n","/* eslint-disable turbo/no-undeclared-env-vars */\nimport { NextConfig } from \"next\";\nimport { loggers } from \"@/lib/logger\";\nimport { withoutUndefined } from \"@/utils\";\nimport {\n CodeVerifier,\n CookieConfig,\n OAuthTokens,\n TokensCookieConfig,\n} from \"@/shared/types\";\nimport { DEFAULT_AUTH_SERVER } from \"@/constants\";\n\nconst logger = loggers.nextjs.handlers.auth;\n\nexport type CookiesConfigObject = {\n tokens: TokensCookieConfig;\n user: CookieConfig;\n};\n\nexport type AuthConfigWithDefaults = {\n clientId: string;\n oauthServer: string;\n callbackUrl: string;\n loginUrl: string;\n logoutUrl: string;\n appUrl?: string;\n challengeUrl: string;\n include: string[];\n exclude: string[];\n cookies: CookiesConfigObject;\n};\n\nexport type AuthConfig = Partial<AuthConfigWithDefaults>;\n\nexport type DefinedAuthConfig = AuthConfigWithDefaults;\n\nconst appUrl =\n typeof process !== \"undefined\" ? process?.env?.NEXT_PUBLIC_APP_URL || \"\" : \"\";\nconst defaultServerSecure = !appUrl.startsWith(\"http://localhost\");\n\n/**\n * Default configuration values that will be used if not overridden\n */\nexport const defaultAuthConfig: Omit<AuthConfigWithDefaults, \"clientId\"> = {\n oauthServer: DEFAULT_AUTH_SERVER,\n callbackUrl: \"/api/auth/callback\",\n challengeUrl: \"/api/auth/challenge\",\n logoutUrl: \"/api/auth/logout\",\n loginUrl: \"/\",\n include: [\"/*\"],\n exclude: [],\n cookies: {\n tokens: {\n [OAuthTokens.ID_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.ACCESS_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.REFRESH_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [CodeVerifier.COOKIE_NAME]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n },\n user: {\n secure: defaultServerSecure,\n httpOnly: false,\n sameSite: \"strict\",\n path: \"/\",\n maxAge: 60 * 60, // 1 hour\n },\n },\n};\n\n/**\n * Resolves the authentication configuration by combining:\n * 1. Default values\n * 2. Environment variables (set internally by the plugin)\n * 3. Explicitly passed configuration\n *\n * Note: Developers should not set _civic_auth_* environment variables directly.\n * Instead, pass configuration to the createCivicAuthPlugin in next.config.js:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * callbackUrl: '/custom/callback',\n * })\n * ```\n */\nexport const resolveAuthConfig = (\n config: AuthConfig = {},\n): AuthConfigWithDefaults => {\n // Read configuration that was set by the plugin via environment variables\n const configFromEnv = withoutUndefined({\n clientId: process.env._civic_auth_client_id,\n oauthServer: process.env._civic_oauth_server,\n callbackUrl: process.env._civic_auth_callback_url,\n challengeUrl: process.env._civic_auth_challenge_url,\n loginUrl: process.env._civic_auth_login_url,\n appUrl: process.env._civic_auth_app_url,\n logoutUrl: process.env._civic_auth_logout_url,\n include: process.env._civic_auth_includes?.split(\",\"),\n exclude: process.env._civic_auth_excludes?.split(\",\"),\n cookies: process.env._civic_auth_cookie_config\n ? JSON.parse(process.env._civic_auth_cookie_config)\n : undefined,\n }) as AuthConfig;\n const mergedConfig = {\n ...defaultAuthConfig,\n ...configFromEnv, // Apply plugin-set config\n ...config, // Override with directly passed config\n cookies: {\n tokens: {\n ...defaultAuthConfig.cookies.tokens,\n ...(configFromEnv?.cookies?.tokens || {}),\n ...(config.cookies?.tokens || {}),\n },\n user: {\n ...defaultAuthConfig.cookies.user,\n ...(configFromEnv?.cookies?.user || {}),\n ...(config.cookies?.user || {}),\n },\n },\n };\n\n logger.debug(\n \"Config from environment:\",\n JSON.stringify(configFromEnv, null, 2),\n );\n logger.debug(\"Resolved config:\", JSON.stringify(mergedConfig, null, 2));\n if (mergedConfig.clientId === undefined) {\n throw new Error(\"Civic Auth client ID is required\");\n }\n return mergedConfig as AuthConfigWithDefaults & { clientId: string };\n};\n\n/**\n * Creates a Next.js plugin that handles auth configuration.\n *\n * This is the main configuration point for the auth system.\n * Do not set _civic_auth_* environment variables directly - instead,\n * pass your configuration here:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * clientId: 'my-client-id',\n * callbackUrl: '/custom/callback',\n * loginUrl: '/custom/login',\n * logoutUrl: '/custom/logout',\n * include: ['/protected/*'],\n * exclude: ['/public/*']\n * })\n * ```\n *\n * The plugin sets internal environment variables that are used by\n * the auth system. These variables should not be set manually.\n */\nexport const createCivicAuthPlugin = (\n authConfig: AuthConfig & Pick<Required<AuthConfig>, \"clientId\">,\n) => {\n return (nextConfig?: NextConfig) => {\n logger.debug(\n \"createCivicAuthPlugin nextConfig\",\n JSON.stringify(nextConfig, null, 2),\n );\n const resolvedConfig = resolveAuthConfig({ ...authConfig });\n return {\n ...nextConfig,\n env: {\n ...nextConfig?.env,\n // Internal environment variables - do not set these manually\n _civic_auth_client_id: resolvedConfig.clientId,\n _civic_oauth_server: resolvedConfig.oauthServer,\n _civic_auth_callback_url: resolvedConfig.callbackUrl,\n _civic_auth_challenge_url: resolvedConfig.challengeUrl,\n _civic_auth_login_url: resolvedConfig.loginUrl,\n _civic_auth_logout_url: resolvedConfig.logoutUrl,\n _civic_auth_app_url: resolvedConfig.appUrl,\n _civic_auth_includes: resolvedConfig.include.join(\",\"),\n _civic_auth_excludes: resolvedConfig.exclude.join(\",\"),\n _civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies),\n },\n };\n };\n};\n"]}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk7K3QN2ATjs = require('./chunk-7K3QN2AT.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
var _chunkCRTRMMJ7js = require('./chunk-CRTRMMJ7.js');
|
|
9
|
+
|
|
10
|
+
// src/lib/logger.ts
|
|
11
|
+
var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug);
|
|
12
|
+
var PACKAGE_NAME = "@civic/auth";
|
|
13
|
+
var DebugLogger = class {
|
|
14
|
+
constructor(namespace) {
|
|
15
|
+
this.debugLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:debug`);
|
|
16
|
+
this.infoLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:info`);
|
|
17
|
+
this.warnLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:warn`);
|
|
18
|
+
this.errorLogger = _debug2.default.call(void 0, `${PACKAGE_NAME}:${namespace}:error`);
|
|
19
|
+
this.debugLogger.color = "4";
|
|
20
|
+
this.infoLogger.color = "2";
|
|
21
|
+
this.warnLogger.color = "3";
|
|
22
|
+
this.errorLogger.color = "1";
|
|
23
|
+
}
|
|
24
|
+
debug(message, ...args) {
|
|
25
|
+
this.debugLogger(message, ...args);
|
|
26
|
+
}
|
|
27
|
+
info(message, ...args) {
|
|
28
|
+
this.infoLogger(message, ...args);
|
|
29
|
+
}
|
|
30
|
+
warn(message, ...args) {
|
|
31
|
+
this.warnLogger(message, ...args);
|
|
32
|
+
}
|
|
33
|
+
error(message, ...args) {
|
|
34
|
+
this.errorLogger(message, ...args);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
var createLogger = (namespace) => new DebugLogger(namespace);
|
|
38
|
+
var loggers = {
|
|
39
|
+
// Next.js specific loggers
|
|
40
|
+
nextjs: {
|
|
41
|
+
routes: createLogger("api:routes"),
|
|
42
|
+
middleware: createLogger("api:middleware"),
|
|
43
|
+
handlers: {
|
|
44
|
+
auth: createLogger("api:handlers:auth")
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
// React specific loggers
|
|
48
|
+
react: {
|
|
49
|
+
components: createLogger("react:components"),
|
|
50
|
+
hooks: createLogger("react:hooks"),
|
|
51
|
+
context: createLogger("react:context")
|
|
52
|
+
},
|
|
53
|
+
// Shared utilities loggers
|
|
54
|
+
services: {
|
|
55
|
+
validation: createLogger("utils:validation"),
|
|
56
|
+
network: createLogger("utils:network")
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/nextjs/config.ts
|
|
61
|
+
var logger = loggers.nextjs.handlers.auth;
|
|
62
|
+
var _a;
|
|
63
|
+
var appUrl = typeof process !== "undefined" ? ((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NEXT_PUBLIC_DASHBOARD_URL) || "" : "";
|
|
64
|
+
var defaultServerSecure = !appUrl.startsWith("http://localhost");
|
|
65
|
+
var defaultAuthConfig = {
|
|
66
|
+
oauthServer: _chunk7K3QN2ATjs.DEFAULT_AUTH_SERVER,
|
|
67
|
+
callbackUrl: "/api/auth/callback",
|
|
68
|
+
challengeUrl: "/api/auth/challenge",
|
|
69
|
+
logoutUrl: "/api/auth/logout",
|
|
70
|
+
loginUrl: "/",
|
|
71
|
+
include: ["/*"],
|
|
72
|
+
exclude: [],
|
|
73
|
+
cookies: {
|
|
74
|
+
tokens: {
|
|
75
|
+
["id_token" /* ID_TOKEN */]: {
|
|
76
|
+
secure: defaultServerSecure,
|
|
77
|
+
httpOnly: true,
|
|
78
|
+
sameSite: "strict",
|
|
79
|
+
path: "/"
|
|
80
|
+
},
|
|
81
|
+
["access_token" /* ACCESS_TOKEN */]: {
|
|
82
|
+
secure: defaultServerSecure,
|
|
83
|
+
httpOnly: true,
|
|
84
|
+
sameSite: "strict",
|
|
85
|
+
path: "/"
|
|
86
|
+
},
|
|
87
|
+
["refresh_token" /* REFRESH_TOKEN */]: {
|
|
88
|
+
secure: defaultServerSecure,
|
|
89
|
+
httpOnly: true,
|
|
90
|
+
sameSite: "strict",
|
|
91
|
+
path: "/"
|
|
92
|
+
},
|
|
93
|
+
["code_verifier" /* COOKIE_NAME */]: {
|
|
94
|
+
secure: defaultServerSecure,
|
|
95
|
+
httpOnly: true,
|
|
96
|
+
sameSite: "strict",
|
|
97
|
+
path: "/"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
user: {
|
|
101
|
+
secure: defaultServerSecure,
|
|
102
|
+
httpOnly: false,
|
|
103
|
+
sameSite: "strict",
|
|
104
|
+
path: "/",
|
|
105
|
+
maxAge: 60 * 60
|
|
106
|
+
// 1 hour
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var resolveAuthConfig = (config = {}) => {
|
|
111
|
+
var _a2, _b, _c, _d, _e, _f;
|
|
112
|
+
logger.debug("resolveAuthConfig inputs", JSON.stringify(config, null, 2));
|
|
113
|
+
const configFromEnv = _chunk7K3QN2ATjs.withoutUndefined.call(void 0, {
|
|
114
|
+
clientId: process.env._civic_auth_client_id,
|
|
115
|
+
oauthServer: process.env._civic_oauth_server,
|
|
116
|
+
callbackUrl: process.env._civic_auth_callback_url,
|
|
117
|
+
challengeUrl: process.env._civic_auth_challenge_url,
|
|
118
|
+
loginUrl: process.env._civic_auth_login_url,
|
|
119
|
+
appUrl: process.env._civic_auth_app_url,
|
|
120
|
+
logoutUrl: process.env._civic_auth_logout_url,
|
|
121
|
+
include: (_a2 = process.env._civic_auth_includes) == null ? void 0 : _a2.split(","),
|
|
122
|
+
exclude: (_b = process.env._civic_auth_excludes) == null ? void 0 : _b.split(","),
|
|
123
|
+
cookies: process.env._civic_auth_cookie_config ? JSON.parse(process.env._civic_auth_cookie_config) : void 0
|
|
124
|
+
});
|
|
125
|
+
const mergedConfig = _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig), configFromEnv), config), {
|
|
126
|
+
// Override with directly passed config
|
|
127
|
+
cookies: {
|
|
128
|
+
tokens: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.tokens), ((_c = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _c.tokens) || {}), ((_d = config.cookies) == null ? void 0 : _d.tokens) || {}),
|
|
129
|
+
user: _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, defaultAuthConfig.cookies.user), ((_e = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _e.user) || {}), ((_f = config.cookies) == null ? void 0 : _f.user) || {})
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
logger.debug(
|
|
133
|
+
"Config from environment:",
|
|
134
|
+
JSON.stringify(configFromEnv, null, 2)
|
|
135
|
+
);
|
|
136
|
+
logger.debug("Resolved config:", JSON.stringify(mergedConfig, null, 2));
|
|
137
|
+
if (mergedConfig.clientId === void 0) {
|
|
138
|
+
throw new Error("Civic Auth client ID is required");
|
|
139
|
+
}
|
|
140
|
+
return mergedConfig;
|
|
141
|
+
};
|
|
142
|
+
var createCivicAuthPlugin = (clientId, authConfig = {}) => {
|
|
143
|
+
return (nextConfig) => {
|
|
144
|
+
logger.debug(
|
|
145
|
+
"createCivicAuthPlugin nextConfig",
|
|
146
|
+
JSON.stringify(nextConfig, null, 2)
|
|
147
|
+
);
|
|
148
|
+
const resolvedConfig = resolveAuthConfig(_chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, authConfig), { clientId }));
|
|
149
|
+
return _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig), {
|
|
150
|
+
env: _chunkCRTRMMJ7js.__spreadProps.call(void 0, _chunkCRTRMMJ7js.__spreadValues.call(void 0, {}, nextConfig == null ? void 0 : nextConfig.env), {
|
|
151
|
+
// Internal environment variables - do not set these manually
|
|
152
|
+
_civic_auth_client_id: clientId,
|
|
153
|
+
_civic_oauth_server: resolvedConfig.oauthServer,
|
|
154
|
+
_civic_auth_callback_url: resolvedConfig.callbackUrl,
|
|
155
|
+
_civic_auth_challenge_url: resolvedConfig.challengeUrl,
|
|
156
|
+
_civic_auth_login_url: resolvedConfig.loginUrl,
|
|
157
|
+
_civic_auth_logout_url: resolvedConfig.logoutUrl,
|
|
158
|
+
_civic_auth_app_url: resolvedConfig.appUrl,
|
|
159
|
+
_civic_auth_includes: resolvedConfig.include.join(","),
|
|
160
|
+
_civic_auth_excludes: resolvedConfig.exclude.join(","),
|
|
161
|
+
_civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies)
|
|
162
|
+
})
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
exports.loggers = loggers; exports.defaultAuthConfig = defaultAuthConfig; exports.resolveAuthConfig = resolveAuthConfig; exports.createCivicAuthPlugin = createCivicAuthPlugin;
|
|
173
|
+
//# sourceMappingURL=chunk-3YV5NEM4.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/pedroapfilho/dev/civic-auth/packages/civic-auth-client/dist/chunk-3YV5NEM4.js","../src/lib/logger.ts","../src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AA;AACf,IAAA;AACF,EAAA;AACF;AAoBE;AA3GF,EAAA;AA6Ge,EAAA;AAES,EAAA;AACE,IAAA;AACG,IAAA;AACA,IAAA;AACC,IAAA;AACJ,IAAA;AACF,IAAA;AACG,IAAA;AACN,IAAA;AACA,IAAA;AACI,IAAA;AAGtB,EAAA;AACoB,EAAA;AAAA;AAIV,IAAA;AACC,MAAA;AAKF,MAAA;AAKR,IAAA;AACF,EAAA;AAEO,EAAA;AACL,IAAA;AACe,IAAA;AACjB,EAAA;AACa,EAAA;AACI,EAAA;AACC,IAAA;AAClB,EAAA;AACO,EAAA;AACT;AA0BE;AAGoC,EAAA;AAC3B,IAAA;AACL,MAAA;AACe,MAAA;AACjB,IAAA;AACuB,IAAA;AAChB,IAAA;AAEA,MAAA;AAAA;AAGH,QAAA;AACqB,QAAA;AACrB,QAAA;AACA,QAAA;AACA,QAAA;AACA,QAAA;AACqB,QAAA;AACC,QAAA;AACA,QAAA;AACtB,QAAA;AACF,MAAA;AACF,IAAA;AACF,EAAA;AACF;AFxC8B;AACA;AACA;AACA;AACA;AACA;AACA","file":"/Users/pedroapfilho/dev/civic-auth/packages/civic-auth-client/dist/chunk-3YV5NEM4.js","sourcesContent":[null,"import debug from \"debug\";\n\nconst PACKAGE_NAME = \"@civic/auth\";\n\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n}\n\nclass DebugLogger implements Logger {\n private debugLogger: debug.Debugger;\n private infoLogger: debug.Debugger;\n private warnLogger: debug.Debugger;\n private errorLogger: debug.Debugger;\n\n constructor(namespace: string) {\n // Format: @org/package:library:component:level\n this.debugLogger = debug(`${PACKAGE_NAME}:${namespace}:debug`);\n this.infoLogger = debug(`${PACKAGE_NAME}:${namespace}:info`);\n this.warnLogger = debug(`${PACKAGE_NAME}:${namespace}:warn`);\n this.errorLogger = debug(`${PACKAGE_NAME}:${namespace}:error`);\n\n this.debugLogger.color = \"4\";\n this.infoLogger.color = \"2\";\n this.warnLogger.color = \"3\";\n this.errorLogger.color = \"1\";\n }\n\n debug(message: string, ...args: unknown[]): void {\n this.debugLogger(message, ...args);\n }\n\n info(message: string, ...args: unknown[]): void {\n this.infoLogger(message, ...args);\n }\n\n warn(message: string, ...args: unknown[]): void {\n this.warnLogger(message, ...args);\n }\n\n error(message: string, ...args: unknown[]): void {\n this.errorLogger(message, ...args);\n }\n}\n\nexport const createLogger = (namespace: string): Logger =>\n new DebugLogger(namespace);\n\n// Pre-configured loggers for different parts of your package\nexport const loggers = {\n // Next.js specific loggers\n nextjs: {\n routes: createLogger(\"api:routes\"),\n middleware: createLogger(\"api:middleware\"),\n handlers: {\n auth: createLogger(\"api:handlers:auth\"),\n },\n },\n // React specific loggers\n react: {\n components: createLogger(\"react:components\"),\n hooks: createLogger(\"react:hooks\"),\n context: createLogger(\"react:context\"),\n },\n // Shared utilities loggers\n services: {\n validation: createLogger(\"utils:validation\"),\n network: createLogger(\"utils:network\"),\n },\n} as const;\n","/* eslint-disable turbo/no-undeclared-env-vars */\nimport { NextConfig } from \"next\";\nimport { loggers } from \"@/lib/logger\";\nimport { withoutUndefined } from \"@/utils\";\nimport {\n CodeVerifier,\n CookieConfig,\n OAuthTokens,\n TokensCookieConfig,\n} from \"@/shared/types\";\nimport { DEFAULT_AUTH_SERVER } from \"@/constants\";\n\nconst logger = loggers.nextjs.handlers.auth;\n\nexport type CookiesConfigObject = {\n tokens: TokensCookieConfig;\n user: CookieConfig;\n};\nexport type AuthConfigWithDefaults = {\n clientId: string;\n oauthServer: string;\n callbackUrl: string;\n loginUrl: string;\n logoutUrl: string;\n appUrl?: string;\n challengeUrl: string;\n include: string[];\n exclude: string[];\n cookies: CookiesConfigObject;\n};\n\nexport type AuthConfig = Partial<AuthConfigWithDefaults>;\n\nexport type DefinedAuthConfig = AuthConfigWithDefaults;\n\nconst appUrl =\n typeof process !== \"undefined\"\n ? process?.env?.NEXT_PUBLIC_DASHBOARD_URL || \"\"\n : \"\";\nconst defaultServerSecure = !appUrl.startsWith(\"http://localhost\");\n\n/**\n * Default configuration values that will be used if not overridden\n */\nexport const defaultAuthConfig: Omit<AuthConfigWithDefaults, \"clientId\"> = {\n oauthServer: DEFAULT_AUTH_SERVER,\n callbackUrl: \"/api/auth/callback\",\n challengeUrl: \"/api/auth/challenge\",\n logoutUrl: \"/api/auth/logout\",\n loginUrl: \"/\",\n include: [\"/*\"],\n exclude: [],\n cookies: {\n tokens: {\n [OAuthTokens.ID_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.ACCESS_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.REFRESH_TOKEN]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [CodeVerifier.COOKIE_NAME]: {\n secure: defaultServerSecure,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n },\n user: {\n secure: defaultServerSecure,\n httpOnly: false,\n sameSite: \"strict\",\n path: \"/\",\n maxAge: 60 * 60, // 1 hour\n },\n },\n};\n\n/**\n * Resolves the authentication configuration by combining:\n * 1. Default values\n * 2. Environment variables (set internally by the plugin)\n * 3. Explicitly passed configuration\n *\n * Note: Developers should not set _civic_auth_* environment variables directly.\n * Instead, pass configuration to the createCivicAuthPlugin in next.config.js:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * callbackUrl: '/custom/callback',\n * })\n * ```\n */\nexport const resolveAuthConfig = (\n config: AuthConfig = {},\n): AuthConfigWithDefaults & { clientId: string } => {\n logger.debug(\"resolveAuthConfig inputs\", JSON.stringify(config, null, 2));\n // Read configuration that was set by the plugin via environment variables\n const configFromEnv = withoutUndefined({\n clientId: process.env._civic_auth_client_id,\n oauthServer: process.env._civic_oauth_server,\n callbackUrl: process.env._civic_auth_callback_url,\n challengeUrl: process.env._civic_auth_challenge_url,\n loginUrl: process.env._civic_auth_login_url,\n appUrl: process.env._civic_auth_app_url,\n logoutUrl: process.env._civic_auth_logout_url,\n include: process.env._civic_auth_includes?.split(\",\"),\n exclude: process.env._civic_auth_excludes?.split(\",\"),\n cookies: process.env._civic_auth_cookie_config\n ? JSON.parse(process.env._civic_auth_cookie_config)\n : undefined,\n }) as AuthConfig;\n const mergedConfig = {\n ...defaultAuthConfig,\n ...configFromEnv, // Apply plugin-set config\n ...config, // Override with directly passed config\n cookies: {\n tokens: {\n ...defaultAuthConfig.cookies.tokens,\n ...(configFromEnv?.cookies?.tokens || {}),\n ...(config.cookies?.tokens || {}),\n },\n user: {\n ...defaultAuthConfig.cookies.user,\n ...(configFromEnv?.cookies?.user || {}),\n ...(config.cookies?.user || {}),\n },\n },\n };\n\n logger.debug(\n \"Config from environment:\",\n JSON.stringify(configFromEnv, null, 2),\n );\n logger.debug(\"Resolved config:\", JSON.stringify(mergedConfig, null, 2));\n if (mergedConfig.clientId === undefined) {\n throw new Error(\"Civic Auth client ID is required\");\n }\n return mergedConfig as AuthConfigWithDefaults & { clientId: string };\n};\n\n/**\n * Creates a Next.js plugin that handles auth configuration.\n *\n * This is the main configuration point for the auth system.\n * Do not set _civic_auth_* environment variables directly - instead,\n * pass your configuration here:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * clientId: 'my-client-id',\n * callbackUrl: '/custom/callback',\n * loginUrl: '/custom/login',\n * logoutUrl: '/custom/logout',\n * include: ['/protected/*'],\n * exclude: ['/public/*']\n * })\n * ```\n *\n * The plugin sets internal environment variables that are used by\n * the auth system. These variables should not be set manually.\n */\nexport const createCivicAuthPlugin = (\n clientId: string,\n authConfig: AuthConfig = {},\n) => {\n return (nextConfig?: NextConfig) => {\n logger.debug(\n \"createCivicAuthPlugin nextConfig\",\n JSON.stringify(nextConfig, null, 2),\n );\n const resolvedConfig = resolveAuthConfig({ ...authConfig, clientId });\n return {\n ...nextConfig,\n env: {\n ...nextConfig?.env,\n // Internal environment variables - do not set these manually\n _civic_auth_client_id: clientId,\n _civic_oauth_server: resolvedConfig.oauthServer,\n _civic_auth_callback_url: resolvedConfig.callbackUrl,\n _civic_auth_challenge_url: resolvedConfig.challengeUrl,\n _civic_auth_login_url: resolvedConfig.loginUrl,\n _civic_auth_logout_url: resolvedConfig.logoutUrl,\n _civic_auth_app_url: resolvedConfig.appUrl,\n _civic_auth_includes: resolvedConfig.include.join(\",\"),\n _civic_auth_excludes: resolvedConfig.exclude.join(\",\"),\n _civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies),\n },\n };\n };\n};\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/
|
|
1
|
+
{"version":3,"sources":["/Users/pedroapfilho/dev/civic-auth/packages/civic-auth-client/dist/chunk-5UQQYXCX.js"],"names":[],"mappings":"AAAA","file":"/Users/pedroapfilho/dev/civic-auth/packages/civic-auth-client/dist/chunk-5UQQYXCX.js"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CookieStorage,
|
|
3
|
+
GenericUserSession,
|
|
4
|
+
clearTokens,
|
|
5
|
+
withoutUndefined
|
|
6
|
+
} from "./chunk-G3P5TIO2.mjs";
|
|
7
|
+
import {
|
|
8
|
+
__async,
|
|
9
|
+
__spreadProps,
|
|
10
|
+
__spreadValues
|
|
11
|
+
} from "./chunk-RGHW4PYM.mjs";
|
|
12
|
+
|
|
13
|
+
// src/lib/logger.ts
|
|
14
|
+
import debug from "debug";
|
|
15
|
+
var PACKAGE_NAME = "@civic/auth";
|
|
16
|
+
var DebugLogger = class {
|
|
17
|
+
constructor(namespace) {
|
|
18
|
+
this.debugLogger = debug(`${PACKAGE_NAME}:${namespace}:debug`);
|
|
19
|
+
this.infoLogger = debug(`${PACKAGE_NAME}:${namespace}:info`);
|
|
20
|
+
this.warnLogger = debug(`${PACKAGE_NAME}:${namespace}:warn`);
|
|
21
|
+
this.errorLogger = debug(`${PACKAGE_NAME}:${namespace}:error`);
|
|
22
|
+
this.debugLogger.color = "4";
|
|
23
|
+
this.infoLogger.color = "2";
|
|
24
|
+
this.warnLogger.color = "3";
|
|
25
|
+
this.errorLogger.color = "1";
|
|
26
|
+
}
|
|
27
|
+
debug(message, ...args) {
|
|
28
|
+
this.debugLogger(message, ...args);
|
|
29
|
+
}
|
|
30
|
+
info(message, ...args) {
|
|
31
|
+
this.infoLogger(message, ...args);
|
|
32
|
+
}
|
|
33
|
+
warn(message, ...args) {
|
|
34
|
+
this.warnLogger(message, ...args);
|
|
35
|
+
}
|
|
36
|
+
error(message, ...args) {
|
|
37
|
+
this.errorLogger(message, ...args);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var createLogger = (namespace) => new DebugLogger(namespace);
|
|
41
|
+
var loggers = {
|
|
42
|
+
// Next.js specific loggers
|
|
43
|
+
nextjs: {
|
|
44
|
+
routes: createLogger("api:routes"),
|
|
45
|
+
middleware: createLogger("api:middleware"),
|
|
46
|
+
handlers: {
|
|
47
|
+
auth: createLogger("api:handlers:auth")
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
// React specific loggers
|
|
51
|
+
react: {
|
|
52
|
+
components: createLogger("react:components"),
|
|
53
|
+
hooks: createLogger("react:hooks"),
|
|
54
|
+
context: createLogger("react:context")
|
|
55
|
+
},
|
|
56
|
+
// Shared utilities loggers
|
|
57
|
+
services: {
|
|
58
|
+
validation: createLogger("utils:validation"),
|
|
59
|
+
network: createLogger("utils:network")
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/nextjs/config.ts
|
|
64
|
+
var logger = loggers.nextjs.handlers.auth;
|
|
65
|
+
var defaultAuthConfig = {
|
|
66
|
+
oauthServer: "https://auth-dev.civic.com/oauth",
|
|
67
|
+
callbackUrl: "/api/auth/callback",
|
|
68
|
+
challengeUrl: "/api/auth/challenge",
|
|
69
|
+
logoutUrl: "/api/auth/logout",
|
|
70
|
+
loginUrl: "/",
|
|
71
|
+
include: ["/*"],
|
|
72
|
+
exclude: [],
|
|
73
|
+
cookies: {
|
|
74
|
+
tokens: {
|
|
75
|
+
["id_token" /* ID_TOKEN */]: {
|
|
76
|
+
secure: true,
|
|
77
|
+
httpOnly: true,
|
|
78
|
+
sameSite: "strict",
|
|
79
|
+
path: "/"
|
|
80
|
+
},
|
|
81
|
+
["access_token" /* ACCESS_TOKEN */]: {
|
|
82
|
+
secure: true,
|
|
83
|
+
httpOnly: true,
|
|
84
|
+
sameSite: "strict",
|
|
85
|
+
path: "/"
|
|
86
|
+
},
|
|
87
|
+
["refresh_token" /* REFRESH_TOKEN */]: {
|
|
88
|
+
secure: true,
|
|
89
|
+
httpOnly: true,
|
|
90
|
+
sameSite: "strict",
|
|
91
|
+
path: "/"
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
user: {
|
|
95
|
+
secure: true,
|
|
96
|
+
httpOnly: false,
|
|
97
|
+
sameSite: "strict",
|
|
98
|
+
path: "/",
|
|
99
|
+
maxAge: 60 * 60
|
|
100
|
+
// 1 hour
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var resolveAuthConfig = (config = {}) => {
|
|
105
|
+
var _a, _b, _c, _d, _e, _f;
|
|
106
|
+
logger.debug("resolveAuthConfig inputs", JSON.stringify(config, null, 2));
|
|
107
|
+
const configFromEnv = withoutUndefined({
|
|
108
|
+
clientId: process.env._civic_auth_client_id,
|
|
109
|
+
oauthServer: process.env._civic_oauth_server,
|
|
110
|
+
callbackUrl: process.env._civic_auth_callback_url,
|
|
111
|
+
challengeUrl: process.env._civic_auth_challenge_url,
|
|
112
|
+
loginUrl: process.env._civic_auth_login_url,
|
|
113
|
+
appUrl: process.env._civic_auth_app_url,
|
|
114
|
+
logoutUrl: process.env._civic_auth_logout_url,
|
|
115
|
+
include: (_a = process.env._civic_auth_includes) == null ? void 0 : _a.split(","),
|
|
116
|
+
exclude: (_b = process.env._civic_auth_excludes) == null ? void 0 : _b.split(","),
|
|
117
|
+
cookies: process.env._civic_auth_cookie_config ? JSON.parse(process.env._civic_auth_cookie_config) : void 0
|
|
118
|
+
});
|
|
119
|
+
const mergedConfig = __spreadProps(__spreadValues(__spreadValues(__spreadValues({}, defaultAuthConfig), configFromEnv), config), {
|
|
120
|
+
// Override with directly passed config
|
|
121
|
+
cookies: {
|
|
122
|
+
tokens: __spreadValues(__spreadValues(__spreadValues({}, defaultAuthConfig.cookies.tokens), ((_c = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _c.tokens) || {}), ((_d = config.cookies) == null ? void 0 : _d.tokens) || {}),
|
|
123
|
+
user: __spreadValues(__spreadValues(__spreadValues({}, defaultAuthConfig.cookies.user), ((_e = configFromEnv == null ? void 0 : configFromEnv.cookies) == null ? void 0 : _e.user) || {}), ((_f = config.cookies) == null ? void 0 : _f.user) || {})
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
logger.debug(
|
|
127
|
+
"Config from environment:",
|
|
128
|
+
JSON.stringify(configFromEnv, null, 2)
|
|
129
|
+
);
|
|
130
|
+
logger.debug("Resolved config:", JSON.stringify(mergedConfig, null, 2));
|
|
131
|
+
if (mergedConfig.clientId === void 0) {
|
|
132
|
+
throw new Error("Civic Auth client ID is required");
|
|
133
|
+
}
|
|
134
|
+
return mergedConfig;
|
|
135
|
+
};
|
|
136
|
+
var createCivicAuthPlugin = (clientId, authConfig = {}) => {
|
|
137
|
+
return (nextConfig) => {
|
|
138
|
+
logger.debug(
|
|
139
|
+
"createCivicAuthPlugin nextConfig",
|
|
140
|
+
JSON.stringify(nextConfig, null, 2)
|
|
141
|
+
);
|
|
142
|
+
const resolvedConfig = resolveAuthConfig(__spreadProps(__spreadValues({}, authConfig), { clientId }));
|
|
143
|
+
return __spreadProps(__spreadValues({}, nextConfig), {
|
|
144
|
+
env: __spreadProps(__spreadValues({}, nextConfig == null ? void 0 : nextConfig.env), {
|
|
145
|
+
// Internal environment variables - do not set these manually
|
|
146
|
+
_civic_auth_client_id: clientId,
|
|
147
|
+
_civic_oauth_server: resolvedConfig.oauthServer,
|
|
148
|
+
_civic_auth_callback_url: resolvedConfig.callbackUrl,
|
|
149
|
+
_civic_auth_challenge_url: resolvedConfig.challengeUrl,
|
|
150
|
+
_civic_auth_login_url: resolvedConfig.loginUrl,
|
|
151
|
+
_civic_auth_logout_url: resolvedConfig.logoutUrl,
|
|
152
|
+
_civic_auth_app_url: resolvedConfig.appUrl,
|
|
153
|
+
_civic_auth_includes: resolvedConfig.include.join(","),
|
|
154
|
+
_civic_auth_excludes: resolvedConfig.exclude.join(","),
|
|
155
|
+
_civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies)
|
|
156
|
+
})
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// src/nextjs/utils.ts
|
|
162
|
+
var resolveCallbackUrl = (config, alternativeUrl) => {
|
|
163
|
+
var _a;
|
|
164
|
+
const baseUrl = (_a = config.appUrl) != null ? _a : alternativeUrl;
|
|
165
|
+
const callbackUrl = new URL(config == null ? void 0 : config.callbackUrl, baseUrl).toString();
|
|
166
|
+
return callbackUrl.toString();
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
// src/nextjs/cookies.ts
|
|
170
|
+
import { cookies } from "next/headers.js";
|
|
171
|
+
var clearAuthCookies = () => __async(void 0, null, function* () {
|
|
172
|
+
const cookieStorage = new NextjsCookieStorage();
|
|
173
|
+
clearTokens(cookieStorage);
|
|
174
|
+
const clientStorage = new NextjsClientStorage();
|
|
175
|
+
const userSession = new GenericUserSession(clientStorage);
|
|
176
|
+
userSession.set(null);
|
|
177
|
+
});
|
|
178
|
+
var NextjsCookieStorage = class extends CookieStorage {
|
|
179
|
+
constructor(config = {}) {
|
|
180
|
+
super({
|
|
181
|
+
secure: true,
|
|
182
|
+
httpOnly: true
|
|
183
|
+
});
|
|
184
|
+
this.config = config;
|
|
185
|
+
}
|
|
186
|
+
get(key) {
|
|
187
|
+
var _a;
|
|
188
|
+
return ((_a = cookies().get(key)) == null ? void 0 : _a.value) || null;
|
|
189
|
+
}
|
|
190
|
+
set(key, value) {
|
|
191
|
+
var _a, _b;
|
|
192
|
+
const cookieSettings = (_b = (_a = this.config) == null ? void 0 : _a[key]) != null ? _b : this.settings;
|
|
193
|
+
console.log(
|
|
194
|
+
"NextjsCookieStorage.set",
|
|
195
|
+
JSON.stringify({ key, value, cookieSettings }, null, 2)
|
|
196
|
+
);
|
|
197
|
+
cookies().set(key, value, cookieSettings);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
var NextjsClientStorage = class extends CookieStorage {
|
|
201
|
+
constructor(config = {}) {
|
|
202
|
+
super(__spreadProps(__spreadValues({}, config), {
|
|
203
|
+
secure: false,
|
|
204
|
+
httpOnly: false
|
|
205
|
+
}));
|
|
206
|
+
}
|
|
207
|
+
get(key) {
|
|
208
|
+
var _a;
|
|
209
|
+
return ((_a = cookies().get(key)) == null ? void 0 : _a.value) || null;
|
|
210
|
+
}
|
|
211
|
+
set(key, value) {
|
|
212
|
+
cookies().set(key, value, this.settings);
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export {
|
|
217
|
+
loggers,
|
|
218
|
+
defaultAuthConfig,
|
|
219
|
+
resolveAuthConfig,
|
|
220
|
+
createCivicAuthPlugin,
|
|
221
|
+
resolveCallbackUrl,
|
|
222
|
+
clearAuthCookies,
|
|
223
|
+
NextjsCookieStorage,
|
|
224
|
+
NextjsClientStorage
|
|
225
|
+
};
|
|
226
|
+
//# sourceMappingURL=chunk-5XL2ST72.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/lib/logger.ts","../src/nextjs/config.ts","../src/nextjs/utils.ts","../src/nextjs/cookies.ts"],"sourcesContent":["import debug from \"debug\";\n\nconst PACKAGE_NAME = \"@civic/auth\";\n\nexport interface Logger {\n debug(message: string, ...args: unknown[]): void;\n info(message: string, ...args: unknown[]): void;\n warn(message: string, ...args: unknown[]): void;\n error(message: string, ...args: unknown[]): void;\n}\n\nclass DebugLogger implements Logger {\n private debugLogger: debug.Debugger;\n private infoLogger: debug.Debugger;\n private warnLogger: debug.Debugger;\n private errorLogger: debug.Debugger;\n\n constructor(namespace: string) {\n // Format: @org/package:library:component:level\n this.debugLogger = debug(`${PACKAGE_NAME}:${namespace}:debug`);\n this.infoLogger = debug(`${PACKAGE_NAME}:${namespace}:info`);\n this.warnLogger = debug(`${PACKAGE_NAME}:${namespace}:warn`);\n this.errorLogger = debug(`${PACKAGE_NAME}:${namespace}:error`);\n\n this.debugLogger.color = \"4\";\n this.infoLogger.color = \"2\";\n this.warnLogger.color = \"3\";\n this.errorLogger.color = \"1\";\n }\n\n debug(message: string, ...args: unknown[]): void {\n this.debugLogger(message, ...args);\n }\n\n info(message: string, ...args: unknown[]): void {\n this.infoLogger(message, ...args);\n }\n\n warn(message: string, ...args: unknown[]): void {\n this.warnLogger(message, ...args);\n }\n\n error(message: string, ...args: unknown[]): void {\n this.errorLogger(message, ...args);\n }\n}\n\nexport const createLogger = (namespace: string): Logger =>\n new DebugLogger(namespace);\n\n// Pre-configured loggers for different parts of your package\nexport const loggers = {\n // Next.js specific loggers\n nextjs: {\n routes: createLogger(\"api:routes\"),\n middleware: createLogger(\"api:middleware\"),\n handlers: {\n auth: createLogger(\"api:handlers:auth\"),\n },\n },\n // React specific loggers\n react: {\n components: createLogger(\"react:components\"),\n hooks: createLogger(\"react:hooks\"),\n context: createLogger(\"react:context\"),\n },\n // Shared utilities loggers\n services: {\n validation: createLogger(\"utils:validation\"),\n network: createLogger(\"utils:network\"),\n },\n} as const;\n","/* eslint-disable turbo/no-undeclared-env-vars */\nimport { NextConfig } from \"next\";\nimport { loggers } from \"@/lib/logger\";\nimport { withoutUndefined } from \"@/utils\";\nimport { CookieConfig, OAuthTokens, TokensCookieConfig } from \"@/shared/types\";\n\nconst logger = loggers.nextjs.handlers.auth;\n\nexport type AuthConfigWithDefaults = {\n clientId: string;\n oauthServer: string;\n callbackUrl: string;\n loginUrl: string;\n logoutUrl: string;\n appUrl?: string;\n challengeUrl: string;\n include: string[];\n exclude: string[];\n cookies: {\n tokens: TokensCookieConfig;\n user: CookieConfig;\n };\n};\n\nexport type AuthConfig = Partial<AuthConfigWithDefaults>;\n\nexport type DefinedAuthConfig = AuthConfigWithDefaults;\n\n/**\n * Default configuration values that will be used if not overridden\n */\nexport const defaultAuthConfig: Omit<AuthConfigWithDefaults, \"clientId\"> = {\n oauthServer: \"https://auth-dev.civic.com/oauth\",\n callbackUrl: \"/api/auth/callback\",\n challengeUrl: \"/api/auth/challenge\",\n logoutUrl: \"/api/auth/logout\",\n loginUrl: \"/\",\n include: [\"/*\"],\n exclude: [],\n cookies: {\n tokens: {\n [OAuthTokens.ID_TOKEN]: {\n secure: true,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.ACCESS_TOKEN]: {\n secure: true,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n [OAuthTokens.REFRESH_TOKEN]: {\n secure: true,\n httpOnly: true,\n sameSite: \"strict\",\n path: \"/\",\n },\n },\n user: {\n secure: true,\n httpOnly: false,\n sameSite: \"strict\",\n path: \"/\",\n maxAge: 60 * 60, // 1 hour\n },\n },\n};\n\n/**\n * Resolves the authentication configuration by combining:\n * 1. Default values\n * 2. Environment variables (set internally by the plugin)\n * 3. Explicitly passed configuration\n *\n * Note: Developers should not set _civic_auth_* environment variables directly.\n * Instead, pass configuration to the createCivicAuthPlugin in next.config.js:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * callbackUrl: '/custom/callback',\n * })\n * ```\n */\nexport const resolveAuthConfig = (\n config: AuthConfig = {},\n): AuthConfigWithDefaults & { clientId: string } => {\n logger.debug(\"resolveAuthConfig inputs\", JSON.stringify(config, null, 2));\n // Read configuration that was set by the plugin via environment variables\n const configFromEnv = withoutUndefined({\n clientId: process.env._civic_auth_client_id,\n oauthServer: process.env._civic_oauth_server,\n callbackUrl: process.env._civic_auth_callback_url,\n challengeUrl: process.env._civic_auth_challenge_url,\n loginUrl: process.env._civic_auth_login_url,\n appUrl: process.env._civic_auth_app_url,\n logoutUrl: process.env._civic_auth_logout_url,\n include: process.env._civic_auth_includes?.split(\",\"),\n exclude: process.env._civic_auth_excludes?.split(\",\"),\n cookies: process.env._civic_auth_cookie_config\n ? JSON.parse(process.env._civic_auth_cookie_config)\n : undefined,\n }) as AuthConfig;\n const mergedConfig = {\n ...defaultAuthConfig,\n ...configFromEnv, // Apply plugin-set config\n ...config, // Override with directly passed config\n cookies: {\n tokens: {\n ...defaultAuthConfig.cookies.tokens,\n ...(configFromEnv?.cookies?.tokens || {}),\n ...(config.cookies?.tokens || {}),\n },\n user: {\n ...defaultAuthConfig.cookies.user,\n ...(configFromEnv?.cookies?.user || {}),\n ...(config.cookies?.user || {}),\n },\n },\n };\n\n logger.debug(\n \"Config from environment:\",\n JSON.stringify(configFromEnv, null, 2),\n );\n logger.debug(\"Resolved config:\", JSON.stringify(mergedConfig, null, 2));\n if (mergedConfig.clientId === undefined) {\n throw new Error(\"Civic Auth client ID is required\");\n }\n return mergedConfig as AuthConfigWithDefaults & { clientId: string };\n};\n\n/**\n * Creates a Next.js plugin that handles auth configuration.\n *\n * This is the main configuration point for the auth system.\n * Do not set _civic_auth_* environment variables directly - instead,\n * pass your configuration here:\n *\n * @example\n * ```js\n * // next.config.js\n * export default createCivicAuthPlugin({\n * clientId: 'my-client-id',\n * callbackUrl: '/custom/callback',\n * loginUrl: '/custom/login',\n * logoutUrl: '/custom/logout',\n * include: ['/protected/*'],\n * exclude: ['/public/*']\n * })\n * ```\n *\n * The plugin sets internal environment variables that are used by\n * the auth system. These variables should not be set manually.\n */\nexport const createCivicAuthPlugin = (\n clientId: string,\n authConfig: AuthConfig = {},\n) => {\n return (nextConfig?: NextConfig) => {\n logger.debug(\n \"createCivicAuthPlugin nextConfig\",\n JSON.stringify(nextConfig, null, 2),\n );\n const resolvedConfig = resolveAuthConfig({ ...authConfig, clientId });\n return {\n ...nextConfig,\n env: {\n ...nextConfig?.env,\n // Internal environment variables - do not set these manually\n _civic_auth_client_id: clientId,\n _civic_oauth_server: resolvedConfig.oauthServer,\n _civic_auth_callback_url: resolvedConfig.callbackUrl,\n _civic_auth_challenge_url: resolvedConfig.challengeUrl,\n _civic_auth_login_url: resolvedConfig.loginUrl,\n _civic_auth_logout_url: resolvedConfig.logoutUrl,\n _civic_auth_app_url: resolvedConfig.appUrl,\n _civic_auth_includes: resolvedConfig.include.join(\",\"),\n _civic_auth_excludes: resolvedConfig.exclude.join(\",\"),\n _civic_auth_cookie_config: JSON.stringify(resolvedConfig.cookies),\n },\n };\n };\n};\n","import { AuthConfigWithDefaults } from \"@/nextjs/config\";\n\nexport const resolveCallbackUrl = (\n config: AuthConfigWithDefaults,\n alternativeUrl?: string,\n): string => {\n const baseUrl = config.appUrl ?? alternativeUrl;\n const callbackUrl = new URL(config?.callbackUrl, baseUrl).toString();\n return callbackUrl.toString();\n};\n","import { SessionData, UnknownObject, User } from \"@/types\";\nimport { NextResponse } from \"next/server\";\nimport { AuthConfig } from \"@/nextjs/config\";\nimport { CookieStorage, CookieStorageSettings } from \"@/server\";\nimport { cookies } from \"next/headers.js\";\nimport { GenericUserSession } from \"@/shared/UserSession\";\nimport { clearTokens } from \"@/shared/util\";\nimport { OAuthTokens, TokensCookieConfig } from \"@/shared/types\";\n\n/**\n * Creates HTTP-only cookies for authentication tokens\n */\nconst createTokenCookies = (\n response: NextResponse,\n sessionData: SessionData,\n config: AuthConfig,\n) => {\n const maxAge = sessionData.expiresIn ?? 3600;\n const cookieOptions = {\n ...config.cookies?.tokens,\n maxAge,\n };\n\n if (sessionData.accessToken) {\n response.cookies.set(\"access_token\", sessionData.accessToken, {\n ...cookieOptions,\n httpOnly: true,\n });\n }\n\n if (sessionData.idToken) {\n response.cookies.set(\"id_token\", sessionData.idToken, {\n ...cookieOptions,\n httpOnly: true,\n });\n }\n\n if (sessionData.refreshToken) {\n response.cookies.set(\"refresh_token\", sessionData.refreshToken, {\n ...cookieOptions,\n httpOnly: true,\n });\n }\n};\n\n/**\n * Creates a client-readable cookie with user info\n */\nconst createUserInfoCookie = (\n response: NextResponse,\n user: User<UnknownObject> | null,\n sessionData: SessionData,\n config: AuthConfig,\n) => {\n if (!user) {\n response.cookies.set(\"user\", \"\", {\n ...config.cookies?.user,\n maxAge: 0,\n });\n return;\n }\n const maxAge = sessionData.expiresIn ?? 3600;\n\n // TODO select fields to include in the user cookie\n const frontendUser = {\n ...user,\n };\n\n // TODO make call to get user info from the\n // auth server /userinfo endpoint when it's available\n // then add to the default claims above\n\n response.cookies.set(\"user\", JSON.stringify(frontendUser), {\n ...config.cookies?.user,\n maxAge,\n });\n};\n\n/**\n * Clears all authentication cookies\n */\nconst clearAuthCookies = async () => {\n // clear session, and tokens\n const cookieStorage = new NextjsCookieStorage();\n clearTokens(cookieStorage);\n\n // clear user\n const clientStorage = new NextjsClientStorage();\n const userSession = new GenericUserSession(clientStorage);\n userSession.set(null);\n};\n\nclass NextjsCookieStorage extends CookieStorage {\n constructor(readonly config: Partial<TokensCookieConfig> = {}) {\n super({\n secure: true,\n httpOnly: true,\n });\n }\n\n get(key: string): string | null {\n return cookies().get(key)?.value || null;\n }\n\n set(key: OAuthTokens, value: string): void {\n const cookieSettings = this.config?.[key] ?? this.settings;\n console.log(\n \"NextjsCookieStorage.set\",\n JSON.stringify({ key, value, cookieSettings }, null, 2),\n );\n cookies().set(key, value, cookieSettings);\n }\n}\n\nclass NextjsClientStorage extends CookieStorage {\n constructor(config: Partial<CookieStorageSettings> = {}) {\n super({\n ...config,\n secure: false,\n httpOnly: false,\n });\n }\n\n get(key: string): string | null {\n return cookies().get(key)?.value || null;\n }\n\n set(key: string, value: string): void {\n cookies().set(key, value, this.settings);\n }\n}\n\nexport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