Last active
December 11, 2024 02:01
-
-
Save jamesdaniels/5f6ccbd0a359a8f1b0c12d2a6dd0a4de to your computer and use it in GitHub Desktop.
This file contains 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
import { getServerAuth } from "@/firebase/server"; | |
import FirebaseClientCookieProvider from "./cookie"; | |
import Image from "next/image"; | |
import SignOutButton from "../sign-out-button"; | |
import SignInButton from "../sign-in-button"; | |
export default async function FirebaseAuthWidget() { | |
const auth = await getServerAuth(); | |
const currentUser = auth.currentUser; | |
return ( | |
<> | |
{ currentUser ? | |
<> | |
<Image src={ currentUser.photoURL! } width="48" height="48" alt=""></Image> | |
<p>{ currentUser.displayName }</p> | |
<SignOutButton></SignOutButton> | |
</> : | |
<SignInButton></SignInButton> } | |
<FirebaseClientCookieProvider serverUid={currentUser?.uid}></FirebaseClientCookieProvider> | |
</> | |
) | |
} |
This file contains 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
import "server-only"; | |
import { FirebaseServerApp, initializeServerApp } from 'firebase/app'; | |
import { getAuth } from "firebase/auth"; | |
import { FIREBASE_OPTIONS, COOKIE_NAME } from "./constants"; | |
import { cookies, headers } from "next/headers"; | |
async function getServerApp() { | |
const authIdToken = (await cookies()).get(COOKIE_NAME)?.value; | |
return initializeServerApp(FIREBASE_OPTIONS, { authIdToken }); | |
} | |
export async function getServerAuth(app?: FirebaseServerApp) { | |
const serverApp = app || await getServerApp(); | |
const serverAuth = getAuth(serverApp); | |
await serverAuth.authStateReady(); | |
return serverAuth; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment