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 #249

Merged
merged 13 commits into from
Sep 15, 2023
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
24 changes: 0 additions & 24 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 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