♻️ Add direct login after registration

This commit is contained in:
Meier Lukas
2023-08-10 20:04:41 +02:00
parent 558beae12f
commit 0282344793

View File

@@ -3,9 +3,11 @@ 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';
import { signIn } from 'next-auth/react';
import { useTranslation } from 'next-i18next'; import { useTranslation } from 'next-i18next';
import Head from 'next/head'; import Head from 'next/head';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { useState } from 'react';
import { z } from 'zod'; import { z } from 'zod';
import { getServerAuthSession } from '~/server/auth'; import { getServerAuthSession } from '~/server/auth';
import { prisma } from '~/server/db'; import { prisma } from '~/server/db';
@@ -22,6 +24,7 @@ export default function AuthInvitePage() {
const router = useRouter(); const router = useRouter();
const query = router.query as { token: string }; const query = router.query as { token: string };
const { mutateAsync } = api.user.createFromInvite.useMutation(); const { mutateAsync } = api.user.createFromInvite.useMutation();
const [isLoading, setIsLoading] = useState(false);
const form = useForm<z.infer<typeof signUpFormSchema>>({ const form = useForm<z.infer<typeof signUpFormSchema>>({
validateInputOnChange: true, validateInputOnChange: true,
@@ -36,6 +39,7 @@ export default function AuthInvitePage() {
message: `${t('notifications.loading.text')}...`, message: `${t('notifications.loading.text')}...`,
loading: true, loading: true,
}); });
setIsLoading(true);
void mutateAsync( void mutateAsync(
{ {
...values, ...values,
@@ -50,7 +54,19 @@ export default function AuthInvitePage() {
color: 'teal', color: 'teal',
icon: <IconCheck />, icon: <IconCheck />,
}); });
router.push('/auth/login'); signIn('credentials', {
redirect: false,
name: values.username,
password: values.password,
callbackUrl: '/',
}).then((response) => {
if (!response?.ok) {
// Redirect to login page if something went wrong
router.push('/auth/login');
return;
}
router.push('/manage');
});
}, },
onError() { onError() {
updateNotification({ updateNotification({
@@ -106,7 +122,7 @@ export default function AuthInvitePage() {
{...form.getInputProps('passwordConfirmation')} {...form.getInputProps('passwordConfirmation')}
/> />
<Button fullWidth type="submit"> <Button fullWidth type="submit" loading={isLoading}>
{t('form.buttons.submit')} {t('form.buttons.submit')}
</Button> </Button>
</Stack> </Stack>