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

fix: Fixes a server client boundary error #3917

Merged
merged 6 commits into from
Jul 3, 2024
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import React, { ReactElement, useRef, useState } from "react";
import React, { ReactElement, useEffect, useRef, useState } from "react";
import { useRouter } from "next/navigation";
import { useTranslation } from "@i18n/client";
import { Button } from "@clientComponents/globals";
Expand All @@ -11,6 +11,8 @@ import { ErrorStatus } from "@clientComponents/forms/Alert/Alert";
import Link from "next/link";

import { useFocusIt } from "@lib/hooks/useFocusIt";
import { safeJSONParse } from "@lib/utils";
import { logMessage } from "@lib/logger";

export const ReVerify = (): ReactElement => {
const router = useRouter();
Expand All @@ -25,20 +27,36 @@ export const ReVerify = (): ReactElement => {
const [authErrorState, setAuthErrorState] = useState<Record<string, string | undefined>>({});
const [resending, setResending] = useState(false);

// If there is no existing flow redirect to login
const { email, authenticationFlowToken }: { email?: string; authenticationFlowToken?: string } =
JSON.parse(sessionStorage.getItem("authFlowToken") || "{}");
if (!email || !authenticationFlowToken) {
router.push(`/${language}/auth/login`);
}
// Makes sure NextJS will not try to render sessionStorage on the server
const existingFlow = useRef<{ email?: string; authenticationFlowToken?: string; error?: string }>(
{}
);
useEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wanted to use the new React hook use() but ran into some errors and an open bug. I wasn't able to find a work around so I sadly went with the old style.
For reference:
facebook/react#26930
and then this
vercel/next.js#51477

async function handleError() {
logMessage.error("Failed to parse authFlowToken JSON.");
const errorText = await getErrorText(language, "InternalServiceException");
setAuthErrorState(errorText);
}
existingFlow.current = safeJSONParse(sessionStorage.getItem("authFlowToken") || "{}");
if (!existingFlow.current.email || !existingFlow.current.authenticationFlowToken) {
router.push(`/${language}/auth/login`);
}
if (!existingFlow.current.error) {
handleError();
}
}, [language, router]);

const handleReVerify = async () => {
setResending(true);
if (!email || !authenticationFlowToken) {
if (!existingFlow.current.email || !existingFlow.current.authenticationFlowToken) {
router.push(`/${language}/auth/login`);
return;
}
const result = await resendVerificationCode(language, email, authenticationFlowToken);
const result = await resendVerificationCode(
language,
existingFlow.current.email,
existingFlow.current.authenticationFlowToken
);

if (result?.error) {
// Internal Error
Expand Down
Loading