@oauth42/next 0.1.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.
- package/LICENSE +57 -0
- package/README.md +226 -0
- package/dist/client/index.d.mts +111 -0
- package/dist/client/index.d.ts +111 -0
- package/dist/client/index.js +234 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/index.mjs +197 -0
- package/dist/client/index.mjs.map +1 -0
- package/dist/index-xJCMwWtK.d.mts +122 -0
- package/dist/index-xJCMwWtK.d.ts +122 -0
- package/dist/index.d.mts +44 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +305 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +260 -0
- package/dist/index.mjs.map +1 -0
- package/dist/server/index.d.mts +5 -0
- package/dist/server/index.d.ts +5 -0
- package/dist/server/index.js +316 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/index.mjs +269 -0
- package/dist/server/index.mjs.map +1 -0
- package/package.json +79 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/client/index.ts
|
|
21
|
+
var client_exports = {};
|
|
22
|
+
__export(client_exports, {
|
|
23
|
+
AuthStatus: () => AuthStatus,
|
|
24
|
+
ProtectedComponent: () => ProtectedComponent,
|
|
25
|
+
SessionProvider: () => import_react4.SessionProvider,
|
|
26
|
+
SignInButton: () => SignInButton,
|
|
27
|
+
SignOutButton: () => SignOutButton,
|
|
28
|
+
UserProfile: () => UserProfile,
|
|
29
|
+
signIn: () => import_react4.signIn,
|
|
30
|
+
signOut: () => import_react4.signOut,
|
|
31
|
+
useOAuth42Session: () => useOAuth42Session,
|
|
32
|
+
useOAuth42Tokens: () => useOAuth42Tokens,
|
|
33
|
+
useOAuth42User: () => useOAuth42User,
|
|
34
|
+
useRequireAuth: () => useRequireAuth,
|
|
35
|
+
useSession: () => import_react4.useSession
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(client_exports);
|
|
38
|
+
var import_react4 = require("next-auth/react");
|
|
39
|
+
|
|
40
|
+
// src/client/hooks.ts
|
|
41
|
+
var import_react = require("next-auth/react");
|
|
42
|
+
var import_react2 = require("react");
|
|
43
|
+
function useOAuth42Session() {
|
|
44
|
+
const { data: session, status } = (0, import_react.useSession)();
|
|
45
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
46
|
+
const handleSignIn = (0, import_react2.useCallback)(async () => {
|
|
47
|
+
try {
|
|
48
|
+
setError(null);
|
|
49
|
+
await (0, import_react.signIn)("oauth42");
|
|
50
|
+
} catch (err) {
|
|
51
|
+
setError(err);
|
|
52
|
+
}
|
|
53
|
+
}, []);
|
|
54
|
+
const handleSignOut = (0, import_react2.useCallback)(async () => {
|
|
55
|
+
try {
|
|
56
|
+
setError(null);
|
|
57
|
+
await (0, import_react.signOut)();
|
|
58
|
+
} catch (err) {
|
|
59
|
+
setError(err);
|
|
60
|
+
}
|
|
61
|
+
}, []);
|
|
62
|
+
return {
|
|
63
|
+
session,
|
|
64
|
+
loading: status === "loading",
|
|
65
|
+
error,
|
|
66
|
+
isAuthenticated: status === "authenticated",
|
|
67
|
+
signIn: handleSignIn,
|
|
68
|
+
signOut: handleSignOut
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function useOAuth42User() {
|
|
72
|
+
const { session, isAuthenticated } = useOAuth42Session();
|
|
73
|
+
return {
|
|
74
|
+
user: isAuthenticated ? session?.user : null,
|
|
75
|
+
isAuthenticated
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function useOAuth42Tokens() {
|
|
79
|
+
const { session } = useOAuth42Session();
|
|
80
|
+
const [isExpired, setIsExpired] = (0, import_react2.useState)(false);
|
|
81
|
+
(0, import_react2.useEffect)(() => {
|
|
82
|
+
if (session?.expires) {
|
|
83
|
+
const expiryTime = new Date(session.expires).getTime();
|
|
84
|
+
const now = Date.now();
|
|
85
|
+
setIsExpired(now >= expiryTime);
|
|
86
|
+
const timeUntilExpiry = expiryTime - now;
|
|
87
|
+
if (timeUntilExpiry > 0) {
|
|
88
|
+
const timer = setTimeout(() => {
|
|
89
|
+
setIsExpired(true);
|
|
90
|
+
}, timeUntilExpiry);
|
|
91
|
+
return () => clearTimeout(timer);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}, [session?.expires]);
|
|
95
|
+
return {
|
|
96
|
+
accessToken: session?.accessToken,
|
|
97
|
+
idToken: session?.idToken,
|
|
98
|
+
isExpired,
|
|
99
|
+
refreshToken: async () => {
|
|
100
|
+
await (0, import_react.signIn)("oauth42");
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function useRequireAuth(redirectTo = "/auth/signin") {
|
|
105
|
+
const { isAuthenticated, loading } = useOAuth42Session();
|
|
106
|
+
const [isRedirecting, setIsRedirecting] = (0, import_react2.useState)(false);
|
|
107
|
+
(0, import_react2.useEffect)(() => {
|
|
108
|
+
if (!loading && !isAuthenticated && !isRedirecting) {
|
|
109
|
+
setIsRedirecting(true);
|
|
110
|
+
if (typeof window !== "undefined") {
|
|
111
|
+
window.location.href = redirectTo;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}, [isAuthenticated, loading, redirectTo, isRedirecting]);
|
|
115
|
+
return {
|
|
116
|
+
isAuthenticated,
|
|
117
|
+
loading: loading || isRedirecting
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/client/components.tsx
|
|
122
|
+
var import_react3 = require("next-auth/react");
|
|
123
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
124
|
+
function SignInButton({
|
|
125
|
+
children = "Sign in with OAuth42",
|
|
126
|
+
className = "",
|
|
127
|
+
callbackUrl = "/",
|
|
128
|
+
onClick
|
|
129
|
+
}) {
|
|
130
|
+
const handleClick = async () => {
|
|
131
|
+
if (onClick) onClick();
|
|
132
|
+
await (0, import_react3.signIn)("oauth42", { callbackUrl });
|
|
133
|
+
};
|
|
134
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
135
|
+
"button",
|
|
136
|
+
{
|
|
137
|
+
onClick: handleClick,
|
|
138
|
+
className,
|
|
139
|
+
type: "button",
|
|
140
|
+
children
|
|
141
|
+
}
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
function SignOutButton({
|
|
145
|
+
children = "Sign out",
|
|
146
|
+
className = "",
|
|
147
|
+
callbackUrl = "/",
|
|
148
|
+
onClick
|
|
149
|
+
}) {
|
|
150
|
+
const handleClick = async () => {
|
|
151
|
+
if (onClick) onClick();
|
|
152
|
+
await (0, import_react3.signOut)({ callbackUrl });
|
|
153
|
+
};
|
|
154
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
155
|
+
"button",
|
|
156
|
+
{
|
|
157
|
+
onClick: handleClick,
|
|
158
|
+
className,
|
|
159
|
+
type: "button",
|
|
160
|
+
children
|
|
161
|
+
}
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
function UserProfile({
|
|
165
|
+
className = "",
|
|
166
|
+
showEmail = true,
|
|
167
|
+
showName = true,
|
|
168
|
+
showImage = true,
|
|
169
|
+
loadingComponent = /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: "Loading..." }),
|
|
170
|
+
notAuthenticatedComponent = /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: "Not authenticated" })
|
|
171
|
+
}) {
|
|
172
|
+
const { session, loading, isAuthenticated } = useOAuth42Session();
|
|
173
|
+
if (loading) {
|
|
174
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: loadingComponent });
|
|
175
|
+
}
|
|
176
|
+
if (!isAuthenticated || !session?.user) {
|
|
177
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: notAuthenticatedComponent });
|
|
178
|
+
}
|
|
179
|
+
const { user } = session;
|
|
180
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className, children: [
|
|
181
|
+
showImage && user.image && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
182
|
+
"img",
|
|
183
|
+
{
|
|
184
|
+
src: user.image,
|
|
185
|
+
alt: user.name || "User",
|
|
186
|
+
style: { width: 50, height: 50, borderRadius: "50%" }
|
|
187
|
+
}
|
|
188
|
+
),
|
|
189
|
+
showName && user.name && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: user.name }),
|
|
190
|
+
showEmail && user.email && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: user.email })
|
|
191
|
+
] });
|
|
192
|
+
}
|
|
193
|
+
function AuthStatus({
|
|
194
|
+
authenticatedComponent,
|
|
195
|
+
unauthenticatedComponent,
|
|
196
|
+
loadingComponent = /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: "Loading..." })
|
|
197
|
+
}) {
|
|
198
|
+
const { isAuthenticated, loading } = useOAuth42Session();
|
|
199
|
+
if (loading) {
|
|
200
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: loadingComponent });
|
|
201
|
+
}
|
|
202
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: isAuthenticated ? authenticatedComponent : unauthenticatedComponent });
|
|
203
|
+
}
|
|
204
|
+
function ProtectedComponent({
|
|
205
|
+
children,
|
|
206
|
+
fallback = /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SignInButton, {}),
|
|
207
|
+
loadingComponent = /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: "Loading..." })
|
|
208
|
+
}) {
|
|
209
|
+
const { isAuthenticated, loading } = useOAuth42Session();
|
|
210
|
+
if (loading) {
|
|
211
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: loadingComponent });
|
|
212
|
+
}
|
|
213
|
+
if (!isAuthenticated) {
|
|
214
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: fallback });
|
|
215
|
+
}
|
|
216
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
|
|
217
|
+
}
|
|
218
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
219
|
+
0 && (module.exports = {
|
|
220
|
+
AuthStatus,
|
|
221
|
+
ProtectedComponent,
|
|
222
|
+
SessionProvider,
|
|
223
|
+
SignInButton,
|
|
224
|
+
SignOutButton,
|
|
225
|
+
UserProfile,
|
|
226
|
+
signIn,
|
|
227
|
+
signOut,
|
|
228
|
+
useOAuth42Session,
|
|
229
|
+
useOAuth42Tokens,
|
|
230
|
+
useOAuth42User,
|
|
231
|
+
useRequireAuth,
|
|
232
|
+
useSession
|
|
233
|
+
});
|
|
234
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/index.ts","../../src/client/hooks.ts","../../src/client/components.tsx"],"sourcesContent":["// Client-side exports\n\n// Re-export commonly used next-auth/react functions\nexport { signIn, signOut, useSession, SessionProvider } from 'next-auth/react';\nexport type { Session } from 'next-auth';\nexport {\n useOAuth42Session,\n useOAuth42User,\n useOAuth42Tokens,\n useRequireAuth,\n} from './hooks';\n\nexport type {\n OAuth42Session,\n UseOAuth42SessionReturn,\n} from './hooks';\n\nexport {\n SignInButton,\n SignOutButton,\n UserProfile,\n AuthStatus,\n ProtectedComponent,\n} from './components';\n\nexport type {\n SignInButtonProps,\n SignOutButtonProps,\n UserProfileProps,\n AuthStatusProps,\n ProtectedComponentProps,\n} from './components';","import { useSession, signIn, signOut } from 'next-auth/react';\nimport { useCallback, useEffect, useState } from 'react';\n\nexport type OAuth42Session<E = {}> = ({\n user?: {\n email?: string | null;\n name?: string | null;\n image?: string | null;\n username?: string;\n emailVerified?: boolean;\n };\n accessToken?: string;\n idToken?: string;\n expires?: string;\n}) & E;\n\nexport interface UseOAuth42SessionReturn<E = {}> {\n session: OAuth42Session<E> | null;\n loading: boolean;\n error: Error | null;\n isAuthenticated: boolean;\n signIn: () => Promise<void>;\n signOut: () => Promise<void>;\n}\n\n/**\n * Hook to manage OAuth42 session with optional extra fields\n */\nexport function useOAuth42Session<E = {}>(): UseOAuth42SessionReturn<E> {\n const { data: session, status } = useSession();\n const [error, setError] = useState<Error | null>(null);\n \n const handleSignIn = useCallback(async () => {\n try {\n setError(null);\n await signIn('oauth42');\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n const handleSignOut = useCallback(async () => {\n try {\n setError(null);\n await signOut();\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n return {\n session: session as unknown as OAuth42Session<E> | null,\n loading: status === 'loading',\n error,\n isAuthenticated: status === 'authenticated',\n signIn: handleSignIn,\n signOut: handleSignOut,\n };\n}\n\n/**\n * Hook to get the current OAuth42 user\n */\nexport function useOAuth42User<E = {}>() {\n const { session, isAuthenticated } = useOAuth42Session<E>();\n \n return {\n user: isAuthenticated ? session?.user : null,\n isAuthenticated,\n };\n}\n\n/**\n * Hook to manage OAuth42 tokens\n */\nexport function useOAuth42Tokens<E = {}>() {\n const { session } = useOAuth42Session<E>();\n const [isExpired, setIsExpired] = useState(false);\n \n useEffect(() => {\n if (session?.expires) {\n const expiryTime = new Date(session.expires).getTime();\n const now = Date.now();\n setIsExpired(now >= expiryTime);\n \n // Set a timer to update expiry status\n const timeUntilExpiry = expiryTime - now;\n if (timeUntilExpiry > 0) {\n const timer = setTimeout(() => {\n setIsExpired(true);\n }, timeUntilExpiry);\n \n return () => clearTimeout(timer);\n }\n }\n }, [session?.expires]);\n \n return {\n accessToken: session?.accessToken,\n idToken: session?.idToken,\n isExpired,\n refreshToken: async () => {\n // Trigger a session refresh\n await signIn('oauth42');\n },\n };\n}\n\n/**\n * Hook for protected routes\n */\nexport function useRequireAuth(redirectTo: string = '/auth/signin') {\n const { isAuthenticated, loading } = useOAuth42Session();\n const [isRedirecting, setIsRedirecting] = useState(false);\n \n useEffect(() => {\n if (!loading && !isAuthenticated && !isRedirecting) {\n setIsRedirecting(true);\n if (typeof window !== 'undefined') {\n window.location.href = redirectTo;\n }\n }\n }, [isAuthenticated, loading, redirectTo, isRedirecting]);\n \n return {\n isAuthenticated,\n loading: loading || isRedirecting,\n };\n}\n","import React from 'react';\nimport { signIn, signOut } from 'next-auth/react';\nimport { useOAuth42Session, useOAuth42User } from './hooks';\n\nexport interface SignInButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign in button component\n */\nexport function SignInButton({ \n children = 'Sign in with OAuth42', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignInButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signIn('oauth42', { callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface SignOutButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign out button component\n */\nexport function SignOutButton({ \n children = 'Sign out', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignOutButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signOut({ callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface UserProfileProps {\n className?: string;\n showEmail?: boolean;\n showName?: boolean;\n showImage?: boolean;\n loadingComponent?: React.ReactNode;\n notAuthenticatedComponent?: React.ReactNode;\n}\n\n/**\n * User profile display component\n */\nexport function UserProfile({\n className = '',\n showEmail = true,\n showName = true,\n showImage = true,\n loadingComponent = <div>Loading...</div>,\n notAuthenticatedComponent = <div>Not authenticated</div>,\n}: UserProfileProps) {\n const { session, loading, isAuthenticated } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated || !session?.user) {\n return <>{notAuthenticatedComponent}</>;\n }\n \n const { user } = session;\n \n return (\n <div className={className}>\n {showImage && user.image && (\n <img \n src={user.image} \n alt={user.name || 'User'} \n style={{ width: 50, height: 50, borderRadius: '50%' }}\n />\n )}\n {showName && user.name && <div>{user.name}</div>}\n {showEmail && user.email && <div>{user.email}</div>}\n </div>\n );\n}\n\nexport interface AuthStatusProps {\n authenticatedComponent?: React.ReactNode;\n unauthenticatedComponent?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Conditional rendering based on auth status\n */\nexport function AuthStatus({\n authenticatedComponent,\n unauthenticatedComponent,\n loadingComponent = <div>Loading...</div>,\n}: AuthStatusProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n return <>{isAuthenticated ? authenticatedComponent : unauthenticatedComponent}</>;\n}\n\nexport interface ProtectedComponentProps {\n children: React.ReactNode;\n fallback?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Wrapper component for protected content\n */\nexport function ProtectedComponent({\n children,\n fallback = <SignInButton />,\n loadingComponent = <div>Loading...</div>,\n}: ProtectedComponentProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated) {\n return <>{fallback}</>;\n }\n \n return <>{children}</>;\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,IAAAA,gBAA6D;;;ACH7D,mBAA4C;AAC5C,IAAAC,gBAAiD;AA2B1C,SAAS,oBAAwD;AACtE,QAAM,EAAE,MAAM,SAAS,OAAO,QAAI,yBAAW;AAC7C,QAAM,CAAC,OAAO,QAAQ,QAAI,wBAAuB,IAAI;AAErD,QAAM,mBAAe,2BAAY,YAAY;AAC3C,QAAI;AACF,eAAS,IAAI;AACb,gBAAM,qBAAO,SAAS;AAAA,IACxB,SAAS,KAAK;AACZ,eAAS,GAAY;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,QAAM,oBAAgB,2BAAY,YAAY;AAC5C,QAAI;AACF,eAAS,IAAI;AACb,gBAAM,sBAAQ;AAAA,IAChB,SAAS,KAAK;AACZ,eAAS,GAAY;AAAA,IACvB;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AAAA,IACL;AAAA,IACA,SAAS,WAAW;AAAA,IACpB;AAAA,IACA,iBAAiB,WAAW;AAAA,IAC5B,QAAQ;AAAA,IACR,SAAS;AAAA,EACX;AACF;AAKO,SAAS,iBAAyB;AACvC,QAAM,EAAE,SAAS,gBAAgB,IAAI,kBAAqB;AAE1D,SAAO;AAAA,IACL,MAAM,kBAAkB,SAAS,OAAO;AAAA,IACxC;AAAA,EACF;AACF;AAKO,SAAS,mBAA2B;AACzC,QAAM,EAAE,QAAQ,IAAI,kBAAqB;AACzC,QAAM,CA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ACpC,GAAoB;AAClB,QAAM,EAAE,iBAAiB,QAAQ,IAAI,kBAAkB;AAEvD,MAAI,SAAS;AACX,WAAO,2EAAG,4BAAiB;AAAA,EAC7B;AAEA,SAAO,2EAAG,4BAAkB,yBAAyB,0BAAyB;AAChF;AAWO,SAAS,mBAAmB;AAAA,EACjC;AAAA,EACA,WAAW,4CAAC,gBAAa;AAAA,EACzB,mBAAmB,4CAAC,SAAI,wBAAU;AACpC,GAA4B;AAC1B,QAAM,EAAE,iBAAiB,QAAQ,IAAI,kBAAkB;AAEvD,MAAI,SAAS;AACX,WAAO,2EAAG,4BAAiB;AAAA,EAC7B;AAEA,MAAI,CAAC,iBAAiB;AACpB,WAAO,2EAAG,oBAAS;AAAA,EACrB;AAEA,SAAO,2EAAG,UAAS;AACrB;","names":["import_react","import_react","import_react"]}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// src/client/index.ts
|
|
2
|
+
import { signIn as signIn3, signOut as signOut3, useSession as useSession2, SessionProvider } from "next-auth/react";
|
|
3
|
+
|
|
4
|
+
// src/client/hooks.ts
|
|
5
|
+
import { useSession, signIn, signOut } from "next-auth/react";
|
|
6
|
+
import { useCallback, useEffect, useState } from "react";
|
|
7
|
+
function useOAuth42Session() {
|
|
8
|
+
const { data: session, status } = useSession();
|
|
9
|
+
const [error, setError] = useState(null);
|
|
10
|
+
const handleSignIn = useCallback(async () => {
|
|
11
|
+
try {
|
|
12
|
+
setError(null);
|
|
13
|
+
await signIn("oauth42");
|
|
14
|
+
} catch (err) {
|
|
15
|
+
setError(err);
|
|
16
|
+
}
|
|
17
|
+
}, []);
|
|
18
|
+
const handleSignOut = useCallback(async () => {
|
|
19
|
+
try {
|
|
20
|
+
setError(null);
|
|
21
|
+
await signOut();
|
|
22
|
+
} catch (err) {
|
|
23
|
+
setError(err);
|
|
24
|
+
}
|
|
25
|
+
}, []);
|
|
26
|
+
return {
|
|
27
|
+
session,
|
|
28
|
+
loading: status === "loading",
|
|
29
|
+
error,
|
|
30
|
+
isAuthenticated: status === "authenticated",
|
|
31
|
+
signIn: handleSignIn,
|
|
32
|
+
signOut: handleSignOut
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function useOAuth42User() {
|
|
36
|
+
const { session, isAuthenticated } = useOAuth42Session();
|
|
37
|
+
return {
|
|
38
|
+
user: isAuthenticated ? session?.user : null,
|
|
39
|
+
isAuthenticated
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function useOAuth42Tokens() {
|
|
43
|
+
const { session } = useOAuth42Session();
|
|
44
|
+
const [isExpired, setIsExpired] = useState(false);
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (session?.expires) {
|
|
47
|
+
const expiryTime = new Date(session.expires).getTime();
|
|
48
|
+
const now = Date.now();
|
|
49
|
+
setIsExpired(now >= expiryTime);
|
|
50
|
+
const timeUntilExpiry = expiryTime - now;
|
|
51
|
+
if (timeUntilExpiry > 0) {
|
|
52
|
+
const timer = setTimeout(() => {
|
|
53
|
+
setIsExpired(true);
|
|
54
|
+
}, timeUntilExpiry);
|
|
55
|
+
return () => clearTimeout(timer);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}, [session?.expires]);
|
|
59
|
+
return {
|
|
60
|
+
accessToken: session?.accessToken,
|
|
61
|
+
idToken: session?.idToken,
|
|
62
|
+
isExpired,
|
|
63
|
+
refreshToken: async () => {
|
|
64
|
+
await signIn("oauth42");
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function useRequireAuth(redirectTo = "/auth/signin") {
|
|
69
|
+
const { isAuthenticated, loading } = useOAuth42Session();
|
|
70
|
+
const [isRedirecting, setIsRedirecting] = useState(false);
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
if (!loading && !isAuthenticated && !isRedirecting) {
|
|
73
|
+
setIsRedirecting(true);
|
|
74
|
+
if (typeof window !== "undefined") {
|
|
75
|
+
window.location.href = redirectTo;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}, [isAuthenticated, loading, redirectTo, isRedirecting]);
|
|
79
|
+
return {
|
|
80
|
+
isAuthenticated,
|
|
81
|
+
loading: loading || isRedirecting
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/client/components.tsx
|
|
86
|
+
import { signIn as signIn2, signOut as signOut2 } from "next-auth/react";
|
|
87
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
88
|
+
function SignInButton({
|
|
89
|
+
children = "Sign in with OAuth42",
|
|
90
|
+
className = "",
|
|
91
|
+
callbackUrl = "/",
|
|
92
|
+
onClick
|
|
93
|
+
}) {
|
|
94
|
+
const handleClick = async () => {
|
|
95
|
+
if (onClick) onClick();
|
|
96
|
+
await signIn2("oauth42", { callbackUrl });
|
|
97
|
+
};
|
|
98
|
+
return /* @__PURE__ */ jsx(
|
|
99
|
+
"button",
|
|
100
|
+
{
|
|
101
|
+
onClick: handleClick,
|
|
102
|
+
className,
|
|
103
|
+
type: "button",
|
|
104
|
+
children
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
function SignOutButton({
|
|
109
|
+
children = "Sign out",
|
|
110
|
+
className = "",
|
|
111
|
+
callbackUrl = "/",
|
|
112
|
+
onClick
|
|
113
|
+
}) {
|
|
114
|
+
const handleClick = async () => {
|
|
115
|
+
if (onClick) onClick();
|
|
116
|
+
await signOut2({ callbackUrl });
|
|
117
|
+
};
|
|
118
|
+
return /* @__PURE__ */ jsx(
|
|
119
|
+
"button",
|
|
120
|
+
{
|
|
121
|
+
onClick: handleClick,
|
|
122
|
+
className,
|
|
123
|
+
type: "button",
|
|
124
|
+
children
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
function UserProfile({
|
|
129
|
+
className = "",
|
|
130
|
+
showEmail = true,
|
|
131
|
+
showName = true,
|
|
132
|
+
showImage = true,
|
|
133
|
+
loadingComponent = /* @__PURE__ */ jsx("div", { children: "Loading..." }),
|
|
134
|
+
notAuthenticatedComponent = /* @__PURE__ */ jsx("div", { children: "Not authenticated" })
|
|
135
|
+
}) {
|
|
136
|
+
const { session, loading, isAuthenticated } = useOAuth42Session();
|
|
137
|
+
if (loading) {
|
|
138
|
+
return /* @__PURE__ */ jsx(Fragment, { children: loadingComponent });
|
|
139
|
+
}
|
|
140
|
+
if (!isAuthenticated || !session?.user) {
|
|
141
|
+
return /* @__PURE__ */ jsx(Fragment, { children: notAuthenticatedComponent });
|
|
142
|
+
}
|
|
143
|
+
const { user } = session;
|
|
144
|
+
return /* @__PURE__ */ jsxs("div", { className, children: [
|
|
145
|
+
showImage && user.image && /* @__PURE__ */ jsx(
|
|
146
|
+
"img",
|
|
147
|
+
{
|
|
148
|
+
src: user.image,
|
|
149
|
+
alt: user.name || "User",
|
|
150
|
+
style: { width: 50, height: 50, borderRadius: "50%" }
|
|
151
|
+
}
|
|
152
|
+
),
|
|
153
|
+
showName && user.name && /* @__PURE__ */ jsx("div", { children: user.name }),
|
|
154
|
+
showEmail && user.email && /* @__PURE__ */ jsx("div", { children: user.email })
|
|
155
|
+
] });
|
|
156
|
+
}
|
|
157
|
+
function AuthStatus({
|
|
158
|
+
authenticatedComponent,
|
|
159
|
+
unauthenticatedComponent,
|
|
160
|
+
loadingComponent = /* @__PURE__ */ jsx("div", { children: "Loading..." })
|
|
161
|
+
}) {
|
|
162
|
+
const { isAuthenticated, loading } = useOAuth42Session();
|
|
163
|
+
if (loading) {
|
|
164
|
+
return /* @__PURE__ */ jsx(Fragment, { children: loadingComponent });
|
|
165
|
+
}
|
|
166
|
+
return /* @__PURE__ */ jsx(Fragment, { children: isAuthenticated ? authenticatedComponent : unauthenticatedComponent });
|
|
167
|
+
}
|
|
168
|
+
function ProtectedComponent({
|
|
169
|
+
children,
|
|
170
|
+
fallback = /* @__PURE__ */ jsx(SignInButton, {}),
|
|
171
|
+
loadingComponent = /* @__PURE__ */ jsx("div", { children: "Loading..." })
|
|
172
|
+
}) {
|
|
173
|
+
const { isAuthenticated, loading } = useOAuth42Session();
|
|
174
|
+
if (loading) {
|
|
175
|
+
return /* @__PURE__ */ jsx(Fragment, { children: loadingComponent });
|
|
176
|
+
}
|
|
177
|
+
if (!isAuthenticated) {
|
|
178
|
+
return /* @__PURE__ */ jsx(Fragment, { children: fallback });
|
|
179
|
+
}
|
|
180
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
181
|
+
}
|
|
182
|
+
export {
|
|
183
|
+
AuthStatus,
|
|
184
|
+
ProtectedComponent,
|
|
185
|
+
SessionProvider,
|
|
186
|
+
SignInButton,
|
|
187
|
+
SignOutButton,
|
|
188
|
+
UserProfile,
|
|
189
|
+
signIn3 as signIn,
|
|
190
|
+
signOut3 as signOut,
|
|
191
|
+
useOAuth42Session,
|
|
192
|
+
useOAuth42Tokens,
|
|
193
|
+
useOAuth42User,
|
|
194
|
+
useRequireAuth,
|
|
195
|
+
useSession2 as useSession
|
|
196
|
+
};
|
|
197
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/client/index.ts","../../src/client/hooks.ts","../../src/client/components.tsx"],"sourcesContent":["// Client-side exports\n\n// Re-export commonly used next-auth/react functions\nexport { signIn, signOut, useSession, SessionProvider } from 'next-auth/react';\nexport type { Session } from 'next-auth';\nexport {\n useOAuth42Session,\n useOAuth42User,\n useOAuth42Tokens,\n useRequireAuth,\n} from './hooks';\n\nexport type {\n OAuth42Session,\n UseOAuth42SessionReturn,\n} from './hooks';\n\nexport {\n SignInButton,\n SignOutButton,\n UserProfile,\n AuthStatus,\n ProtectedComponent,\n} from './components';\n\nexport type {\n SignInButtonProps,\n SignOutButtonProps,\n UserProfileProps,\n AuthStatusProps,\n ProtectedComponentProps,\n} from './components';","import { useSession, signIn, signOut } from 'next-auth/react';\nimport { useCallback, useEffect, useState } from 'react';\n\nexport type OAuth42Session<E = {}> = ({\n user?: {\n email?: string | null;\n name?: string | null;\n image?: string | null;\n username?: string;\n emailVerified?: boolean;\n };\n accessToken?: string;\n idToken?: string;\n expires?: string;\n}) & E;\n\nexport interface UseOAuth42SessionReturn<E = {}> {\n session: OAuth42Session<E> | null;\n loading: boolean;\n error: Error | null;\n isAuthenticated: boolean;\n signIn: () => Promise<void>;\n signOut: () => Promise<void>;\n}\n\n/**\n * Hook to manage OAuth42 session with optional extra fields\n */\nexport function useOAuth42Session<E = {}>(): UseOAuth42SessionReturn<E> {\n const { data: session, status } = useSession();\n const [error, setError] = useState<Error | null>(null);\n \n const handleSignIn = useCallback(async () => {\n try {\n setError(null);\n await signIn('oauth42');\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n const handleSignOut = useCallback(async () => {\n try {\n setError(null);\n await signOut();\n } catch (err) {\n setError(err as Error);\n }\n }, []);\n \n return {\n session: session as unknown as OAuth42Session<E> | null,\n loading: status === 'loading',\n error,\n isAuthenticated: status === 'authenticated',\n signIn: handleSignIn,\n signOut: handleSignOut,\n };\n}\n\n/**\n * Hook to get the current OAuth42 user\n */\nexport function useOAuth42User<E = {}>() {\n const { session, isAuthenticated } = useOAuth42Session<E>();\n \n return {\n user: isAuthenticated ? session?.user : null,\n isAuthenticated,\n };\n}\n\n/**\n * Hook to manage OAuth42 tokens\n */\nexport function useOAuth42Tokens<E = {}>() {\n const { session } = useOAuth42Session<E>();\n const [isExpired, setIsExpired] = useState(false);\n \n useEffect(() => {\n if (session?.expires) {\n const expiryTime = new Date(session.expires).getTime();\n const now = Date.now();\n setIsExpired(now >= expiryTime);\n \n // Set a timer to update expiry status\n const timeUntilExpiry = expiryTime - now;\n if (timeUntilExpiry > 0) {\n const timer = setTimeout(() => {\n setIsExpired(true);\n }, timeUntilExpiry);\n \n return () => clearTimeout(timer);\n }\n }\n }, [session?.expires]);\n \n return {\n accessToken: session?.accessToken,\n idToken: session?.idToken,\n isExpired,\n refreshToken: async () => {\n // Trigger a session refresh\n await signIn('oauth42');\n },\n };\n}\n\n/**\n * Hook for protected routes\n */\nexport function useRequireAuth(redirectTo: string = '/auth/signin') {\n const { isAuthenticated, loading } = useOAuth42Session();\n const [isRedirecting, setIsRedirecting] = useState(false);\n \n useEffect(() => {\n if (!loading && !isAuthenticated && !isRedirecting) {\n setIsRedirecting(true);\n if (typeof window !== 'undefined') {\n window.location.href = redirectTo;\n }\n }\n }, [isAuthenticated, loading, redirectTo, isRedirecting]);\n \n return {\n isAuthenticated,\n loading: loading || isRedirecting,\n };\n}\n","import React from 'react';\nimport { signIn, signOut } from 'next-auth/react';\nimport { useOAuth42Session, useOAuth42User } from './hooks';\n\nexport interface SignInButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign in button component\n */\nexport function SignInButton({ \n children = 'Sign in with OAuth42', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignInButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signIn('oauth42', { callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface SignOutButtonProps {\n children?: React.ReactNode;\n className?: string;\n callbackUrl?: string;\n onClick?: () => void;\n}\n\n/**\n * Sign out button component\n */\nexport function SignOutButton({ \n children = 'Sign out', \n className = '',\n callbackUrl = '/',\n onClick\n}: SignOutButtonProps) {\n const handleClick = async () => {\n if (onClick) onClick();\n await signOut({ callbackUrl });\n };\n \n return (\n <button\n onClick={handleClick}\n className={className}\n type=\"button\"\n >\n {children}\n </button>\n );\n}\n\nexport interface UserProfileProps {\n className?: string;\n showEmail?: boolean;\n showName?: boolean;\n showImage?: boolean;\n loadingComponent?: React.ReactNode;\n notAuthenticatedComponent?: React.ReactNode;\n}\n\n/**\n * User profile display component\n */\nexport function UserProfile({\n className = '',\n showEmail = true,\n showName = true,\n showImage = true,\n loadingComponent = <div>Loading...</div>,\n notAuthenticatedComponent = <div>Not authenticated</div>,\n}: UserProfileProps) {\n const { session, loading, isAuthenticated } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated || !session?.user) {\n return <>{notAuthenticatedComponent}</>;\n }\n \n const { user } = session;\n \n return (\n <div className={className}>\n {showImage && user.image && (\n <img \n src={user.image} \n alt={user.name || 'User'} \n style={{ width: 50, height: 50, borderRadius: '50%' }}\n />\n )}\n {showName && user.name && <div>{user.name}</div>}\n {showEmail && user.email && <div>{user.email}</div>}\n </div>\n );\n}\n\nexport interface AuthStatusProps {\n authenticatedComponent?: React.ReactNode;\n unauthenticatedComponent?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Conditional rendering based on auth status\n */\nexport function AuthStatus({\n authenticatedComponent,\n unauthenticatedComponent,\n loadingComponent = <div>Loading...</div>,\n}: AuthStatusProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n return <>{isAuthenticated ? authenticatedComponent : unauthenticatedComponent}</>;\n}\n\nexport interface ProtectedComponentProps {\n children: React.ReactNode;\n fallback?: React.ReactNode;\n loadingComponent?: React.ReactNode;\n}\n\n/**\n * Wrapper component for protected content\n */\nexport function ProtectedComponent({\n children,\n fallback = <SignInButton />,\n loadingComponent = <div>Loading...</div>,\n}: ProtectedComponentProps) {\n const { isAuthenticated, loading } = useOAuth42Session();\n \n if (loading) {\n return <>{loadingComponent}</>;\n }\n \n if (!isAuthenticated) {\n return <>{fallback}</>;\n }\n \n return <>{children}</>;\n}"],"mappings":";AAGA,SAAS,UAAAA,SAAQ,WAAAC,UAAS,cAAAC,aAAY,uBAAuB;;;ACH7D,SAAS,YAAY,QAAQ,eAAe;AAC5C,SAAS,aAAa,WAAW,gBAAgB;AA2B1C,SAAS,oBAAwD;AACtE,QAAM,EAAE,MAAM,SAAS,OAAO,IAAI,WAAW;AAC7C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAuB,IAAI;AAErD,QAAM,eAAe,YAAY,YAAY;AAC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@@ -0,0 +1,122 @@
|
|
|
1
|
+
import * as next_auth from 'next-auth';
|
|
2
|
+
import { NextAuthOptions } from 'next-auth';
|
|
3
|
+
import { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from 'next';
|
|
4
|
+
import { OAuthUserConfig, OAuthConfig } from 'next-auth/providers/oauth';
|
|
5
|
+
import { NextRequest, NextResponse } from 'next/server';
|
|
6
|
+
|
|
7
|
+
interface OAuth42Profile {
|
|
8
|
+
sub: string;
|
|
9
|
+
email: string;
|
|
10
|
+
email_verified?: boolean;
|
|
11
|
+
name?: string;
|
|
12
|
+
given_name?: string;
|
|
13
|
+
family_name?: string;
|
|
14
|
+
picture?: string;
|
|
15
|
+
username?: string;
|
|
16
|
+
id?: string;
|
|
17
|
+
}
|
|
18
|
+
interface OAuth42ProviderOptions {
|
|
19
|
+
clientId: string;
|
|
20
|
+
clientSecret: string;
|
|
21
|
+
issuer?: string;
|
|
22
|
+
authorizationUrl?: string;
|
|
23
|
+
tokenUrl?: string;
|
|
24
|
+
userinfoUrl?: string;
|
|
25
|
+
scopes?: string[];
|
|
26
|
+
pkceEnabled?: boolean;
|
|
27
|
+
}
|
|
28
|
+
declare function OAuth42Provider<P extends OAuth42Profile>(options: OAuthUserConfig<P> & Partial<OAuth42ProviderOptions>): OAuthConfig<P>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get the OAuth42 session server-side
|
|
32
|
+
*
|
|
33
|
+
* This is the primary method for retrieving sessions in OAuth42 SDK.
|
|
34
|
+
* Supports both Pages Router and App Router:
|
|
35
|
+
*
|
|
36
|
+
* App Router:
|
|
37
|
+
* ```ts
|
|
38
|
+
* const session = await getOAuth42Session(authOptions);
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* Pages Router:
|
|
42
|
+
* ```ts
|
|
43
|
+
* const session = await getOAuth42Session(req, res, authOptions);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function getOAuth42Session(...args: [GetServerSidePropsContext['req'], GetServerSidePropsContext['res'], NextAuthOptions] | [NextApiRequest, NextApiResponse, NextAuthOptions] | [NextAuthOptions]): Promise<next_auth.Session | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Helper for protecting API routes
|
|
49
|
+
*/
|
|
50
|
+
declare function withOAuth42Session(handler: (req: NextApiRequest, res: NextApiResponse, session: any) => Promise<void> | void, authOptions: NextAuthOptions): (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Helper for protecting server-side props
|
|
53
|
+
*/
|
|
54
|
+
declare function withOAuth42ServerSideProps(getServerSideProps: (context: GetServerSidePropsContext, session: any) => Promise<any>, authOptions: NextAuthOptions): (context: GetServerSidePropsContext) => Promise<any>;
|
|
55
|
+
|
|
56
|
+
interface CreateAuthOptions {
|
|
57
|
+
clientId?: string;
|
|
58
|
+
clientSecret?: string;
|
|
59
|
+
issuer?: string;
|
|
60
|
+
scopes?: string[];
|
|
61
|
+
pkceEnabled?: boolean;
|
|
62
|
+
debug?: boolean;
|
|
63
|
+
callbacks?: NextAuthOptions['callbacks'];
|
|
64
|
+
pages?: NextAuthOptions['pages'];
|
|
65
|
+
session?: NextAuthOptions['session'];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Create a pre-configured NextAuth instance for OAuth42
|
|
69
|
+
* This provides a simplified setup with sensible defaults
|
|
70
|
+
*/
|
|
71
|
+
declare function createAuth(options?: CreateAuthOptions): {
|
|
72
|
+
auth: NextAuthOptions;
|
|
73
|
+
handlers: any;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Create NextAuth handlers for API routes
|
|
77
|
+
*/
|
|
78
|
+
declare function createHandlers(authOptions: NextAuthOptions): {
|
|
79
|
+
GET: any;
|
|
80
|
+
POST: any;
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Helper to get the current session server-side
|
|
84
|
+
* @deprecated Use getOAuth42Session instead - this is now just an alias for backward compatibility
|
|
85
|
+
*
|
|
86
|
+
* This function is maintained for backward compatibility but internally
|
|
87
|
+
* calls getOAuth42Session which properly handles both App Router and Pages Router
|
|
88
|
+
*/
|
|
89
|
+
declare const getServerSession: typeof getOAuth42Session;
|
|
90
|
+
/**
|
|
91
|
+
* Token refresh helper
|
|
92
|
+
*/
|
|
93
|
+
declare function refreshAccessToken(token: any, clientId: string, clientSecret: string, issuer?: string): Promise<any>;
|
|
94
|
+
|
|
95
|
+
interface OAuth42AuthOptions {
|
|
96
|
+
pages?: {
|
|
97
|
+
signIn?: string;
|
|
98
|
+
error?: string;
|
|
99
|
+
};
|
|
100
|
+
callbacks?: {
|
|
101
|
+
authorized?: (params: {
|
|
102
|
+
token: any;
|
|
103
|
+
req: NextRequest;
|
|
104
|
+
}) => boolean | Promise<boolean>;
|
|
105
|
+
};
|
|
106
|
+
protectedPaths?: string[];
|
|
107
|
+
publicPaths?: string[];
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Middleware helper for protecting routes with OAuth42
|
|
111
|
+
*/
|
|
112
|
+
declare function withOAuth42Auth(options?: OAuth42AuthOptions): (req: NextRequest) => Promise<NextResponse<unknown>>;
|
|
113
|
+
/**
|
|
114
|
+
* Helper to create middleware configuration
|
|
115
|
+
*/
|
|
116
|
+
declare function createMiddlewareConfig(protectedPaths?: string[], publicPaths?: string[]): {
|
|
117
|
+
matcher: string[];
|
|
118
|
+
protectedPaths: string[];
|
|
119
|
+
publicPaths: string[];
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export { type CreateAuthOptions as C, OAuth42Provider as O, type OAuth42Profile as a, type OAuth42ProviderOptions as b, createAuth as c, createMiddlewareConfig as d, type OAuth42AuthOptions as e, getOAuth42Session as f, getServerSession as g, withOAuth42Session as h, withOAuth42ServerSideProps as i, createHandlers as j, refreshAccessToken as r, withOAuth42Auth as w };
|