-
Notifications
You must be signed in to change notification settings - Fork 428
Fix import issue with Next.js 14 #272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
experimental: { | ||
serverActions: true, | ||
} | ||
}; | ||
|
||
module.exports = nextConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { initializeApp } from "firebase/app"; | ||
import { | ||
getAuth, | ||
signInWithCustomToken, | ||
} from "firebase/auth"; | ||
import { auth as firebaseAuth, firebaseApp, firebaseConfig } from "@/src/lib/firebase/firebase.js"; | ||
|
||
export async function getAuthenticatedAppForUser(session = null) { | ||
if (typeof window !== 'undefined') { | ||
// Running on the client | ||
console.log("client: ", firebaseApp); | ||
return { app: firebaseApp, user: firebaseAuth.currentUser.toJSON() }; | ||
} | ||
|
||
const { initializeApp: initializeAdminApp, getApps: getAdminApps } = await import("firebase-admin/app"); | ||
const { getAuth: getAdminAuth } = await import("firebase-admin/auth"); | ||
const { credential } = await import("firebase-admin"); | ||
|
||
const ADMIN_APP_NAME = "firebase-frameworks"; | ||
const adminApp = | ||
getAdminApps().find((it) => it.name === ADMIN_APP_NAME) || | ||
initializeAdminApp({ | ||
credential: credential.applicationDefault(), | ||
}, ADMIN_APP_NAME); | ||
|
||
const adminAuth = getAdminAuth(adminApp); | ||
const noSessionReturn = { app: null, currentUser: null }; | ||
|
||
if (!session) { | ||
// if no session cookie was passed, try to get from next/headers for app router | ||
session = await getAppRouterSession(); | ||
if (!session) return noSessionReturn; | ||
} | ||
|
||
const decodedIdToken = await adminAuth.verifySessionCookie(session); | ||
const app = initializeAuthenticatedApp(decodedIdToken.uid) | ||
const auth = getAuth(app) | ||
|
||
// handle revoked tokens | ||
const isRevoked = !(await adminAuth | ||
.verifySessionCookie(session, true) | ||
.catch((e) => console.error(e.message))); | ||
if (isRevoked) return noSessionReturn; | ||
|
||
// authenticate with custom token | ||
if (auth.currentUser?.uid !== decodedIdToken.uid) { | ||
// TODO(jamesdaniels) get custom claims | ||
const customToken = await adminAuth | ||
.createCustomToken(decodedIdToken.uid) | ||
.catch((e) => console.error(e.message)); | ||
|
||
if (!customToken) return noSessionReturn; | ||
await signInWithCustomToken(auth, customToken); | ||
} | ||
|
||
console.log("server: ", app); | ||
return { app, currentUser: auth.currentUser }; | ||
} | ||
|
||
async function getAppRouterSession() { | ||
// dynamically import to prevent import errors in pages router | ||
const { cookies } = await import("next/headers"); | ||
|
||
try { | ||
return cookies().get("__session")?.value; | ||
} catch (error) { | ||
// cookies() throws when called from pages router | ||
return undefined; | ||
} | ||
} | ||
|
||
function initializeAuthenticatedApp(uid) { | ||
const random = Math.random().toString(36).split(".")[1]; | ||
const appName = `authenticated-context:${uid}:${random}`; | ||
const app = initializeApp(firebaseConfig, appName); | ||
return app; | ||
} |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.