@tern-secure/nextjs 4.0.0 → 4.2.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/dist/cjs/app-router/client/TernSecureProvider.js +17 -2
- package/dist/cjs/app-router/client/TernSecureProvider.js.map +1 -1
- package/dist/cjs/app-router/client/actions.js +49 -49
- package/dist/cjs/app-router/client/actions.js.map +1 -1
- package/dist/cjs/app-router/route-handler/internal-route.js +17 -2
- package/dist/cjs/app-router/route-handler/internal-route.js.map +1 -1
- package/dist/cjs/app-router/server/auth.js +42 -28
- package/dist/cjs/app-router/server/auth.js.map +1 -1
- package/dist/cjs/app-router/server/edge-session.js +80 -0
- package/dist/cjs/app-router/server/edge-session.js.map +1 -0
- package/dist/cjs/app-router/server/index.js +4 -0
- package/dist/cjs/app-router/server/index.js.map +1 -1
- package/dist/cjs/app-router/server/jwt.js +141 -0
- package/dist/cjs/app-router/server/jwt.js.map +1 -0
- package/dist/cjs/app-router/server/sessionTernSecure.js +14 -9
- package/dist/cjs/app-router/server/sessionTernSecure.js.map +1 -1
- package/dist/cjs/app-router/server/ternSecureMiddleware.js +134 -13
- package/dist/cjs/app-router/server/ternSecureMiddleware.js.map +1 -1
- package/dist/cjs/boundary/TernSecureClientProvider.js +163 -40
- package/dist/cjs/boundary/TernSecureClientProvider.js.map +1 -1
- package/dist/cjs/boundary/TernSecureCtx.js.map +1 -1
- package/dist/cjs/boundary/hooks/useAuth.js +7 -8
- package/dist/cjs/boundary/hooks/useAuth.js.map +1 -1
- package/dist/cjs/components/sign-in.js +136 -45
- package/dist/cjs/components/sign-in.js.map +1 -1
- package/dist/cjs/components/sign-out-button.js +10 -1
- package/dist/cjs/components/sign-out-button.js.map +1 -1
- package/dist/cjs/components/sign-out.js +12 -3
- package/dist/cjs/components/sign-out.js.map +1 -1
- package/dist/cjs/components/sign-up.js +10 -5
- package/dist/cjs/components/sign-up.js.map +1 -1
- package/dist/cjs/errors.js +232 -5
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/index.js +0 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types.js +14 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utils/construct.js +50 -18
- package/dist/cjs/utils/construct.js.map +1 -1
- package/dist/cjs/utils/redirect.js +57 -0
- package/dist/cjs/utils/redirect.js.map +1 -0
- package/dist/esm/app-router/client/TernSecureProvider.js +17 -2
- package/dist/esm/app-router/client/TernSecureProvider.js.map +1 -1
- package/dist/esm/app-router/client/actions.js +59 -51
- package/dist/esm/app-router/client/actions.js.map +1 -1
- package/dist/esm/app-router/route-handler/internal-route.js +13 -1
- package/dist/esm/app-router/route-handler/internal-route.js.map +1 -1
- package/dist/esm/app-router/server/auth.js +40 -28
- package/dist/esm/app-router/server/auth.js.map +1 -1
- package/dist/esm/app-router/server/edge-session.js +56 -0
- package/dist/esm/app-router/server/edge-session.js.map +1 -0
- package/dist/esm/app-router/server/index.js +4 -2
- package/dist/esm/app-router/server/index.js.map +1 -1
- package/dist/esm/app-router/server/jwt.js +117 -0
- package/dist/esm/app-router/server/jwt.js.map +1 -0
- package/dist/esm/app-router/server/sessionTernSecure.js +14 -9
- package/dist/esm/app-router/server/sessionTernSecure.js.map +1 -1
- package/dist/esm/app-router/server/ternSecureMiddleware.js +132 -13
- package/dist/esm/app-router/server/ternSecureMiddleware.js.map +1 -1
- package/dist/esm/boundary/TernSecureClientProvider.js +164 -41
- package/dist/esm/boundary/TernSecureClientProvider.js.map +1 -1
- package/dist/esm/boundary/TernSecureCtx.js.map +1 -1
- package/dist/esm/boundary/hooks/useAuth.js +7 -8
- package/dist/esm/boundary/hooks/useAuth.js.map +1 -1
- package/dist/esm/components/sign-in.js +137 -46
- package/dist/esm/components/sign-in.js.map +1 -1
- package/dist/esm/components/sign-out-button.js +11 -2
- package/dist/esm/components/sign-out-button.js.map +1 -1
- package/dist/esm/components/sign-out.js +13 -4
- package/dist/esm/components/sign-out.js.map +1 -1
- package/dist/esm/components/sign-up.js +10 -5
- package/dist/esm/components/sign-up.js.map +1 -1
- package/dist/esm/errors.js +228 -4
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +0 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.js +6 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utils/construct.js +46 -17
- package/dist/esm/utils/construct.js.map +1 -1
- package/dist/esm/utils/redirect.js +32 -0
- package/dist/esm/utils/redirect.js.map +1 -0
- package/dist/types/app-router/client/TernSecureProvider.d.ts +14 -3
- package/dist/types/app-router/client/TernSecureProvider.d.ts.map +1 -1
- package/dist/types/app-router/client/actions.d.ts +23 -21
- package/dist/types/app-router/client/actions.d.ts.map +1 -1
- package/dist/types/app-router/route-handler/internal-route.d.ts +3 -0
- package/dist/types/app-router/route-handler/internal-route.d.ts.map +1 -1
- package/dist/types/app-router/server/auth.d.ts +13 -1
- package/dist/types/app-router/server/auth.d.ts.map +1 -1
- package/dist/types/app-router/server/edge-session.d.ts +15 -0
- package/dist/types/app-router/server/edge-session.d.ts.map +1 -0
- package/dist/types/app-router/server/index.d.ts +3 -2
- package/dist/types/app-router/server/index.d.ts.map +1 -1
- package/dist/types/app-router/server/jwt.d.ts +20 -0
- package/dist/types/app-router/server/jwt.d.ts.map +1 -0
- package/dist/types/app-router/server/sessionTernSecure.d.ts +4 -1
- package/dist/types/app-router/server/sessionTernSecure.d.ts.map +1 -1
- package/dist/types/app-router/server/ternSecureMiddleware.d.ts +17 -4
- package/dist/types/app-router/server/ternSecureMiddleware.d.ts.map +1 -1
- package/dist/types/boundary/TernSecureClientProvider.d.ts +17 -1
- package/dist/types/boundary/TernSecureClientProvider.d.ts.map +1 -1
- package/dist/types/boundary/TernSecureCtx.d.ts +3 -1
- package/dist/types/boundary/TernSecureCtx.d.ts.map +1 -1
- package/dist/types/boundary/hooks/useAuth.d.ts +4 -1
- package/dist/types/boundary/hooks/useAuth.d.ts.map +1 -1
- package/dist/types/components/sign-in.d.ts +1 -2
- package/dist/types/components/sign-in.d.ts.map +1 -1
- package/dist/types/components/sign-out-button.d.ts +2 -1
- package/dist/types/components/sign-out-button.d.ts.map +1 -1
- package/dist/types/components/sign-out.d.ts +2 -1
- package/dist/types/components/sign-out.d.ts.map +1 -1
- package/dist/types/components/sign-up.d.ts.map +1 -1
- package/dist/types/components/ui/alert.d.ts +1 -1
- package/dist/types/components/ui/button.d.ts +1 -1
- package/dist/types/errors.d.ts +36 -2
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +35 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/construct.d.ts +20 -4
- package/dist/types/utils/construct.d.ts.map +1 -1
- package/dist/types/utils/redirect.d.ts +9 -0
- package/dist/types/utils/redirect.d.ts.map +1 -0
- package/package.json +7 -6
- package/dist/cjs/boundary/hooks/useUser.js +0 -44
- package/dist/cjs/boundary/hooks/useUser.js.map +0 -1
- package/dist/esm/boundary/hooks/useUser.js +0 -20
- package/dist/esm/boundary/hooks/useUser.js.map +0 -1
- package/dist/types/boundary/hooks/useUser.d.ts +0 -7
- package/dist/types/boundary/hooks/useUser.d.ts.map +0 -1
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { TernSecureAuth } from "../../utils/client-init";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
signInWithEmailAndPassword,
|
|
4
|
+
signInWithRedirect,
|
|
5
|
+
getRedirectResult,
|
|
6
|
+
GoogleAuthProvider,
|
|
7
|
+
OAuthProvider,
|
|
8
|
+
createUserWithEmailAndPassword,
|
|
9
|
+
sendEmailVerification
|
|
10
|
+
} from "firebase/auth";
|
|
11
|
+
import { handleFirebaseAuthError } from "../../errors";
|
|
4
12
|
async function createUser(email, password) {
|
|
5
13
|
const auth = TernSecureAuth();
|
|
6
14
|
try {
|
|
@@ -10,27 +18,19 @@ async function createUser(email, password) {
|
|
|
10
18
|
};
|
|
11
19
|
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
|
|
12
20
|
await sendEmailVerification(userCredential.user, actionCodeSettings);
|
|
13
|
-
return {
|
|
21
|
+
return {
|
|
22
|
+
success: true,
|
|
23
|
+
message: "Account created successfully. Please check your email for verification",
|
|
24
|
+
user: userCredential.user
|
|
25
|
+
};
|
|
14
26
|
} catch (error) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
throw new Error("Email is already registered.");
|
|
23
|
-
case "auth/invalid-email":
|
|
24
|
-
throw new Error("Invalid email address.");
|
|
25
|
-
case "auth/operation-not-allowed":
|
|
26
|
-
throw new Error("Email/password accounts are not enabled.");
|
|
27
|
-
case "auth/weak-password":
|
|
28
|
-
throw new Error("Password is too weak.");
|
|
29
|
-
default:
|
|
30
|
-
throw new Error(error.message);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
throw new Error("Failed to create account");
|
|
27
|
+
const authError = handleFirebaseAuthError(error);
|
|
28
|
+
return {
|
|
29
|
+
success: false,
|
|
30
|
+
message: authError.message,
|
|
31
|
+
error: authError.code,
|
|
32
|
+
user: null
|
|
33
|
+
};
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
async function signInWithEmail(email, password) {
|
|
@@ -41,12 +41,17 @@ async function signInWithEmail(email, password) {
|
|
|
41
41
|
return {
|
|
42
42
|
success: true,
|
|
43
43
|
message: "Authentication successful",
|
|
44
|
-
user
|
|
45
|
-
error: !user.emailVerified ?
|
|
44
|
+
user,
|
|
45
|
+
error: !user.emailVerified ? "REQUIRES_VERIFICATION" : "AUTHENTICATED"
|
|
46
46
|
};
|
|
47
47
|
} catch (error) {
|
|
48
|
-
const
|
|
49
|
-
|
|
48
|
+
const authError = handleFirebaseAuthError(error);
|
|
49
|
+
return {
|
|
50
|
+
success: false,
|
|
51
|
+
message: authError.message,
|
|
52
|
+
error: authError.code,
|
|
53
|
+
user: null
|
|
54
|
+
};
|
|
50
55
|
}
|
|
51
56
|
}
|
|
52
57
|
async function signInWithRedirectGoogle() {
|
|
@@ -60,8 +65,13 @@ async function signInWithRedirectGoogle() {
|
|
|
60
65
|
await signInWithRedirect(auth, provider);
|
|
61
66
|
return { success: true, message: "Redirect initiated" };
|
|
62
67
|
} catch (error) {
|
|
63
|
-
|
|
64
|
-
return {
|
|
68
|
+
const authError = handleFirebaseAuthError(error);
|
|
69
|
+
return {
|
|
70
|
+
success: false,
|
|
71
|
+
message: authError.message,
|
|
72
|
+
error: authError.code,
|
|
73
|
+
user: null
|
|
74
|
+
};
|
|
65
75
|
}
|
|
66
76
|
}
|
|
67
77
|
async function signInWithMicrosoft() {
|
|
@@ -74,8 +84,13 @@ async function signInWithMicrosoft() {
|
|
|
74
84
|
await signInWithRedirect(auth, provider);
|
|
75
85
|
return { success: true, message: "Redirect initiated" };
|
|
76
86
|
} catch (error) {
|
|
77
|
-
|
|
78
|
-
return {
|
|
87
|
+
const authError = handleFirebaseAuthError(error);
|
|
88
|
+
return {
|
|
89
|
+
success: false,
|
|
90
|
+
message: authError.message,
|
|
91
|
+
error: authError.code,
|
|
92
|
+
user: null
|
|
93
|
+
};
|
|
79
94
|
}
|
|
80
95
|
}
|
|
81
96
|
async function handleAuthRedirectResult() {
|
|
@@ -89,8 +104,13 @@ async function handleAuthRedirectResult() {
|
|
|
89
104
|
return { success: false, error: "No redirect result" };
|
|
90
105
|
}
|
|
91
106
|
} catch (error) {
|
|
92
|
-
|
|
93
|
-
return {
|
|
107
|
+
const authError = handleFirebaseAuthError(error);
|
|
108
|
+
return {
|
|
109
|
+
success: false,
|
|
110
|
+
message: authError.message,
|
|
111
|
+
error: authError.code,
|
|
112
|
+
user: null
|
|
113
|
+
};
|
|
94
114
|
}
|
|
95
115
|
}
|
|
96
116
|
async function resendEmailVerification() {
|
|
@@ -119,25 +139,13 @@ async function resendEmailVerification() {
|
|
|
119
139
|
isVerified: false
|
|
120
140
|
};
|
|
121
141
|
} catch (error) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
throw new Error("Email is already registered.");
|
|
130
|
-
case "auth/invalid-email":
|
|
131
|
-
throw new Error("Invalid email address.");
|
|
132
|
-
case "auth/operation-not-allowed":
|
|
133
|
-
throw new Error("Email/password accounts are not enabled.");
|
|
134
|
-
case "auth/weak-password":
|
|
135
|
-
throw new Error("Password is too weak.");
|
|
136
|
-
default:
|
|
137
|
-
throw new Error(error.message);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
throw new Error("Failed to resend verification email.");
|
|
142
|
+
const authError = handleFirebaseAuthError(error);
|
|
143
|
+
return {
|
|
144
|
+
success: false,
|
|
145
|
+
message: authError.message,
|
|
146
|
+
error: authError.code,
|
|
147
|
+
user: null
|
|
148
|
+
};
|
|
141
149
|
}
|
|
142
150
|
}
|
|
143
151
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/app-router/client/actions.ts"],"sourcesContent":["import { TernSecureAuth } from '../../utils/client-init'\nimport { signInWithEmailAndPassword, signInWithRedirect, getRedirectResult, GoogleAuthProvider, OAuthProvider, createUserWithEmailAndPassword, sendEmailVerification } from 'firebase/auth'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../src/app-router/client/actions.ts"],"sourcesContent":["import { TernSecureAuth } from '../../utils/client-init'\nimport { \n signInWithEmailAndPassword, \n signInWithRedirect, \n getRedirectResult, \n GoogleAuthProvider, \n OAuthProvider, \n createUserWithEmailAndPassword, \n sendEmailVerification } from 'firebase/auth'\nimport type { SignInResponse } from '../../types'\nimport { handleFirebaseAuthError } from '../../errors'\n\n\nexport async function createUser(email: string, password: string): Promise<SignInResponse> {\n const auth = TernSecureAuth()\n try {\n \n const actionCodeSettings = {\n url: `${window.location.origin}/sign-in`,\n handleCodeInApp: true\n };\n\n const userCredential = await createUserWithEmailAndPassword(auth, email, password);\n\n await sendEmailVerification(userCredential.user, actionCodeSettings)\n \n return { \n success: true, \n message: 'Account created successfully. Please check your email for verification', \n user: userCredential.user \n };\n\n } catch (error) {\n const authError = handleFirebaseAuthError(error)\n return { \n success: false, \n message: authError.message, \n error: authError.code,\n user: null\n }\n }\n}\n\n\nexport async function signInWithEmail(email: string, password: string): Promise<SignInResponse> {\n const auth = TernSecureAuth()\n try {\n const UserCredential = await signInWithEmailAndPassword(auth, email, password)\n const user = UserCredential.user\n \n return { \n success: true, \n message: 'Authentication successful',\n user: user,\n error: !user.emailVerified ? 'REQUIRES_VERIFICATION' : 'AUTHENTICATED'\n };\n\n} catch (error){\n const authError = handleFirebaseAuthError(error)\n return { \n success: false,\n message: authError.message,\n error: authError.code,\n user: null\n }\n}\n}\n\nexport async function signInWithRedirectGoogle() {\n const auth = TernSecureAuth()\n const provider = new GoogleAuthProvider()\n provider.setCustomParameters({\n login_hint: 'user@example.com',\n prompt: 'select_account'\n })\n\n try {\n await signInWithRedirect(auth, provider)\n return { success: true, message: 'Redirect initiated' }\n } catch (error) {\n const authError = handleFirebaseAuthError(error)\n return {\n success: false,\n message: authError.message,\n error: authError.code,\n user: null\n }\n }\n}\n\n\nexport async function signInWithMicrosoft() {\n const auth = TernSecureAuth()\n const provider = new OAuthProvider('microsoft.com')\n provider.setCustomParameters({\n prompt: 'consent'\n })\n\n try {\n await signInWithRedirect(auth, provider)\n return { success: true, message: 'Redirect initiated' }\n } catch (error) {\n const authError = handleFirebaseAuthError(error)\n return {\n success: false, \n message: authError.message,\n error: authError.code,\n user: null\n }\n }\n}\n\n\nexport async function handleAuthRedirectResult() {\n const auth = TernSecureAuth()\n try {\n const result = await getRedirectResult(auth)\n if (result) {\n const user = result.user\n return { success: true, user }\n } else {\n return { success: false, error: 'No redirect result' }\n }\n } catch (error: any) {\n const authError = handleFirebaseAuthError(error)\n return {\n success: false,\n message: authError.message,\n error: authError.code,\n user: null\n }\n }\n}\n\n\nexport async function resendEmailVerification() {\n const auth = TernSecureAuth()\n try {\n const user = auth.currentUser;\n if (!user) {\n throw new Error('No user found. Please try signing up again.');\n }\n\n await user.reload();\n\n if (user.emailVerified) {\n return { \n success: true, \n message: 'Email is already verified. You can sign in.',\n isVerified: true \n };\n }\n\n const actionCodeSettings = {\n url: `${window.location.origin}/sign-in`,\n handleCodeInApp: true,\n };\n\n await sendEmailVerification(user, actionCodeSettings);\n return { \n success: true, \n message: 'Verification email sent successfully.',\n isVerified: false\n };\n } catch (error) {\n const authError = handleFirebaseAuthError(error)\n return {\n success: false,\n message: authError.message,\n error: authError.code,\n user: null\n }\n }\n}"],"mappings":"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","names":[]}
|
|
@@ -9,6 +9,15 @@ const internalRoutes = {
|
|
|
9
9
|
component: Verify
|
|
10
10
|
}
|
|
11
11
|
};
|
|
12
|
+
function isInternalRoute(pathname) {
|
|
13
|
+
return Object.values(internalRoutes).some((route) => route.pattern.test(pathname));
|
|
14
|
+
}
|
|
15
|
+
function isAuthRoute(pathname) {
|
|
16
|
+
return pathname.startsWith("/sign-in") || pathname.startsWith("/sign-up");
|
|
17
|
+
}
|
|
18
|
+
function isBaseAuthRoute(pathname) {
|
|
19
|
+
return pathname === "/sign-in" || pathname === "/sign-up";
|
|
20
|
+
}
|
|
12
21
|
function handleInternalRoute(pathname) {
|
|
13
22
|
for (const [key, route] of Object.entries(internalRoutes)) {
|
|
14
23
|
if (route.pattern.test(pathname)) {
|
|
@@ -19,6 +28,9 @@ function handleInternalRoute(pathname) {
|
|
|
19
28
|
}
|
|
20
29
|
export {
|
|
21
30
|
handleInternalRoute,
|
|
22
|
-
internalRoutes
|
|
31
|
+
internalRoutes,
|
|
32
|
+
isAuthRoute,
|
|
33
|
+
isBaseAuthRoute,
|
|
34
|
+
isInternalRoute
|
|
23
35
|
};
|
|
24
36
|
//# sourceMappingURL=internal-route.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/app-router/route-handler/internal-route.tsx"],"sourcesContent":["import { Verify } from \"../../components/verify\"\n\n// Internal route mapping\nexport const internalRoutes = {\n signUpVerify: {\n pattern: /^\\/sign-up\\/verify$/,\n component: Verify,\n },\n signInVerify: {\n pattern: /^\\/sign-in\\/verify$/,\n component: Verify,\n },\n}\n\n// Internal route handler\nexport function handleInternalRoute(pathname: string) {\n for (const [key, route] of Object.entries(internalRoutes)) {\n if (route.pattern.test(pathname)) {\n return route.component\n }\n }\n return null\n}
|
|
1
|
+
{"version":3,"sources":["../../../../src/app-router/route-handler/internal-route.tsx"],"sourcesContent":["import { Verify } from \"../../components/verify\"\n\n// Internal route mapping\nexport const internalRoutes = {\n signUpVerify: {\n pattern: /^\\/sign-up\\/verify$/,\n component: Verify,\n },\n signInVerify: {\n pattern: /^\\/sign-in\\/verify$/,\n component: Verify,\n },\n}\n\n// Check if path is an internal route\nexport function isInternalRoute(pathname: string): boolean {\n return Object.values(internalRoutes).some((route) => route.pattern.test(pathname))\n}\n\n// Check if path is within auth routes\nexport function isAuthRoute(pathname: string): boolean {\n return pathname.startsWith(\"/sign-in\") || pathname.startsWith(\"/sign-up\")\n}\n\n// Check if path is exactly the base auth route\nexport function isBaseAuthRoute(pathname: string): boolean {\n return pathname === \"/sign-in\" || pathname === \"/sign-up\"\n}\n\n// Internal route handler\nexport function handleInternalRoute(pathname: string) {\n for (const [key, route] of Object.entries(internalRoutes)) {\n if (route.pattern.test(pathname)) {\n return route.component\n }\n }\n return null\n}"],"mappings":"AAAA,SAAS,cAAc;AAGhB,MAAM,iBAAiB;AAAA,EAC5B,cAAc;AAAA,IACZ,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AAAA,EACA,cAAc;AAAA,IACZ,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AACF;AAGO,SAAS,gBAAgB,UAA2B;AACzD,SAAO,OAAO,OAAO,cAAc,EAAE,KAAK,CAAC,UAAU,MAAM,QAAQ,KAAK,QAAQ,CAAC;AACnF;AAGO,SAAS,YAAY,UAA2B;AACrD,SAAO,SAAS,WAAW,UAAU,KAAK,SAAS,WAAW,UAAU;AAC1E;AAGO,SAAS,gBAAgB,UAA2B;AACzD,SAAO,aAAa,cAAc,aAAa;AACjD;AAGO,SAAS,oBAAoB,UAAkB;AACpD,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,cAAc,GAAG;AACzD,QAAI,MAAM,QAAQ,KAAK,QAAQ,GAAG;AAChC,aAAO,MAAM;AAAA,IACf;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
|
|
@@ -1,47 +1,59 @@
|
|
|
1
1
|
"use server";
|
|
2
|
-
import { cookies } from "next/headers";
|
|
3
|
-
import { verifyTernIdToken, verifyTernSessionCookie } from "./sessionTernSecure";
|
|
2
|
+
import { cookies, headers } from "next/headers";
|
|
4
3
|
async function auth() {
|
|
5
|
-
var _a, _b
|
|
4
|
+
var _a, _b;
|
|
6
5
|
try {
|
|
6
|
+
const headersList = await headers();
|
|
7
7
|
const cookieStore = await cookies();
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
userId: (_c = tokenResult.uid) != null ? _c : null,
|
|
25
|
-
token: idToken,
|
|
26
|
-
error: null
|
|
27
|
-
};
|
|
28
|
-
}
|
|
8
|
+
const userId = headersList.get("x-user-id");
|
|
9
|
+
const authTime = headersList.get("x-auth-time");
|
|
10
|
+
const emailVerified = headersList.get("x-auth-verified") === "true";
|
|
11
|
+
if (userId) {
|
|
12
|
+
const token = ((_a = cookieStore.get("_session_cookie")) == null ? void 0 : _a.value) || ((_b = cookieStore.get("_session_token")) == null ? void 0 : _b.value) || null;
|
|
13
|
+
return {
|
|
14
|
+
user: {
|
|
15
|
+
uid: userId,
|
|
16
|
+
email: headersList.get("x-user-email") || null,
|
|
17
|
+
emailVerified,
|
|
18
|
+
authTime: authTime ? parseInt(authTime) : void 0
|
|
19
|
+
},
|
|
20
|
+
token,
|
|
21
|
+
error: null
|
|
22
|
+
};
|
|
29
23
|
}
|
|
30
24
|
return {
|
|
31
|
-
|
|
25
|
+
user: null,
|
|
32
26
|
token: null,
|
|
33
27
|
error: new Error("No valid session or token found")
|
|
34
28
|
};
|
|
35
29
|
} catch (error) {
|
|
36
|
-
console.error("Error in
|
|
30
|
+
console.error("Error in getAuthResult:", error);
|
|
37
31
|
return {
|
|
38
|
-
|
|
32
|
+
user: null,
|
|
39
33
|
token: null,
|
|
40
34
|
error: error instanceof Error ? error : new Error("An unknown error occurred")
|
|
41
35
|
};
|
|
42
36
|
}
|
|
43
37
|
}
|
|
38
|
+
async function isAuthenticated() {
|
|
39
|
+
const authResult = await auth();
|
|
40
|
+
return authResult.user !== null;
|
|
41
|
+
}
|
|
42
|
+
async function getUserInfo() {
|
|
43
|
+
const authResult = await auth();
|
|
44
|
+
if (!authResult.user) {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
uid: authResult.user.uid,
|
|
49
|
+
email: authResult.user.email,
|
|
50
|
+
emailVerified: authResult.user.emailVerified,
|
|
51
|
+
authTime: authResult.user.authTime
|
|
52
|
+
};
|
|
53
|
+
}
|
|
44
54
|
export {
|
|
45
|
-
auth
|
|
55
|
+
auth,
|
|
56
|
+
getUserInfo,
|
|
57
|
+
isAuthenticated
|
|
46
58
|
};
|
|
47
59
|
//# sourceMappingURL=auth.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/app-router/server/auth.ts"],"sourcesContent":["'use server'\
|
|
1
|
+
{"version":3,"sources":["../../../../src/app-router/server/auth.ts"],"sourcesContent":["'use server'\nimport { cookies, headers } from \"next/headers\"\nimport type { UserInfo, SessionResult } from \"./edge-session\"\n\n\nexport interface AuthResult {\n user: UserInfo | null\n token: string | null\n error: Error | null\n}\n\n\n /**\n * Get the current authenticated user from the session or token\n */\n export async function auth(): Promise<AuthResult> {\n try {\n const headersList = await headers()\n const cookieStore = await cookies()\n\n const userId = headersList.get('x-user-id')\n const authTime = headersList.get('x-auth-time')\n const emailVerified = headersList.get('x-auth-verified') === 'true'\n\n if (userId) {\n const token = cookieStore.get(\"_session_cookie\")?.value || \n cookieStore.get(\"_session_token\")?.value || \n null\n \n return {\n user: {\n uid: userId,\n email: headersList.get('x-user-email') || null,\n emailVerified,\n authTime: authTime ? parseInt(authTime) : undefined\n },\n token,\n error: null\n }\n }\n\n return {\n user: null,\n token: null,\n error: new Error(\"No valid session or token found\"),\n }\n } catch (error) {\n console.error(\"Error in getAuthResult:\", error)\n return {\n user: null,\n token: null,\n error: error instanceof Error ? error : new Error(\"An unknown error occurred\"),\n }\n }\n}\n\n/**\n * Type guard to check if user is authenticated\n */\nexport async function isAuthenticated(): Promise<boolean> {\n const authResult = await auth()\n return authResult.user !== null\n}\n\n/**\n * Get user info from auth result\n */\nexport async function getUserInfo(): Promise<UserInfo | null> {\n const authResult = await auth()\n if (!authResult.user) {\n return null\n }\n\n return {\n uid: authResult.user.uid,\n email: authResult.user.email,\n emailVerified: authResult.user.emailVerified,\n authTime: authResult.user.authTime\n }\n }\n\n\n"],"mappings":";AACA,SAAS,SAAS,eAAe;AAc/B,eAAsB,OAA4B;AAfpD;AAgBI,MAAI;AACF,UAAM,cAAc,MAAM,QAAQ;AAClC,UAAM,cAAc,MAAM,QAAQ;AAElC,UAAM,SAAS,YAAY,IAAI,WAAW;AAC1C,UAAM,WAAW,YAAY,IAAI,aAAa;AAC9C,UAAM,gBAAgB,YAAY,IAAI,iBAAiB,MAAM;AAE7D,QAAI,QAAQ;AACV,YAAM,UAAQ,iBAAY,IAAI,iBAAiB,MAAjC,mBAAoC,YACrC,iBAAY,IAAI,gBAAgB,MAAhC,mBAAmC,UACnC;AAEb,aAAO;AAAA,QACL,MAAM;AAAA,UACJ,KAAK;AAAA,UACL,OAAO,YAAY,IAAI,cAAc,KAAK;AAAA,UAC1C;AAAA,UACA,UAAU,WAAW,SAAS,QAAQ,IAAI;AAAA,QAC5C;AAAA,QACA;AAAA,QACA,OAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,MACL,MAAM;AAAA,MACN,OAAO;AAAA,MACP,OAAO,IAAI,MAAM,iCAAiC;AAAA,IACpD;AAAA,EACF,SAAS,OAAO;AACd,YAAQ,MAAM,2BAA2B,KAAK;AAC9C,WAAO;AAAA,MACL,MAAM;AAAA,MACN,OAAO;AAAA,MACP,OAAO,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,2BAA2B;AAAA,IAC/E;AAAA,EACF;AACJ;AAKA,eAAsB,kBAAoC;AACxD,QAAM,aAAa,MAAM,KAAK;AAC9B,SAAO,WAAW,SAAS;AAC7B;AAKA,eAAsB,cAAwC;AAC5D,QAAM,aAAa,MAAM,KAAK;AAC9B,MAAI,CAAC,WAAW,MAAM;AACpB,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,KAAK,WAAW,KAAK;AAAA,IACrB,OAAO,WAAW,KAAK;AAAA,IACvB,eAAe,WAAW,KAAK;AAAA,IAC/B,UAAU,WAAW,KAAK;AAAA,EAC5B;AACA;","names":[]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { cookies } from "next/headers";
|
|
2
|
+
import { verifyFirebaseToken } from "./jwt";
|
|
3
|
+
async function verifySession(request) {
|
|
4
|
+
var _a, _b, _c, _d, _e, _f;
|
|
5
|
+
try {
|
|
6
|
+
const cookieStore = await cookies();
|
|
7
|
+
const sessionCookie = (_a = cookieStore.get("_session_cookie")) == null ? void 0 : _a.value;
|
|
8
|
+
if (sessionCookie) {
|
|
9
|
+
const result = await verifyFirebaseToken(sessionCookie, true);
|
|
10
|
+
if (result.valid) {
|
|
11
|
+
return {
|
|
12
|
+
isAuthenticated: true,
|
|
13
|
+
user: {
|
|
14
|
+
uid: (_b = result.uid) != null ? _b : "",
|
|
15
|
+
email: result.email || null,
|
|
16
|
+
emailVerified: (_c = result.emailVerified) != null ? _c : false,
|
|
17
|
+
disabled: false
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
console.log("Session cookie verification failed:", result.error);
|
|
22
|
+
}
|
|
23
|
+
const idToken = (_d = cookieStore.get("_session_token")) == null ? void 0 : _d.value;
|
|
24
|
+
if (idToken) {
|
|
25
|
+
const result = await verifyFirebaseToken(idToken, false);
|
|
26
|
+
if (result.valid) {
|
|
27
|
+
return {
|
|
28
|
+
isAuthenticated: true,
|
|
29
|
+
user: {
|
|
30
|
+
uid: (_e = result.uid) != null ? _e : "",
|
|
31
|
+
email: result.email || null,
|
|
32
|
+
emailVerified: (_f = result.emailVerified) != null ? _f : false,
|
|
33
|
+
disabled: false
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
console.log("ID token verification failed:", result.error);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
isAuthenticated: false,
|
|
41
|
+
user: null,
|
|
42
|
+
error: "No valid session found"
|
|
43
|
+
};
|
|
44
|
+
} catch (error) {
|
|
45
|
+
console.error("Session verification error:", error);
|
|
46
|
+
return {
|
|
47
|
+
isAuthenticated: false,
|
|
48
|
+
user: null,
|
|
49
|
+
error: error instanceof Error ? error.message : "Session verification failed"
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
verifySession
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=edge-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/app-router/server/edge-session.ts"],"sourcesContent":["import { cookies } from \"next/headers\"\nimport { verifyFirebaseToken } from \"./jwt\"\nimport type { NextRequest } from \"next/server\"\n\nexport interface UserInfo {\n uid: string\n email: string | null\n emailVerified?: boolean\n authTime?: number\n disabled?: boolean\n}\n\n\nexport interface SessionResult {\n isAuthenticated: boolean\n user: UserInfo | null\n error?: string\n}\n\nexport async function verifySession(request: NextRequest): Promise<SessionResult> {\n try {\n const cookieStore = await cookies()\n\n // First try session cookie\n const sessionCookie = cookieStore.get(\"_session_cookie\")?.value\n if (sessionCookie) {\n const result = await verifyFirebaseToken(sessionCookie, true)\n if (result.valid) {\n return {\n isAuthenticated: true,\n user: {\n uid: result.uid ?? '',\n email: result.email || null,\n emailVerified: result.emailVerified ?? false,\n disabled: false,\n },\n }\n }\n console.log(\"Session cookie verification failed:\", result.error)\n }\n\n // Then try ID token\n const idToken = cookieStore.get(\"_session_token\")?.value\n if (idToken) {\n const result = await verifyFirebaseToken(idToken, false)\n if (result.valid) {\n return {\n isAuthenticated: true,\n user: {\n uid: result.uid ?? '',\n email: result.email || null,\n emailVerified: result.emailVerified ?? false,\n disabled: false,\n },\n }\n }\n console.log(\"ID token verification failed:\", result.error)\n }\n\n return {\n isAuthenticated: false,\n user: null,\n error: \"No valid session found\",\n }\n } catch (error) {\n console.error(\"Session verification error:\", error)\n return {\n isAuthenticated: false,\n user: null,\n error: error instanceof Error ? error.message : \"Session verification failed\",\n }\n }\n}"],"mappings":"AAAA,SAAS,eAAe;AACxB,SAAS,2BAA2B;AAkBpC,eAAsB,cAAc,SAA8C;AAnBlF;AAoBE,MAAI;AACF,UAAM,cAAc,MAAM,QAAQ;AAGlC,UAAM,iBAAgB,iBAAY,IAAI,iBAAiB,MAAjC,mBAAoC;AAC1D,QAAI,eAAe;AACjB,YAAM,SAAS,MAAM,oBAAoB,eAAe,IAAI;AAC5D,UAAI,OAAO,OAAO;AAChB,eAAO;AAAA,UACL,iBAAiB;AAAA,UACjB,MAAM;AAAA,YACJ,MAAK,YAAO,QAAP,YAAc;AAAA,YACnB,OAAO,OAAO,SAAS;AAAA,YACvB,gBAAe,YAAO,kBAAP,YAAwB;AAAA,YACvC,UAAU;AAAA,UACZ;AAAA,QACF;AAAA,MACF;AACA,cAAQ,IAAI,uCAAuC,OAAO,KAAK;AAAA,IACjE;AAGA,UAAM,WAAU,iBAAY,IAAI,gBAAgB,MAAhC,mBAAmC;AACnD,QAAI,SAAS;AACX,YAAM,SAAS,MAAM,oBAAoB,SAAS,KAAK;AACvD,UAAI,OAAO,OAAO;AAChB,eAAO;AAAA,UACL,iBAAiB;AAAA,UACjB,MAAM;AAAA,YACJ,MAAK,YAAO,QAAP,YAAc;AAAA,YACnB,OAAO,OAAO,SAAS;AAAA,YACvB,gBAAe,YAAO,kBAAP,YAAwB;AAAA,YACvC,UAAU;AAAA,UACZ;AAAA,QACF;AAAA,MACF;AACA,cAAQ,IAAI,iCAAiC,OAAO,KAAK;AAAA,IAC3D;AAEA,WAAO;AAAA,MACL,iBAAiB;AAAA,MACjB,MAAM;AAAA,MACN,OAAO;AAAA,IACT;AAAA,EACF,SAAS,OAAO;AACd,YAAQ,MAAM,+BAA+B,KAAK;AAClD,WAAO;AAAA,MACL,iBAAiB;AAAA,MACjB,MAAM;AAAA,MACN,OAAO,iBAAiB,QAAQ,MAAM,UAAU;AAAA,IAClD;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { adminTernSecureAuth, adminTernSecureDb } from "../../utils/admin-init";
|
|
2
|
-
import { ternSecureMiddleware } from "./ternSecureMiddleware";
|
|
2
|
+
import { ternSecureMiddleware, createRouteMatcher } from "./ternSecureMiddleware";
|
|
3
3
|
import { verifyTernSessionCookie, createSessionCookie } from "./sessionTernSecure";
|
|
4
|
-
import { auth } from "./auth";
|
|
4
|
+
import { auth, getUserInfo } from "./auth";
|
|
5
5
|
export {
|
|
6
6
|
adminTernSecureAuth,
|
|
7
7
|
adminTernSecureDb,
|
|
8
8
|
auth,
|
|
9
|
+
createRouteMatcher,
|
|
9
10
|
createSessionCookie,
|
|
11
|
+
getUserInfo,
|
|
10
12
|
ternSecureMiddleware,
|
|
11
13
|
verifyTernSessionCookie
|
|
12
14
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/app-router/server/index.ts"],"sourcesContent":["export { adminTernSecureAuth, adminTernSecureDb } from '../../utils/admin-init'\nexport { ternSecureMiddleware } from './ternSecureMiddleware'\nexport { verifyTernSessionCookie, createSessionCookie } from './sessionTernSecure'\nexport { auth } from './auth'"],"mappings":"AAAA,SAAS,qBAAqB,yBAAyB;AACvD,SAAS,
|
|
1
|
+
{"version":3,"sources":["../../../../src/app-router/server/index.ts"],"sourcesContent":["export { adminTernSecureAuth, adminTernSecureDb } from '../../utils/admin-init'\nexport { ternSecureMiddleware, createRouteMatcher } from './ternSecureMiddleware'\nexport { verifyTernSessionCookie, createSessionCookie } from './sessionTernSecure'\nexport { auth, getUserInfo } from './auth'\nexport type { AuthResult } from './auth'"],"mappings":"AAAA,SAAS,qBAAqB,yBAAyB;AACvD,SAAS,sBAAsB,0BAA0B;AACzD,SAAS,yBAAyB,2BAA2B;AAC7D,SAAS,MAAM,mBAAmB;","names":[]}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { jwtVerify, createRemoteJWKSet } from "jose";
|
|
2
|
+
import { cache } from "react";
|
|
3
|
+
const FIREBASE_ID_TOKEN_URL = "https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com";
|
|
4
|
+
const FIREBASE_SESSION_CERT_URL = "https://identitytoolkit.googleapis.com/v1/sessionCookiePublicKeys";
|
|
5
|
+
const getIdTokenJWKS = cache(() => {
|
|
6
|
+
return createRemoteJWKSet(new URL(FIREBASE_ID_TOKEN_URL), {
|
|
7
|
+
cacheMaxAge: 36e5,
|
|
8
|
+
// 1 hour
|
|
9
|
+
timeoutDuration: 5e3,
|
|
10
|
+
// 5 seconds
|
|
11
|
+
cooldownDuration: 3e4
|
|
12
|
+
// 30 seconds between retries
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
const getSessionJWKS = cache(() => {
|
|
16
|
+
return createRemoteJWKSet(new URL(FIREBASE_SESSION_CERT_URL), {
|
|
17
|
+
cacheMaxAge: 36e5,
|
|
18
|
+
// 1 hour
|
|
19
|
+
timeoutDuration: 5e3,
|
|
20
|
+
// 5 seconds
|
|
21
|
+
cooldownDuration: 3e4
|
|
22
|
+
// 30 seconds between retries
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
function decodeJwt(token) {
|
|
26
|
+
try {
|
|
27
|
+
const [headerB64, payloadB64] = token.split(".");
|
|
28
|
+
const header = JSON.parse(Buffer.from(headerB64, "base64").toString());
|
|
29
|
+
const payload = JSON.parse(Buffer.from(payloadB64, "base64").toString());
|
|
30
|
+
return { header, payload };
|
|
31
|
+
} catch (error) {
|
|
32
|
+
console.error("Error decoding JWT:", error);
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function verifyFirebaseToken(token, isSessionCookie = false) {
|
|
37
|
+
try {
|
|
38
|
+
const projectId = process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID;
|
|
39
|
+
if (!projectId) {
|
|
40
|
+
throw new Error("Firebase Project ID is not configured");
|
|
41
|
+
}
|
|
42
|
+
const decoded = decodeJwt(token);
|
|
43
|
+
if (!decoded) {
|
|
44
|
+
throw new Error("Invalid token format");
|
|
45
|
+
}
|
|
46
|
+
console.log("Token details:", {
|
|
47
|
+
header: decoded.header,
|
|
48
|
+
type: isSessionCookie ? "session_cookie" : "id_token"
|
|
49
|
+
});
|
|
50
|
+
let retries = 3;
|
|
51
|
+
let lastError = null;
|
|
52
|
+
while (retries > 0) {
|
|
53
|
+
try {
|
|
54
|
+
const JWKS = isSessionCookie ? await getSessionJWKS() : await getIdTokenJWKS();
|
|
55
|
+
const { payload } = await jwtVerify(token, JWKS, {
|
|
56
|
+
issuer: isSessionCookie ? "https://session.firebase.google.com/" + projectId : "https://securetoken.google.com/" + projectId,
|
|
57
|
+
audience: projectId,
|
|
58
|
+
algorithms: ["RS256"]
|
|
59
|
+
});
|
|
60
|
+
const firebasePayload = payload;
|
|
61
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
62
|
+
if (firebasePayload.exp <= now) {
|
|
63
|
+
throw new Error("Token has expired");
|
|
64
|
+
}
|
|
65
|
+
if (firebasePayload.iat > now) {
|
|
66
|
+
throw new Error("Token issued time is in the future");
|
|
67
|
+
}
|
|
68
|
+
if (!firebasePayload.sub) {
|
|
69
|
+
throw new Error("Token subject is empty");
|
|
70
|
+
}
|
|
71
|
+
if (firebasePayload.auth_time > now) {
|
|
72
|
+
throw new Error("Token auth time is in the future");
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
valid: true,
|
|
76
|
+
uid: firebasePayload.sub,
|
|
77
|
+
email: firebasePayload.email,
|
|
78
|
+
emailVerified: firebasePayload.email_verified,
|
|
79
|
+
authTime: firebasePayload.auth_time,
|
|
80
|
+
issuedAt: firebasePayload.iat,
|
|
81
|
+
expiresAt: firebasePayload.exp
|
|
82
|
+
};
|
|
83
|
+
} catch (error) {
|
|
84
|
+
lastError = error;
|
|
85
|
+
if (error instanceof Error && error.name === "JWKSNoMatchingKey") {
|
|
86
|
+
console.warn(`JWKS retry attempt ${4 - retries}:`, error.message);
|
|
87
|
+
retries--;
|
|
88
|
+
if (retries > 0) {
|
|
89
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
throw lastError || new Error("Failed to verify token after retries");
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error("Token verification details:", {
|
|
99
|
+
error: error instanceof Error ? {
|
|
100
|
+
name: error.name,
|
|
101
|
+
message: error.message,
|
|
102
|
+
stack: error.stack
|
|
103
|
+
} : error,
|
|
104
|
+
decoded: decodeJwt(token),
|
|
105
|
+
//projectId,
|
|
106
|
+
isSessionCookie
|
|
107
|
+
});
|
|
108
|
+
return {
|
|
109
|
+
valid: false,
|
|
110
|
+
error: error instanceof Error ? error.message : "Invalid token"
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
export {
|
|
115
|
+
verifyFirebaseToken
|
|
116
|
+
};
|
|
117
|
+
//# sourceMappingURL=jwt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/app-router/server/jwt.ts"],"sourcesContent":["import { jwtVerify, createRemoteJWKSet } from \"jose\"\nimport { cache } from \"react\"\n\ninterface FirebaseIdTokenPayload {\n iss: string\n aud: string\n auth_time: number\n user_id: string\n sub: string\n iat: number\n exp: number\n email?: string\n email_verified?: boolean\n firebase: {\n identities: {\n [key: string]: any\n }\n sign_in_provider: string\n }\n}\n\n// Firebase public key endpoints\nconst FIREBASE_ID_TOKEN_URL = \"https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com\"\nconst FIREBASE_SESSION_CERT_URL = \"https://identitytoolkit.googleapis.com/v1/sessionCookiePublicKeys\"\n\n// Cache the JWKS using React cache\nconst getIdTokenJWKS = cache(() => {\n return createRemoteJWKSet(new URL(FIREBASE_ID_TOKEN_URL), {\n cacheMaxAge: 3600000, // 1 hour\n timeoutDuration: 5000, // 5 seconds\n cooldownDuration: 30000, // 30 seconds between retries\n })\n})\n\nconst getSessionJWKS = cache(() => {\n return createRemoteJWKSet(new URL(FIREBASE_SESSION_CERT_URL), {\n cacheMaxAge: 3600000, // 1 hour\n timeoutDuration: 5000, // 5 seconds\n cooldownDuration: 30000, // 30 seconds between retries\n })\n})\n\n// Helper to decode JWT without verification\nfunction decodeJwt(token: string) {\n try {\n const [headerB64, payloadB64] = token.split(\".\")\n const header = JSON.parse(Buffer.from(headerB64, \"base64\").toString())\n const payload = JSON.parse(Buffer.from(payloadB64, \"base64\").toString())\n return { header, payload }\n } catch (error) {\n console.error(\"Error decoding JWT:\", error)\n return null\n }\n}\n\nexport async function verifyFirebaseToken(token: string, isSessionCookie = false) {\n try {\n const projectId = process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID\n if (!projectId) {\n throw new Error(\"Firebase Project ID is not configured\")\n }\n\n // Decode token for debugging and type checking\n const decoded = decodeJwt(token)\n if (!decoded) {\n throw new Error(\"Invalid token format\")\n }\n\n console.log(\"Token details:\", {\n header: decoded.header,\n type: isSessionCookie ? \"session_cookie\" : \"id_token\",\n })\n\n let retries = 3\n let lastError: Error | null = null\n\n while (retries > 0) {\n try {\n // Use different JWKS based on token type\n const JWKS = isSessionCookie ? await getSessionJWKS() : await getIdTokenJWKS()\n\n const { payload } = await jwtVerify(token, JWKS, {\n issuer: isSessionCookie\n ? \"https://session.firebase.google.com/\" + projectId\n : \"https://securetoken.google.com/\" + projectId,\n audience: projectId,\n algorithms: [\"RS256\"],\n })\n\n const firebasePayload = payload as unknown as FirebaseIdTokenPayload\n const now = Math.floor(Date.now() / 1000)\n\n // Verify token claims\n if (firebasePayload.exp <= now) {\n throw new Error(\"Token has expired\")\n }\n\n if (firebasePayload.iat > now) {\n throw new Error(\"Token issued time is in the future\")\n }\n\n if (!firebasePayload.sub) {\n throw new Error(\"Token subject is empty\")\n }\n\n if (firebasePayload.auth_time > now) {\n throw new Error(\"Token auth time is in the future\")\n }\n\n return {\n valid: true,\n uid: firebasePayload.sub,\n email: firebasePayload.email,\n emailVerified: firebasePayload.email_verified,\n authTime: firebasePayload.auth_time,\n issuedAt: firebasePayload.iat,\n expiresAt: firebasePayload.exp,\n }\n } catch (error) {\n lastError = error as Error\n if (error instanceof Error && error.name === \"JWKSNoMatchingKey\") {\n console.warn(`JWKS retry attempt ${4 - retries}:`, error.message)\n retries--\n if (retries > 0) {\n await new Promise((resolve) => setTimeout(resolve, 1000))\n continue\n }\n }\n throw error\n }\n }\n\n throw lastError || new Error(\"Failed to verify token after retries\")\n } catch (error) {\n console.error(\"Token verification details:\", {\n error:\n error instanceof Error\n ? {\n name: error.name,\n message: error.message,\n stack: error.stack,\n }\n : error,\n decoded: decodeJwt(token),\n //projectId,\n isSessionCookie,\n })\n\n return {\n valid: false,\n error: error instanceof Error ? error.message : \"Invalid token\",\n }\n }\n}\n\n"],"mappings":"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","names":[]}
|
|
@@ -54,15 +54,20 @@ async function getIdToken() {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
async function setServerSession(token) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
try {
|
|
58
|
+
const cookieStore = await cookies();
|
|
59
|
+
cookieStore.set("_session_token", token, {
|
|
60
|
+
httpOnly: true,
|
|
61
|
+
secure: process.env.NODE_ENV === "production",
|
|
62
|
+
sameSite: "strict",
|
|
63
|
+
maxAge: 60 * 60,
|
|
64
|
+
// 1 hour
|
|
65
|
+
path: "/"
|
|
66
|
+
});
|
|
67
|
+
return { success: true, message: "Session created" };
|
|
68
|
+
} catch {
|
|
69
|
+
return { success: false, message: "Failed to create session" };
|
|
70
|
+
}
|
|
66
71
|
}
|
|
67
72
|
async function verifyTernIdToken(token) {
|
|
68
73
|
try {
|