Skip to content

Commit

Permalink
Simplify, no router no state
Browse files Browse the repository at this point in the history
jamesdaniels committed Oct 3, 2024
1 parent 509ec26 commit 5bc7c73
Showing 2 changed files with 6 additions and 15 deletions.
18 changes: 5 additions & 13 deletions nextjs-end/src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
"use client";
import React, { useState, useEffect } from "react";
import React, { 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";
import { firebaseConfig } from "@/src/lib/firebase/config";

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

function useUserSession(user) {
// Register the service worker that sends auth state back to server
// The service worker is built with npm run build-service-worker
useEffect(() => {
@@ -40,14 +35,11 @@ function useUserSession(initialUser) {
return;
}
await navigator.serviceWorker.ready;
await fetch(`/__/auth/wait/${authUser?.uid}`, { method: "HEAD" }).catch(
() => undefined
);
setUser(authUser);
router.refresh();
await fetch(`/__/auth/wait/${authUser?.uid}`);
window.location.reload();
});
}
}, [user, router]);
}, [user]);

return user;
}
3 changes: 1 addition & 2 deletions nextjs-start/src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"use client";
import React, { useState, useEffect } from "react";
import React, { 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";
import { firebaseConfig } from "@/src/lib/firebase/config";

function useUserSession(initialUser) {

0 comments on commit 5bc7c73

Please sign in to comment.