4466ba436b
## Description This PR ensures that the default prettier config is used for examples and templates. This config is compatible with `prettier@3` as well (upgrading prettier is bigger change that can be a future PR). ## Changes - Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will be needed upgrading to prettier@3) - Added `examples/.prettierrc.json` with default config (this will change every example) - Added `packages/create-next-app/templates/.prettierrc.json` with default config (this will change every template) ## Related - Fixes #54402 - Closes #54409
31 lines
883 B
JavaScript
31 lines
883 B
JavaScript
import { useEffect } from "react";
|
|
import Router from "next/router";
|
|
import useSWR from "swr";
|
|
|
|
const fetcher = (url) =>
|
|
fetch(url)
|
|
.then((r) => r.json())
|
|
.then((data) => {
|
|
return { user: data?.user || null };
|
|
});
|
|
|
|
export function useUser({ redirectTo, redirectIfFound } = {}) {
|
|
const { data, error } = useSWR("/api/user", fetcher);
|
|
const user = data?.user;
|
|
const finished = Boolean(data);
|
|
const hasUser = Boolean(user);
|
|
|
|
useEffect(() => {
|
|
if (!redirectTo || !finished) return;
|
|
if (
|
|
// If redirectTo is set, redirect if the user was not found.
|
|
(redirectTo && !redirectIfFound && !hasUser) ||
|
|
// If redirectIfFound is also set, redirect if the user was found
|
|
(redirectIfFound && hasUser)
|
|
) {
|
|
Router.push(redirectTo);
|
|
}
|
|
}, [redirectTo, redirectIfFound, finished, hasUser]);
|
|
|
|
return error ? null : user;
|
|
}
|