diff --git a/src/app/services/auth.ts b/src/app/services/auth.ts index e888dca3..ef95d760 100644 --- a/src/app/services/auth.ts +++ b/src/app/services/auth.ts @@ -78,7 +78,11 @@ export const authApi = createApi({ query: (data) => ({ url: `user/register`, method: "POST", - body: data + body: { + ...data, + gender: 0, + device: "browser" + } }) }), // 更新token diff --git a/src/routes/invite/index.tsx b/src/routes/invite/index.tsx index f4d9a520..7ef83e93 100644 --- a/src/routes/invite/index.tsx +++ b/src/routes/invite/index.tsx @@ -18,7 +18,7 @@ const InvitePage: FC = () => { const { token: loginToken } = useAppSelector((store) => store.authData); const [secondPwd, setSecondPwd] = useState(""); const [samePwd, setSamePwd] = useState(true); - const [token, setToken] = useState(""); + const [token, setToken] = useState(); const [valid, setValid] = useState(false); const [register, { data, isLoading, isSuccess, isError, error }] = useRegisterMutation(); const [checkToken, { data: isValid, isLoading: checkLoading, isSuccess: checkSuccess }] = @@ -26,7 +26,7 @@ const InvitePage: FC = () => { useEffect(() => { const query = new URLSearchParams(location.search); - setToken(query.get("token")); + setToken(query.get("token") ?? undefined); }, []); useEffect(() => { @@ -54,7 +54,6 @@ const InvitePage: FC = () => { register({ ...input, magic_token: token, - gender: 1 }); }; diff --git a/src/routes/reg/Register.tsx b/src/routes/reg/Register.tsx index fba7dd6c..e0bea7bb 100644 --- a/src/routes/reg/Register.tsx +++ b/src/routes/reg/Register.tsx @@ -1,48 +1,49 @@ import { useState, useEffect, ChangeEvent, FormEvent } from "react"; import toast from "react-hot-toast"; +import { useTranslation } from "react-i18next"; import BASE_URL, { KEY_LOCAL_MAGIC_TOKEN } from "../../app/config"; import Input from "../../common/component/styled/Input"; import Button from "../../common/component/styled/Button"; -import { useLazyCheckEmailQuery, useSendRegMagicLinkMutation } from "../../app/services/auth"; +import { useLazyCheckEmailQuery, useRegisterMutation, useSendRegMagicLinkMutation } from "../../app/services/auth"; import EmailNextTip from "./EmailNextStepTip"; import SignInLink from "./SignInLink"; import Divider from "../../common/component/Divider"; -import { useTranslation } from "react-i18next"; import { useGetLoginConfigQuery } from "../../app/services/server"; import SocialLoginButtons from "../login/SocialLoginButtons"; +import { setAuthData } from "../../app/slices/auth.data"; +import { useDispatch } from "react-redux"; interface AuthForm { + name?: string, email: string; password: string; confirmPassword: string; } -export default function Reg() { +export default function Register() { const { t } = useTranslation("auth"); + const { t: ct } = useTranslation(); const [sendRegMagicLink, { isLoading: signingUp, data, isSuccess }] = useSendRegMagicLinkMutation(); + const dispatch = useDispatch(); + const [register, { isLoading: registering, data: regData, isSuccess: regSuccess }] = useRegisterMutation(); const [checkEmail, { isLoading: checkingEmail }] = useLazyCheckEmailQuery(); const { data: loginConfig, isSuccess: loginConfigSuccess } = useGetLoginConfigQuery(); // const navigateTo = useNavigate(); - const [magicToken, setMagicToken] = useState(""); const [input, setInput] = useState({ email: "", password: "", confirmPassword: "" }); - - useEffect(() => { - const query = new URLSearchParams(location.search); - const token = query.get("magic_token"); - if (token) { - //本地存一下 magic token 后续oauth流程用到 - localStorage.setItem(KEY_LOCAL_MAGIC_TOKEN, token); - setMagicToken(token); - } - }, []); - + const query = new URLSearchParams(location.search); + const magic_token = query.get("magic_token") ?? undefined; + if (magic_token) { + //本地存一下 magic token 后续oauth流程用到 + localStorage.setItem(KEY_LOCAL_MAGIC_TOKEN, magic_token); + } + // send reg link useEffect(() => { if (isSuccess && data) { const { new_magic_token, mail_is_sent } = data; @@ -52,21 +53,40 @@ export default function Reg() { } } }, [isSuccess, data]); + // register + useEffect(() => { + if (regSuccess && regData) { + // 更新本地认证信息 + toast.success(ct("tip.reg")); + dispatch(setAuthData(regData)); + // tricky + location.href = `/#/`; + } + }, [regSuccess, regData]); const handleReg = async (evt: FormEvent) => { evt.preventDefault(); - const { email, password, confirmPassword } = input; + const { name, email, password, confirmPassword } = input; if (password !== confirmPassword) { toast.error("Not Same Password!"); return; } const { data: canReg } = await checkEmail(email); if (canReg) { - sendRegMagicLink({ - magic_token: magicToken, - email, - password - }); + if (magic_token) { + sendRegMagicLink({ + magic_token, + email, + password + }); + } else { + // 带用户名的注册 + register({ + name, + email, + password + }); + } } else { toast.error("Email already registered!"); } @@ -93,12 +113,12 @@ export default function Reg() { who_can_sign_up: whoCanSignUp } = loginConfig; // magic token 没有并且没有开放注册 - if (whoCanSignUp !== "EveryOne" && !magicToken) + if (whoCanSignUp !== "EveryOne" && !magic_token) // todo: i18n return <>Sign up method is updated to Invitation Link Only; - const { email, password, confirmPassword } = input; + const { name, email, password, confirmPassword } = input; if (data?.mail_is_sent) return ; - const isLoading = signingUp || checkingEmail; + const isLoading = registering || signingUp || checkingEmail; return ( <> @@ -109,6 +129,17 @@ export default function Reg() {
+ {/* 不存在 magic token */} + {!magic_token && } - + ); } diff --git a/src/types/user.ts b/src/types/user.ts index fbdcde40..75ad7714 100644 --- a/src/types/user.ts +++ b/src/types/user.ts @@ -48,8 +48,8 @@ export interface UserDTO extends Partial { password: string; } -export interface UserRegDTO extends Pick, Pick { - password: string; +export interface UserRegDTO extends Partial>, Partial> { + password?: string; magic_token?: string } export interface UserRegResponse extends AuthToken { diff --git a/src/widget/Popup/Login/index.tsx b/src/widget/Popup/Login/index.tsx index 7f45c003..cd01e7ed 100644 --- a/src/widget/Popup/Login/index.tsx +++ b/src/widget/Popup/Login/index.tsx @@ -39,8 +39,6 @@ const Login = () => { name: `${name}-[${from}]`, email, password: email, - gender: 0, - device: "unknown" }); // const content = new FormData(form).get("prompt") as string; };