From c7cba811f4e70daab6b4d4b87962ffd8c7fb2fbb Mon Sep 17 00:00:00 2001 From: je1999 Date: Thu, 9 May 2024 09:39:10 -0400 Subject: [PATCH] feat: Add redirect to login page In the useEffect hook, implement logic to check if the user session is active using the ory.toSession() function. If the session is active, redirect the user to the login page. --- src/app/[lang]/identification/form.tsx | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/app/[lang]/identification/form.tsx b/src/app/[lang]/identification/form.tsx index 58a37818..42be99cb 100644 --- a/src/app/[lang]/identification/form.tsx +++ b/src/app/[lang]/identification/form.tsx @@ -7,7 +7,7 @@ import { useReCaptcha } from 'next-recaptcha-v3'; import { useRouter } from 'next/navigation'; import { useForm } from 'react-hook-form'; import * as Sentry from '@sentry/nextjs'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import Link from 'next/link'; import { z } from 'zod'; @@ -25,6 +25,7 @@ import { CustomTextMask } from '@/components/CustomTextMask'; import { useSnackAlert } from '@/components/elements/alert'; import { ButtonApp } from '@/components/elements/button'; import { Validations } from '@/common/helpers'; +import { ory } from '@/common/lib/ory'; import theme from '@/components/themes/theme'; import { useLanguage } from '../provider'; @@ -32,12 +33,25 @@ type CedulaForm = z.infer>; export function Form() { const { AlertError, AlertWarning } = useSnackAlert(); - const [loading, setLoading] = useState(false); + const [loading, setLoading] = useState(true); const { executeRecaptcha } = useReCaptcha(); const router = useRouter(); const { intl } = useLanguage(); + useEffect(() => { + ory + .toSession() + .then(({ data }) => { + if (data.active) { + return router.push('https://mi.cuentaunica.gob.do/ui/login'); + } else { + setLoading(false); + } + }) + .catch(() => setLoading(false)); + }, []); + const { handleSubmit, formState: { errors },