Skip to content

Commit f3c95fa

Browse files
authored
fix i18n flicker on auth pages (#1183)
* fix flicker on `/auth/login` * fix flicker on logout * fix flicker on error * fix i18n flicker on signup
1 parent 669b779 commit f3c95fa

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

pages/auth/error.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { XIcon } from "@heroicons/react/outline";
2+
import { GetServerSidePropsContext } from "next";
23
import Link from "next/link";
34
import { useRouter } from "next/router";
45

56
import { useLocale } from "@lib/hooks/useLocale";
67

78
import { HeadSeo } from "@components/seo/head-seo";
89

10+
import { ssrInit } from "@server/lib/ssr";
11+
912
export default function Error() {
1013
const { t } = useLocale();
1114
const router = useRouter();
@@ -48,3 +51,13 @@ export default function Error() {
4851
</div>
4952
);
5053
}
54+
55+
export async function getServerSideProps(context: GetServerSidePropsContext) {
56+
const ssr = await ssrInit(context);
57+
58+
return {
59+
props: {
60+
trpcState: ssr.dehydrate(),
61+
},
62+
};
63+
}

pages/auth/login.tsx

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
import { GetServerSidePropsContext } from "next";
12
import { getCsrfToken, signIn } from "next-auth/client";
23
import Link from "next/link";
34
import { useRouter } from "next/router";
45
import { useState } from "react";
56

67
import { ErrorCode, getSession } from "@lib/auth";
78
import { useLocale } from "@lib/hooks/useLocale";
9+
import { inferSSRProps } from "@lib/types/inferSSRProps";
810

911
import AddToHomescreen from "@components/AddToHomescreen";
1012
import Loader from "@components/Loader";
1113
import { HeadSeo } from "@components/seo/head-seo";
1214

13-
export default function Login({ csrfToken }) {
15+
import { ssrInit } from "@server/lib/ssr";
16+
17+
export default function Login({ csrfToken }: inferSSRProps<typeof getServerSideProps>) {
1418
const { t } = useLocale();
1519
const router = useRouter();
1620
const [email, setEmail] = useState("");
@@ -90,7 +94,7 @@ export default function Login({ csrfToken }) {
9094
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
9195
<div className="bg-white py-8 px-4 mx-2 rounded-sm sm:px-10 border border-neutral-200">
9296
<form className="space-y-6" onSubmit={handleSubmit}>
93-
<input name="csrfToken" type="hidden" defaultValue={csrfToken} hidden />
97+
<input name="csrfToken" type="hidden" defaultValue={csrfToken || undefined} hidden />
9498
<div>
9599
<label htmlFor="email" className="block text-sm font-medium text-neutral-700">
96100
{t("email_address")}
@@ -185,17 +189,24 @@ export default function Login({ csrfToken }) {
185189
);
186190
}
187191

188-
Login.getInitialProps = async (context) => {
189-
const { req, res } = context;
192+
export async function getServerSideProps(context: GetServerSidePropsContext) {
193+
const { req } = context;
190194
const session = await getSession({ req });
195+
const ssr = await ssrInit(context);
191196

192197
if (session) {
193-
res.writeHead(302, { Location: "/" });
194-
res.end();
195-
return;
198+
return {
199+
redirect: {
200+
destination: "/",
201+
permanent: false,
202+
},
203+
};
196204
}
197205

198206
return {
199-
csrfToken: await getCsrfToken(context),
207+
props: {
208+
csrfToken: await getCsrfToken(context),
209+
trpcState: ssr.dehydrate(),
210+
},
200211
};
201-
};
212+
}

pages/auth/logout.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { CheckIcon } from "@heroicons/react/outline";
2+
import { GetServerSidePropsContext } from "next";
23
import Link from "next/link";
34

45
import { useLocale } from "@lib/hooks/useLocale";
56

67
import { HeadSeo } from "@components/seo/head-seo";
78

9+
import { ssrInit } from "@server/lib/ssr";
10+
811
export default function Logout() {
912
const { t } = useLocale();
1013

@@ -45,3 +48,13 @@ export default function Logout() {
4548
</div>
4649
);
4750
}
51+
52+
export async function getServerSideProps(context: GetServerSidePropsContext) {
53+
const ssr = await ssrInit(context);
54+
55+
return {
56+
props: {
57+
trpcState: ssr.dehydrate(),
58+
},
59+
};
60+
}

pages/auth/signup.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { HeadSeo } from "@components/seo/head-seo";
1313
import { Alert } from "@components/ui/Alert";
1414
import Button from "@components/ui/Button";
1515

16+
import { ssrInit } from "@server/lib/ssr";
17+
1618
type Props = inferSSRProps<typeof getServerSideProps>;
1719

1820
type FormValues = {
@@ -133,6 +135,7 @@ export default function Signup({ email }: Props) {
133135
}
134136

135137
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
138+
const ssr = await ssrInit(ctx);
136139
const token = asStringOrNull(ctx.query.token);
137140
if (!token) {
138141
return {
@@ -169,9 +172,17 @@ export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
169172

170173
if (existingUser) {
171174
return {
172-
redirect: { permanent: false, destination: "/auth/login?callbackUrl=" + ctx.query.callbackUrl },
175+
redirect: {
176+
permanent: false,
177+
destination: "/auth/login?callbackUrl=" + ctx.query.callbackUrl,
178+
},
173179
};
174180
}
175181

176-
return { props: { email: verificationRequest.identifier } };
182+
return {
183+
props: {
184+
email: verificationRequest.identifier,
185+
trpcState: ssr.dehydrate(),
186+
},
187+
};
177188
};

0 commit comments

Comments
 (0)