Skip to content
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

Ssr update #250

Merged
merged 15 commits into from
Sep 15, 2023
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
7 changes: 6 additions & 1 deletion nextjs-end/src/app/actions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
"use server";

import { addReviewToRestaurant } from "@/src/lib/firebase/firestore.js";
import { getAuthenticatedAppForUser } from "@/src/lib/firebase/firebase";
import { getFirestore } from "firebase/firestore";

// This is a next.js server action, an alpha feature, so
// use with caution
// https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
export async function handleReviewFormSubmission(data) {
await addReviewToRestaurant(data.get("restaurantId"), {
const { app } = await getAuthenticatedAppForUser();
const db = getFirestore(app);

await addReviewToRestaurant(db, data.get("restaurantId"), {
text: data.get("text"),
rating: data.get("rating"),

Expand Down
2 changes: 1 addition & 1 deletion nextjs-end/src/app/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default async function RootLayout({ children }) {
<html lang="en">

<body>
<Header initialUser={currentUser.toJSON()}/>
<Header initialUser={currentUser?.toJSON()}/>

<main>{children}</main>
</body>
Expand Down
34 changes: 32 additions & 2 deletions nextjs-end/src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
'use client'
import React from "react";
import React, { useState, useEffect } from "react";
import Link from "next/link";
import {
signInWithGoogle,
signOut,
onAuthStateChanged
} from "@/src/lib/firebase/auth.js";
import { addFakeRestaurantsAndReviews } from "@/src/lib/firebase/firestore.js";
import { useRouter } from "next/navigation";

function useUserSession(initialUser) {
// The initialUser comes from the server via a server component
const [user, setUser] = useState(initialUser);
const router = useRouter()

useEffect(() => {
const unsubscribe = onAuthStateChanged((authUser) => {
setUser(authUser)
})

return () => unsubscribe()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

useEffect(() => {
onAuthStateChanged((authUser) => {
if (user === undefined) return

// refresh when user changed to ease testing
if (user?.email !== authUser?.email) {
router.refresh()
}
})
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [user])

return user;
}

export default function Header({initialUser}) {

const user = initialUser ;
const user = useUserSession(initialUser) ;

const handleSignOut = event => {
event.preventDefault();
Expand Down
6 changes: 4 additions & 2 deletions nextjs-end/src/lib/firebase/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
GoogleAuthProvider,
signInWithPopup,
onAuthStateChanged as _onAuthStateChanged,
} from "firebase/auth";

import { auth } from "@/src/lib/firebase/firebase";



export function onAuthStateChanged(cb) {
return _onAuthStateChanged(auth, cb);
}

export async function signInWithGoogle() {
const provider = new GoogleAuthProvider();
Expand Down
42 changes: 11 additions & 31 deletions nextjs-end/src/lib/firebase/firebase.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
// import firebaseConfig from "@/src/lib/firebase/config.js";
// import { initializeApp } from "firebase/app";

// import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";

// import { getAuth, connectAuthEmulator } from "firebase/auth";

// import { getStorage, connectStorageEmulator } from "firebase/storage";

// import { getApps } from "firebase/app";

// let app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];

// export const db = getFirestore(app);
// export const storage = getStorage(app);
// export const auth = getAuth(app);

// // For development purposes only
// connectFirestoreEmulator(db, "127.0.0.1", 8080);
// connectStorageEmulator(storage, "127.0.0.1", 9199);
// connectAuthEmulator(auth, "http://127.0.0.1:9099", {
// disableWarnings: true,
// });

import { initializeApp, getApps } from "firebase/app";
import {
Expand All @@ -30,7 +7,6 @@ import {
} from "firebase/auth";
import { getFirestore, connectFirestoreEmulator } from "firebase/firestore";
import { getStorage, connectStorageEmulator } from "firebase/storage";
import dynamic from "next/dynamic";

export const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
Expand All @@ -48,11 +24,11 @@ export const db = getFirestore(firebaseApp);
export const storage = getStorage(firebaseApp);

// For development purposes only
connectFirestoreEmulator(db, "127.0.0.1", 8080);
connectStorageEmulator(storage, "127.0.0.1", 9199);
connectAuthEmulator(auth, "http://127.0.0.1:9099", {
disableWarnings: true,
});
// connectFirestoreEmulator(db, "127.0.0.1", 8080);
// connectStorageEmulator(storage, "127.0.0.1", 9199);
// connectAuthEmulator(auth, "http://127.0.0.1:9099", {
// disableWarnings: true,
// });

export async function getAuthenticatedAppForUser(session = null) {

Expand All @@ -63,15 +39,19 @@ export async function getAuthenticatedAppForUser(session = null) {

return { app: firebaseApp, user: auth.currentUser.toJSON() };
}
// import {initializeApp as initializeAdminApp, getApps as getAdminApps} from "firebase-admin/app";

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({}, ADMIN_APP_NAME);
initializeAdminApp({
credential: credential.applicationDefault(),
}, ADMIN_APP_NAME);

const adminAuth = getAdminAuth(adminApp);
const noSessionReturn = { app: null, currentUser: null };
Expand Down
2 changes: 1 addition & 1 deletion nextjs-end/src/lib/firebase/firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const updateWithRating = async (
});
};

export async function addReviewToRestaurant(restaurantId, review) {
export async function addReviewToRestaurant(db, restaurantId, review) {
if (!restaurantId) {
throw new Error("No restaurant ID has been provided.");
}
Expand Down
3 changes: 2 additions & 1 deletion nextjs-end/storage.rules
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if false;
allow read;
allow write: if request.auth.uid != null;
}
}
}
Loading