mirror of
https://github.com/perfect-panel/ppanel-web.git
synced 2026-02-16 13:21:10 -05:00
🐛 fix(auth): Refactor user authentication forms to remove global store dependency and improve type handling
This commit is contained in:
parent
6301409531
commit
12026b02cd
@ -4,7 +4,6 @@ import {
|
|||||||
NEXT_PUBLIC_DEFAULT_USER_EMAIL,
|
NEXT_PUBLIC_DEFAULT_USER_EMAIL,
|
||||||
NEXT_PUBLIC_DEFAULT_USER_PASSWORD,
|
NEXT_PUBLIC_DEFAULT_USER_PASSWORD,
|
||||||
} from '@/config/constants';
|
} from '@/config/constants';
|
||||||
import useGlobalStore from '@/config/use-global';
|
|
||||||
import { checkUser, resetPassword, userLogin, userRegister } from '@/services/common/auth';
|
import { checkUser, resetPassword, userLogin, userRegister } from '@/services/common/auth';
|
||||||
import { getRedirectUrl, setAuthorization } from '@/utils/common';
|
import { getRedirectUrl, setAuthorization } from '@/utils/common';
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
@ -18,8 +17,6 @@ import UserResetForm from './user-reset-form';
|
|||||||
|
|
||||||
export default function UserAuthForm() {
|
export default function UserAuthForm() {
|
||||||
const t = useTranslations('auth');
|
const t = useTranslations('auth');
|
||||||
const { common } = useGlobalStore();
|
|
||||||
const { register } = common;
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [type, setType] = useState<'login' | 'register' | 'reset'>();
|
const [type, setType] = useState<'login' | 'register' | 'reset'>();
|
||||||
const [loading, startTransition] = useTransition();
|
const [loading, startTransition] = useTransition();
|
||||||
|
|||||||
@ -8,30 +8,21 @@ import { useEffect, useRef, useState } from 'react';
|
|||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
interface UserCheckFormProps {
|
||||||
|
loading?: boolean;
|
||||||
|
onSubmit: (data: any) => void;
|
||||||
|
initialValues: any;
|
||||||
|
}
|
||||||
|
|
||||||
export default function UserCheckForm({
|
export default function UserCheckForm({
|
||||||
loading,
|
loading,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
initialValues,
|
initialValues,
|
||||||
}: {
|
}: Readonly<UserCheckFormProps>) {
|
||||||
loading?: boolean;
|
|
||||||
onSubmit: (data: any) => void;
|
|
||||||
initialValues: any;
|
|
||||||
}) {
|
|
||||||
const t = useTranslations('auth.check');
|
const t = useTranslations('auth.check');
|
||||||
// const { common } = useGlobalStore();
|
|
||||||
// const { register } = common;
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
email: z.string().email(t('email')),
|
email: z.string().email(t('email')),
|
||||||
// .refine(
|
|
||||||
// (email) => {
|
|
||||||
// if (!register.enable_email_domain_suffix) return true;
|
|
||||||
// const domain = email.split('@')[1] as string;
|
|
||||||
// return register.email_domain_suffix_list.split('\n').includes(domain);
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// message: t('whitelist'),
|
|
||||||
// },
|
|
||||||
// ),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
@ -61,7 +52,7 @@ export default function UserCheckForm({
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (initialValues && initialValues.email) {
|
if (initialValues?.email) {
|
||||||
handleEmailChange(initialValues.email);
|
handleEmailChange(initialValues.email);
|
||||||
}
|
}
|
||||||
return () => {
|
return () => {
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import useGlobalStore from '@/config/use-global';
|
import { resetPassword, userLogin, userRegister } from '@/services/common/auth';
|
||||||
import { checkUser, resetPassword, userLogin, userRegister } from '@/services/common/auth';
|
|
||||||
import { useTranslations } from 'next-intl';
|
import { useTranslations } from 'next-intl';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { ReactNode, useState, useTransition } from 'react';
|
import { ReactNode, useState, useTransition } from 'react';
|
||||||
@ -19,8 +18,6 @@ import UserResetForm from './user-reset-form';
|
|||||||
|
|
||||||
export default function UserAuthForm() {
|
export default function UserAuthForm() {
|
||||||
const t = useTranslations('auth');
|
const t = useTranslations('auth');
|
||||||
const { common } = useGlobalStore();
|
|
||||||
const { register } = common;
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [type, setType] = useState<'login' | 'register' | 'reset'>();
|
const [type, setType] = useState<'login' | 'register' | 'reset'>();
|
||||||
const [loading, startTransition] = useTransition();
|
const [loading, startTransition] = useTransition();
|
||||||
@ -42,33 +39,31 @@ export default function UserAuthForm() {
|
|||||||
startTransition(async () => {
|
startTransition(async () => {
|
||||||
try {
|
try {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'login':
|
case 'login': {
|
||||||
// eslint-disable-next-line no-case-declarations
|
|
||||||
const login = await userLogin(params);
|
const login = await userLogin(params);
|
||||||
toast.success(t('login.success'));
|
toast.success(t('login.success'));
|
||||||
onLogin(login.data.data?.token);
|
onLogin(login.data.data?.token);
|
||||||
break;
|
break;
|
||||||
case 'register':
|
}
|
||||||
// eslint-disable-next-line no-case-declarations
|
case 'register': {
|
||||||
const create = await userRegister(params);
|
const create = await userRegister(params);
|
||||||
toast.success(t('register.success'));
|
toast.success(t('register.success'));
|
||||||
onLogin(create.data.data?.token);
|
onLogin(create.data.data?.token);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case 'reset':
|
case 'reset':
|
||||||
await resetPassword(params);
|
await resetPassword(params);
|
||||||
toast.success(t('reset.success'));
|
toast.success(t('reset.success'));
|
||||||
setType('login');
|
setType('login');
|
||||||
break;
|
break;
|
||||||
default:
|
default: {
|
||||||
if (type === 'reset') break;
|
if (type === 'reset') break;
|
||||||
// eslint-disable-next-line no-case-declarations
|
|
||||||
const response = await checkUser(params);
|
|
||||||
setInitialValues({
|
setInitialValues({
|
||||||
...initialValues,
|
...initialValues,
|
||||||
...params,
|
...params,
|
||||||
});
|
});
|
||||||
setType(response.data.data?.exist ? 'login' : 'register');
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
/* empty */
|
/* empty */
|
||||||
@ -116,6 +111,8 @@ export default function UserAuthForm() {
|
|||||||
loading={loading}
|
loading={loading}
|
||||||
onSubmit={handleFormSubmit}
|
onSubmit={handleFormSubmit}
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
|
type={type}
|
||||||
|
setType={setType}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import useGlobalStore from '@/config/use-global';
|
import useGlobalStore from '@/config/use-global';
|
||||||
|
import { checkUser } from '@/services/common/auth';
|
||||||
import { zodResolver } from '@hookform/resolvers/zod';
|
import { zodResolver } from '@hookform/resolvers/zod';
|
||||||
import { Icon } from '@iconify/react/dist/iconify.js';
|
import { Icon } from '@iconify/react/dist/iconify.js';
|
||||||
import { Button } from '@workspace/ui/components/button';
|
import { Button } from '@workspace/ui/components/button';
|
||||||
@ -9,32 +10,62 @@ import { useEffect, useRef, useState } from 'react';
|
|||||||
import { useForm } from 'react-hook-form';
|
import { useForm } from 'react-hook-form';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
interface UserCheckFormProps {
|
||||||
|
loading?: boolean;
|
||||||
|
onSubmit: (data: any) => void;
|
||||||
|
initialValues: any;
|
||||||
|
type?: 'login' | 'register' | 'reset';
|
||||||
|
setType: (type: 'login' | 'register' | 'reset') => void;
|
||||||
|
}
|
||||||
|
|
||||||
export default function UserCheckForm({
|
export default function UserCheckForm({
|
||||||
loading,
|
loading,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
initialValues,
|
initialValues,
|
||||||
}: {
|
type,
|
||||||
loading?: boolean;
|
setType,
|
||||||
onSubmit: (data: any) => void;
|
}: Readonly<UserCheckFormProps>) {
|
||||||
initialValues: any;
|
|
||||||
}) {
|
|
||||||
const t = useTranslations('auth.check');
|
const t = useTranslations('auth.check');
|
||||||
const { common } = useGlobalStore();
|
const { common } = useGlobalStore();
|
||||||
const { register } = common;
|
const { register } = common;
|
||||||
|
|
||||||
|
const handleCheckUser = async (email: string) => {
|
||||||
|
try {
|
||||||
|
const response = await checkUser({ email });
|
||||||
|
const exist = response.data.data?.exist;
|
||||||
|
|
||||||
|
if (exist) {
|
||||||
|
setType('login');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!register.enable_email_domain_suffix) {
|
||||||
|
setType('register');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const domain = email.split('@')[1];
|
||||||
|
const isValid = register.email_domain_suffix_list.split('\n').includes(domain || '');
|
||||||
|
|
||||||
|
if (isValid) {
|
||||||
|
setType('register');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch (error) {
|
||||||
|
console.log('Error checking user:', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
email: z
|
email: z
|
||||||
.string()
|
.string()
|
||||||
.email(t('email'))
|
.email(t('email'))
|
||||||
.refine(
|
.refine(handleCheckUser, {
|
||||||
(email) => {
|
message: t('whitelist'),
|
||||||
if (!register.enable_email_domain_suffix) return true;
|
}),
|
||||||
const domain = email.split('@')[1];
|
|
||||||
return register.email_domain_suffix_list.split('\n').includes(domain || '');
|
|
||||||
},
|
|
||||||
{
|
|
||||||
message: t('whitelist'),
|
|
||||||
},
|
|
||||||
),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
@ -45,7 +76,7 @@ export default function UserCheckForm({
|
|||||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||||
const typingTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
const typingTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
const handleEmailChange = (value: string) => {
|
const handleEmailChange = async (value: string) => {
|
||||||
form.setValue('email', value);
|
form.setValue('email', value);
|
||||||
|
|
||||||
if (typingTimeoutRef.current) {
|
if (typingTimeoutRef.current) {
|
||||||
@ -60,11 +91,11 @@ export default function UserCheckForm({
|
|||||||
} else {
|
} else {
|
||||||
setIsSubmitting(false);
|
setIsSubmitting(false);
|
||||||
}
|
}
|
||||||
}, 500);
|
}, 1000);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (initialValues && initialValues.email) {
|
if (initialValues?.email) {
|
||||||
handleEmailChange(initialValues.email);
|
handleEmailChange(initialValues.email);
|
||||||
}
|
}
|
||||||
return () => {
|
return () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user