♻️ Make login and invite page inaccessible when signed in
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Button, Card, Flex, PasswordInput, Stack, Text, TextInput, Title } from '@mantine/core';
|
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 { showNotification, updateNotification } from '@mantine/notifications';
|
||||||
import { IconCheck, IconX } from '@tabler/icons-react';
|
import { IconCheck, IconX } from '@tabler/icons-react';
|
||||||
import { GetServerSideProps } from 'next';
|
import { GetServerSideProps } from 'next';
|
||||||
@@ -7,6 +7,7 @@ import { useTranslation } from 'next-i18next';
|
|||||||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
import { getServerAuthSession } from '~/server/auth';
|
||||||
import { prisma } from '~/server/db';
|
import { prisma } from '~/server/db';
|
||||||
import { inviteNamespaces } from '~/tools/server/translation-namespaces';
|
import { inviteNamespaces } from '~/tools/server/translation-namespaces';
|
||||||
import { api } from '~/utils/api';
|
import { api } from '~/utils/api';
|
||||||
@@ -114,10 +115,27 @@ const routeParamsSchema = z.object({
|
|||||||
inviteId: z.string(),
|
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 queryParams = queryParamsSchema.safeParse(query);
|
||||||
const routeParams = routeParamsSchema.safeParse(params);
|
const routeParams = routeParamsSchema.safeParse(params);
|
||||||
console.log(queryParams, routeParams);
|
|
||||||
if (!queryParams.success || !routeParams.success) {
|
if (!queryParams.success || !routeParams.success) {
|
||||||
return {
|
return {
|
||||||
notFound: true,
|
notFound: true,
|
||||||
|
|||||||
@@ -9,17 +9,17 @@ import {
|
|||||||
TextInput,
|
TextInput,
|
||||||
Title,
|
Title,
|
||||||
} from '@mantine/core';
|
} from '@mantine/core';
|
||||||
import { useForm, zodResolver } from '@mantine/form';
|
import { useForm } from '@mantine/form';
|
||||||
import { showNotification, updateNotification } from '@mantine/notifications';
|
import { IconAlertTriangle } from '@tabler/icons-react';
|
||||||
import { IconAlertCircle, IconAlertTriangle, IconCheck, IconX } from '@tabler/icons-react';
|
import { GetServerSideProps } from 'next';
|
||||||
import axios from 'axios';
|
|
||||||
import { setCookie } from 'cookies-next';
|
|
||||||
import { signIn } from 'next-auth/react';
|
import { signIn } from 'next-auth/react';
|
||||||
import { useTranslation } from 'next-i18next';
|
import { useTranslation } from 'next-i18next';
|
||||||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
import { CommonHeader } from '~/components/layout/common-header';
|
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 { signInSchema } from '~/validations/user';
|
||||||
|
|
||||||
import { loginNamespaces } from '../../tools/server/translation-namespaces';
|
import { loginNamespaces } from '../../tools/server/translation-namespaces';
|
||||||
@@ -27,11 +27,12 @@ import { loginNamespaces } from '../../tools/server/translation-namespaces';
|
|||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const { t } = useTranslation(['authentication/login']);
|
const { t } = useTranslation(['authentication/login']);
|
||||||
const queryParams = useRouter().query as { error?: 'CredentialsSignin' | (string & {}) };
|
const queryParams = useRouter().query as { error?: 'CredentialsSignin' | (string & {}) };
|
||||||
|
const { i18nZodResolver } = useI18nZodResolver();
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof signInSchema>>({
|
const form = useForm<z.infer<typeof signInSchema>>({
|
||||||
validateInputOnChange: true,
|
validateInputOnChange: true,
|
||||||
validateInputOnBlur: true,
|
validateInputOnBlur: true,
|
||||||
validate: zodResolver(signInSchema),
|
validate: i18nZodResolver(signInSchema),
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSubmit = (values: z.infer<typeof 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 {
|
return {
|
||||||
props: {
|
props: {
|
||||||
...(await serverSideTranslations(locale, loginNamespaces)),
|
...(await serverSideTranslations(locale ?? 'en', loginNamespaces)),
|
||||||
// Will be passed to the page component as props
|
// Will be passed to the page component as props
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user