|
| 1 | +import { GetServerSidePropsContext } from "next"; |
1 | 2 | import { getCsrfToken, signIn } from "next-auth/client";
|
2 | 3 | import Link from "next/link";
|
3 | 4 | import { useRouter } from "next/router";
|
4 | 5 | import { useState } from "react";
|
5 | 6 |
|
6 | 7 | import { ErrorCode, getSession } from "@lib/auth";
|
7 | 8 | import { useLocale } from "@lib/hooks/useLocale";
|
| 9 | +import { inferSSRProps } from "@lib/types/inferSSRProps"; |
8 | 10 |
|
9 | 11 | import AddToHomescreen from "@components/AddToHomescreen";
|
10 | 12 | import Loader from "@components/Loader";
|
11 | 13 | import { HeadSeo } from "@components/seo/head-seo";
|
12 | 14 |
|
13 |
| -export default function Login({ csrfToken }) { |
| 15 | +import { ssrInit } from "@server/lib/ssr"; |
| 16 | + |
| 17 | +export default function Login({ csrfToken }: inferSSRProps<typeof getServerSideProps>) { |
14 | 18 | const { t } = useLocale();
|
15 | 19 | const router = useRouter();
|
16 | 20 | const [email, setEmail] = useState("");
|
@@ -90,7 +94,7 @@ export default function Login({ csrfToken }) {
|
90 | 94 | <div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
|
91 | 95 | <div className="bg-white py-8 px-4 mx-2 rounded-sm sm:px-10 border border-neutral-200">
|
92 | 96 | <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 /> |
94 | 98 | <div>
|
95 | 99 | <label htmlFor="email" className="block text-sm font-medium text-neutral-700">
|
96 | 100 | {t("email_address")}
|
@@ -185,17 +189,24 @@ export default function Login({ csrfToken }) {
|
185 | 189 | );
|
186 | 190 | }
|
187 | 191 |
|
188 |
| -Login.getInitialProps = async (context) => { |
189 |
| - const { req, res } = context; |
| 192 | +export async function getServerSideProps(context: GetServerSidePropsContext) { |
| 193 | + const { req } = context; |
190 | 194 | const session = await getSession({ req });
|
| 195 | + const ssr = await ssrInit(context); |
191 | 196 |
|
192 | 197 | if (session) {
|
193 |
| - res.writeHead(302, { Location: "/" }); |
194 |
| - res.end(); |
195 |
| - return; |
| 198 | + return { |
| 199 | + redirect: { |
| 200 | + destination: "/", |
| 201 | + permanent: false, |
| 202 | + }, |
| 203 | + }; |
196 | 204 | }
|
197 | 205 |
|
198 | 206 | return {
|
199 |
| - csrfToken: await getCsrfToken(context), |
| 207 | + props: { |
| 208 | + csrfToken: await getCsrfToken(context), |
| 209 | + trpcState: ssr.dehydrate(), |
| 210 | + }, |
200 | 211 | };
|
201 |
| -}; |
| 212 | +} |
0 commit comments