Skip to content

Commit 7e75f48

Browse files
authored
Merge pull request #84 from TernSecure/kayp/remove-firebaseserverapp
feat: update auth handling to remove FirebaseServerApp functionality
2 parents e899563 + fce31a7 commit 7e75f48

6 files changed

Lines changed: 26 additions & 24 deletions

File tree

.changeset/clever-cats-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tern-secure/nextjs': patch
3+
---
4+
5+
feat: update auth handling to remove FirebaseServerApp functionality

apps/test/app/protected/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { ProtectedPageClient } from './protectedClient';
44
export const dynamic = 'force-dynamic';
55

66
export default async function ProtectedPage() {
7-
const { user, require, redirectToSignIn } = await auth();
7+
const { sessionClaims, require, redirectToSignIn } = await auth();
88
if (!require({ role: 'admin' })) return <div>Access Denied now</div>;
99

10-
if (!user) return redirectToSignIn();
10+
if (!sessionClaims?.aud) return redirectToSignIn();
1111

12-
return <ProtectedPageClient user={user} />;
12+
return <ProtectedPageClient user={sessionClaims} />;
1313
}

apps/test/app/protected/protectedClient.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"use client";
22

33
import { useRouter } from "next/navigation";
4-
import type { TernSecureUser } from "@tern-secure/nextjs";
4+
import type { DecodedIdToken } from "@tern-secure/nextjs";
55

6-
export type SerializableTernSecureUser = Omit<TernSecureUser, 'delete' | 'getIdToken' | 'getIdTokenResult' | 'reload' | 'toJSON'>;
76

87
interface ProtectedPageClientProps {
9-
user: SerializableTernSecureUser;
8+
user: DecodedIdToken;
109
}
1110

1211
export function ProtectedPageClient({

packages/nextjs/src/app-router/server/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createRedirect, createTernSecureRequest } from '@tern-secure/backend';
33
import { notFound, redirect } from 'next/navigation';
44

55
import { SIGN_IN_URL, SIGN_UP_URL } from '../../server/constant';
6-
import { type Aobj, getAuthDataFromRequest } from '../../server/data/getAuthDataFromRequest';
6+
import { getAuthDataFromRequest } from '../../server/data/getAuthDataFromRequest';
77
import { getAuthKeyFromRequest } from '../../server/headers-utils';
88
import { type AuthProtect, createProtect } from '../../server/protect';
99
import type { BaseUser, RequestLike } from '../../server/types';
@@ -20,7 +20,7 @@ export interface AuthResult {
2020
/**
2121
* `Auth` object of the currently active user and the `redirectToSignIn()` method.
2222
*/
23-
type Auth = AuthObject & Aobj & {
23+
type Auth = AuthObject & {
2424
redirectToSignIn: RedirectFun<ReturnType<typeof redirect>>;
2525
redirectToSignUp: RedirectFun<ReturnType<typeof redirect>>;
2626
};

packages/nextjs/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export {
1515
} from './components/uiComponents'
1616

1717
export type {
18+
DecodedIdToken,
1819
TernSecureUser,
1920
SignInResponse,
2021
SignUpResponse,

packages/nextjs/src/server/data/getAuthDataFromRequest.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,38 +91,35 @@ export async function getTernSecureAuthData(
9191
return authObjectToSerializable({ ...initialState, ...authObject });
9292
}
9393

94-
export async function getAuthDataFromRequest(req: RequestLike): Promise<AuthObject & Aobj> {
94+
95+
/**
96+
* Given the issue ( https://github.com/firebase/firebase-js-sdk/issues/9423 ) that affects the authenticateRequest function,
97+
* change from Promise<AuthObject & Aobj> to Promise<AuthObject> only. no firebaseserverapp user object needed.
98+
* @param req
99+
* @returns
100+
*/
101+
export async function getAuthDataFromRequest(req: RequestLike): Promise<AuthObject> {
95102
const authStatus = getAuthKeyFromRequest(req, "AuthStatus");
96103
const authToken = getAuthKeyFromRequest(req, "AuthToken");
97104

98105
if (!authStatus || authStatus !== AuthStatus.SignedIn) {
99106
return {
100107
...signedOutAuthObject(),
101-
user: null,
102-
userId: null
103108
}
104109
}
105110

106-
const firebaseUser = await authenticateRequest(
107-
authToken as string,
108-
req as any
109-
);
110-
if (!firebaseUser || !firebaseUser.claims) {
111-
return {
112-
...signedOutAuthObject(),
113-
user: null,
114-
userId: null
115-
}
116-
}
117-
const { user } = firebaseUser;
118111
const jwt = ternDecodeJwt(authToken as string);
119112
const authObject = signedInAuthObject(authToken as string, jwt.payload);
120113
return {
121114
...authObject,
122-
user: user || null,
123115
};
124116
}
125117

118+
/***
119+
* InitializeServerApp seems to have issue with Refer header. firebase doesnt have a fix yet.
120+
* see link https://github.com/firebase/firebase-js-sdk/issues/9423
121+
* we might need to use this feature in the future when firebase fix this issue.
122+
*/
126123
const authenticateRequest = async (
127124
token: string,
128125
request: Request,

0 commit comments

Comments
 (0)