Check setup step and redirect to /setup if needed

This commit is contained in:
Zoe Roux 2024-05-26 15:49:28 +02:00
parent cdf482ac4a
commit d178b78e86
No known key found for this signature in database

View File

@ -34,6 +34,8 @@ import {
getTokenWJ, getTokenWJ,
setSsrApiUrl, setSsrApiUrl,
useUserTheme, useUserTheme,
SetupStep,
ServerInfo,
} from "@kyoo/models"; } from "@kyoo/models";
import { getCurrentAccount, readCookie, updateAccount } from "@kyoo/models/src/account-internal"; import { getCurrentAccount, readCookie, updateAccount } from "@kyoo/models/src/account-internal";
import { import {
@ -56,6 +58,8 @@ import { Tooltip } from "react-tooltip";
import superjson from "superjson"; import superjson from "superjson";
import { StyleRegistryProvider, useMobileHover, useStyleRegistry, useTheme } from "yoshiki/web"; import { StyleRegistryProvider, useMobileHover, useStyleRegistry, useTheme } from "yoshiki/web";
import { withTranslations } from "../i18n"; import { withTranslations } from "../i18n";
import { redirect } from "next/navigation";
import { NextResponse } from "next/server";
const font = Poppins({ weight: ["300", "400", "900"], subsets: ["latin"], display: "swap" }); const font = Poppins({ weight: ["300", "400", "900"], subsets: ["latin"], display: "swap" });
@ -214,22 +218,36 @@ App.getInitialProps = async (ctx: AppContext) => {
setSsrApiUrl(); setSsrApiUrl();
appProps.pageProps.theme = readCookie(ctx.ctx.req?.headers.cookie, "theme") ?? "auto";
const account = readCookie(ctx.ctx.req?.headers.cookie, "account", AccountP); const account = readCookie(ctx.ctx.req?.headers.cookie, "account", AccountP);
if (account) urls.push({ path: ["auth", "me"], parser: UserP }); if (account) urls.push({ path: ["auth", "me"], parser: UserP });
const [authToken, token, error] = await getTokenWJ(account); const [authToken, token, error] = await getTokenWJ(account);
if (error) appProps.pageProps.ssrError = error; if (error) appProps.pageProps.ssrError = error;
else { const client = (await fetchQuery(urls, authToken))!;
const client = (await fetchQuery(urls, authToken))!; appProps.pageProps.queryState = dehydrate(client);
appProps.pageProps.queryState = dehydrate(client); if (account) {
if (account) { appProps.pageProps.token = token;
appProps.pageProps.token = token; appProps.pageProps.account = {
appProps.pageProps.account = { ...client.getQueryData(["auth", "me"]),
...client.getQueryData(["auth", "me"]), ...account,
...account, };
}; }
}
const info = client.getQueryData<ServerInfo>(["info"]);
if (
info!.setupStatus !== SetupStep.Done &&
!(
(ctx.router.route === "/setup" && ctx.router.query.step === info!.setupStatus) ||
ctx.router.route === "/register" ||
ctx.router.route === "/login" ||
ctx.router.route === "/settings"
)
) {
ctx.ctx.res!.writeHead(307, { Location: `/setup?step=${info!.setupStatus}` });
ctx.ctx.res!.end();
return {} as any;
} }
appProps.pageProps.theme = readCookie(ctx.ctx.req?.headers.cookie, "theme") ?? "auto";
} catch (e) { } catch (e) {
console.error("SSR error, disabling it."); console.error("SSR error, disabling it.");
} }