Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-cats-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tern-secure/nextjs': patch
---

feat: update auth handling to remove FirebaseServerApp functionality
6 changes: 3 additions & 3 deletions apps/test/app/protected/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { ProtectedPageClient } from './protectedClient';
export const dynamic = 'force-dynamic';

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

if (!user) return redirectToSignIn();
if (!sessionClaims?.aud) return redirectToSignIn();

return <ProtectedPageClient user={user} />;
return <ProtectedPageClient user={sessionClaims} />;
}
5 changes: 2 additions & 3 deletions apps/test/app/protected/protectedClient.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"use client";

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

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

interface ProtectedPageClientProps {
user: SerializableTernSecureUser;
user: DecodedIdToken;
}

export function ProtectedPageClient({
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/app-router/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createRedirect, createTernSecureRequest } from '@tern-secure/backend';
import { notFound, redirect } from 'next/navigation';

import { SIGN_IN_URL, SIGN_UP_URL } from '../../server/constant';
import { type Aobj, getAuthDataFromRequest } from '../../server/data/getAuthDataFromRequest';
import { getAuthDataFromRequest } from '../../server/data/getAuthDataFromRequest';
import { getAuthKeyFromRequest } from '../../server/headers-utils';
import { type AuthProtect, createProtect } from '../../server/protect';
import type { BaseUser, RequestLike } from '../../server/types';
Expand All @@ -20,7 +20,7 @@ export interface AuthResult {
/**
* `Auth` object of the currently active user and the `redirectToSignIn()` method.
*/
type Auth = AuthObject & Aobj & {
type Auth = AuthObject & {
redirectToSignIn: RedirectFun<ReturnType<typeof redirect>>;
redirectToSignUp: RedirectFun<ReturnType<typeof redirect>>;
};
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
} from './components/uiComponents'

export type {
DecodedIdToken,
TernSecureUser,
SignInResponse,
SignUpResponse,
Expand Down
29 changes: 13 additions & 16 deletions packages/nextjs/src/server/data/getAuthDataFromRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,38 +91,35 @@ export async function getTernSecureAuthData(
return authObjectToSerializable({ ...initialState, ...authObject });
}

export async function getAuthDataFromRequest(req: RequestLike): Promise<AuthObject & Aobj> {

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

if (!authStatus || authStatus !== AuthStatus.SignedIn) {
return {
...signedOutAuthObject(),
user: null,
userId: null
}
}

const firebaseUser = await authenticateRequest(
authToken as string,
req as any
);
if (!firebaseUser || !firebaseUser.claims) {
return {
...signedOutAuthObject(),
user: null,
userId: null
}
}
const { user } = firebaseUser;
const jwt = ternDecodeJwt(authToken as string);
const authObject = signedInAuthObject(authToken as string, jwt.payload);
return {
...authObject,
user: user || null,
};
}

/***
* InitializeServerApp seems to have issue with Refer header. firebase doesnt have a fix yet.
* see link https://github.com/firebase/firebase-js-sdk/issues/9423
* we might need to use this feature in the future when firebase fix this issue.
*/
const authenticateRequest = async (
token: string,
request: Request,
Expand Down
Loading