♻️ Make login and invite page inaccessible when signed in

This commit is contained in:
Meier Lukas
2023-08-01 14:51:27 +02:00
parent a75d19fd78
commit 6b8d94b6b5
2 changed files with 42 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
import { Button, Card, Flex, PasswordInput, Stack, Text, TextInput, Title } from '@mantine/core';
import { useForm, zodResolver } from '@mantine/form';
import { useForm } from '@mantine/form';
import { showNotification, updateNotification } from '@mantine/notifications';
import { IconCheck, IconX } from '@tabler/icons-react';
import { GetServerSideProps } from 'next';
@@ -7,6 +7,7 @@ import { useTranslation } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { useRouter } from 'next/router';
import { z } from 'zod';
import { getServerAuthSession } from '~/server/auth';
import { prisma } from '~/server/db';
import { inviteNamespaces } from '~/tools/server/translation-namespaces';
import { api } from '~/utils/api';
@@ -114,10 +115,27 @@ const routeParamsSchema = z.object({
inviteId: z.string(),
});
export const getServerSideProps: GetServerSideProps = async ({ locale, query, params }) => {
export const getServerSideProps: GetServerSideProps = async ({
locale,
req,
res,
query,
params,
}) => {
const session = await getServerAuthSession({ req, res });
if (session) {
return {
redirect: {
destination: '/',
permanent: false,
},
};
}
const queryParams = queryParamsSchema.safeParse(query);
const routeParams = routeParamsSchema.safeParse(params);
console.log(queryParams, routeParams);
if (!queryParams.success || !routeParams.success) {
return {
notFound: true,

View File

@@ -9,17 +9,17 @@ import {
TextInput,
Title,
} from '@mantine/core';
import { useForm, zodResolver } from '@mantine/form';
import { showNotification, updateNotification } from '@mantine/notifications';
import { IconAlertCircle, IconAlertTriangle, IconCheck, IconX } from '@tabler/icons-react';
import axios from 'axios';
import { setCookie } from 'cookies-next';
import { useForm } from '@mantine/form';
import { IconAlertTriangle } from '@tabler/icons-react';
import { GetServerSideProps } from 'next';
import { signIn } from 'next-auth/react';
import { useTranslation } from 'next-i18next';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
import { useRouter } from 'next/router';
import { z } from 'zod';
import { CommonHeader } from '~/components/layout/common-header';
import { getServerAuthSession } from '~/server/auth';
import { useI18nZodResolver } from '~/utils/i18n-zod-resolver';
import { signInSchema } from '~/validations/user';
import { loginNamespaces } from '../../tools/server/translation-namespaces';
@@ -27,11 +27,12 @@ import { loginNamespaces } from '../../tools/server/translation-namespaces';
export default function LoginPage() {
const { t } = useTranslation(['authentication/login']);
const queryParams = useRouter().query as { error?: 'CredentialsSignin' | (string & {}) };
const { i18nZodResolver } = useI18nZodResolver();
const form = useForm<z.infer<typeof signInSchema>>({
validateInputOnChange: true,
validateInputOnBlur: true,
validate: zodResolver(signInSchema),
validate: i18nZodResolver(signInSchema),
});
const handleSubmit = (values: z.infer<typeof signInSchema>) => {
@@ -89,11 +90,22 @@ export default function LoginPage() {
);
}
export async function getServerSideProps({ locale }: { locale: string }) {
export const getServerSideProps: GetServerSideProps = async ({ locale, req, res }) => {
const session = await getServerAuthSession({ req, res });
if (session) {
return {
redirect: {
destination: '/',
permanent: false,
},
};
}
return {
props: {
...(await serverSideTranslations(locale, loginNamespaces)),
...(await serverSideTranslations(locale ?? 'en', loginNamespaces)),
// Will be passed to the page component as props
},
};
}
};