注册验证邮箱
This commit is contained in:
parent
f121dca589
commit
8bdff06dca
@ -14,14 +14,15 @@ export async function login(email: string, password: string, captchaToken: strin
|
|||||||
return await apiGet<tokenData>(v)
|
return await apiGet<tokenData>(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function register(email: string, username: string, password: string, captchaToken: string) {
|
export async function register(email: string, username: string, password: string, captchaToken: string, code: string) {
|
||||||
const v = await fetch(root() + "/api/v1/user/reg", {
|
const v = await fetch(root() + "/api/v1/user/reg", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
"Email": email,
|
"Email": email,
|
||||||
"Password": password,
|
"Password": password,
|
||||||
"Name": username,
|
"Name": username,
|
||||||
"CaptchaToken": captchaToken
|
"CaptchaToken": captchaToken,
|
||||||
|
"EmailJwt": code,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
return await apiGet<tokenData>(v)
|
return await apiGet<tokenData>(v)
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import TextField from '@mui/material/TextField';
|
import TextField from '@mui/material/TextField';
|
||||||
import { useState, useImperativeHandle, forwardRef } from 'react';
|
import { useState, useImperativeHandle, forwardRef } from 'react';
|
||||||
import type { TextFieldProps } from '@mui/material/TextField';
|
import type { TextFieldProps } from '@mui/material/TextField';
|
||||||
|
import { useControllableValue } from 'ahooks';
|
||||||
|
|
||||||
export type refType = {
|
export type refType = {
|
||||||
verify: () => boolean
|
verify: () => boolean
|
||||||
@ -15,7 +16,8 @@ type prop = {
|
|||||||
|
|
||||||
export const CheckInput = forwardRef<refType, prop>(({ required, checkList, ...textFied }, ref) => {
|
export const CheckInput = forwardRef<refType, prop>(({ required, checkList, ...textFied }, ref) => {
|
||||||
const [err, setErr] = useState("");
|
const [err, setErr] = useState("");
|
||||||
const [value, setValue] = useState("");
|
const [value, setValue] = useControllableValue<string>(textFied);
|
||||||
|
|
||||||
|
|
||||||
const check = (value: string) => {
|
const check = (value: string) => {
|
||||||
if (required && (!value || value == "")) {
|
if (required && (!value || value == "")) {
|
||||||
|
@ -9,7 +9,7 @@ import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
|
|||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import Container from '@mui/material/Container';
|
import Container from '@mui/material/Container';
|
||||||
import { Link as RouterLink } from "react-router-dom";
|
import { Link as RouterLink } from "react-router-dom";
|
||||||
import { register } from '@/apis/apis'
|
import { getConfig, register } from '@/apis/apis'
|
||||||
import CheckInput, { refType } from '@/components/CheckInput'
|
import CheckInput, { refType } from '@/components/CheckInput'
|
||||||
import { useRef, useState } from 'react';
|
import { useRef, useState } from 'react';
|
||||||
import Alert from '@mui/material/Alert';
|
import Alert from '@mui/material/Alert';
|
||||||
@ -22,6 +22,7 @@ import useTitle from '@/hooks/useTitle';
|
|||||||
import { ApiErr } from '@/apis/error';
|
import { ApiErr } from '@/apis/error';
|
||||||
import { useSetAtom } from 'jotai';
|
import { useSetAtom } from 'jotai';
|
||||||
import { token, user } from '@/store/store';
|
import { token, user } from '@/store/store';
|
||||||
|
import { useRequest } from 'ahooks';
|
||||||
|
|
||||||
export default function SignUp() {
|
export default function SignUp() {
|
||||||
const [regErr, setRegErr] = useState("");
|
const [regErr, setRegErr] = useState("");
|
||||||
@ -32,6 +33,38 @@ export default function SignUp() {
|
|||||||
useTitle("注册")
|
useTitle("注册")
|
||||||
const setToken = useSetAtom(token)
|
const setToken = useSetAtom(token)
|
||||||
const setUserInfo = useSetAtom(user)
|
const setUserInfo = useSetAtom(user)
|
||||||
|
const [code, setCode] = useState("")
|
||||||
|
const [email, setEmail] = useState("")
|
||||||
|
const [disableEmail, setDisableEmail] = useState(false)
|
||||||
|
|
||||||
|
const u = new URL(location.href)
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const e = u.searchParams.get("email")
|
||||||
|
if (!e || e == "") return
|
||||||
|
setEmail(e)
|
||||||
|
setDisableEmail(true)
|
||||||
|
}, [u.searchParams])
|
||||||
|
|
||||||
|
useRequest(getConfig, {
|
||||||
|
cacheKey: "/api/v1/config",
|
||||||
|
staleTime: 60000,
|
||||||
|
onError: e => {
|
||||||
|
console.warn(e)
|
||||||
|
setRegErr(String(e))
|
||||||
|
},
|
||||||
|
onSuccess: v => {
|
||||||
|
if (!v.NeedEmail) return
|
||||||
|
|
||||||
|
const code = u.searchParams.get("code")
|
||||||
|
if (!code || code == "") {
|
||||||
|
navigate("/register_email")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setCode(code)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const checkList = React.useRef<Map<string, refType>>(new Map<string, refType>())
|
const checkList = React.useRef<Map<string, refType>>(new Map<string, refType>())
|
||||||
@ -42,7 +75,6 @@ export default function SignUp() {
|
|||||||
if (loading) return
|
if (loading) return
|
||||||
const data = new FormData(event.currentTarget);
|
const data = new FormData(event.currentTarget);
|
||||||
const d = {
|
const d = {
|
||||||
email: data.get('email')?.toString(),
|
|
||||||
password: data.get('password')?.toString(),
|
password: data.get('password')?.toString(),
|
||||||
username: data.get("username")?.toString()
|
username: data.get("username")?.toString()
|
||||||
}
|
}
|
||||||
@ -54,7 +86,7 @@ export default function SignUp() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
setLoading(true)
|
setLoading(true)
|
||||||
register(d.email ?? "", d.username ?? "", d.password ?? "", captchaToken).
|
register(email ?? "", d.username ?? "", d.password ?? "", captchaToken, code).
|
||||||
then(v => {
|
then(v => {
|
||||||
if (!v) return
|
if (!v) return
|
||||||
setToken(v.token)
|
setToken(v.token)
|
||||||
@ -71,7 +103,7 @@ export default function SignUp() {
|
|||||||
switch (v.code) {
|
switch (v.code) {
|
||||||
case 10:
|
case 10:
|
||||||
setRegErr("验证码错误")
|
setRegErr("验证码错误")
|
||||||
return
|
return
|
||||||
case 3:
|
case 3:
|
||||||
setRegErr("邮箱已存在")
|
setRegErr("邮箱已存在")
|
||||||
return
|
return
|
||||||
@ -116,6 +148,9 @@ export default function SignUp() {
|
|||||||
fullWidth
|
fullWidth
|
||||||
name="email"
|
name="email"
|
||||||
label="邮箱"
|
label="邮箱"
|
||||||
|
value={email}
|
||||||
|
disabled={disableEmail}
|
||||||
|
onChange={v => setEmail(v.target.value)}
|
||||||
autoComplete="email"
|
autoComplete="email"
|
||||||
ref={(dom) => {
|
ref={(dom) => {
|
||||||
dom && checkList.current.set("1", dom)
|
dom && checkList.current.set("1", dom)
|
||||||
|
@ -118,11 +118,16 @@ func (e EmailService) SendVerifyUrl(ctx context.Context, email string, interval
|
|||||||
return fmt.Errorf("SendVerifyUrl: %w", err)
|
return fmt.Errorf("SendVerifyUrl: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
q := url.Values{}
|
||||||
|
q.Set("code", code)
|
||||||
|
q.Set("email", email)
|
||||||
|
|
||||||
u := url.URL{
|
u := url.URL{
|
||||||
Host: host,
|
Host: host,
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
Path: "/register?code=" + url.QueryEscape(code),
|
Path: "/register",
|
||||||
}
|
}
|
||||||
|
u.RawQuery = q.Encode()
|
||||||
|
|
||||||
if e.config.WebBaseUrl != "" {
|
if e.config.WebBaseUrl != "" {
|
||||||
webBase, err := url.Parse(e.config.WebBaseUrl)
|
webBase, err := url.Parse(e.config.WebBaseUrl)
|
||||||
@ -156,7 +161,7 @@ var (
|
|||||||
|
|
||||||
func (e EmailService) VerifyJwt(email, jwtStr string) error {
|
func (e EmailService) VerifyJwt(email, jwtStr string) error {
|
||||||
token, err := jwt.ParseWithClaims(jwtStr, &jwt.RegisteredClaims{}, func(t *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(jwtStr, &jwt.RegisteredClaims{}, func(t *jwt.Token) (interface{}, error) {
|
||||||
return e.pri.PublicKey, nil
|
return &e.pri.PublicKey, nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("VerifyJwt: %w", err)
|
return fmt.Errorf("VerifyJwt: %w", err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user