Skip to content

Commit

Permalink
注册验证邮箱
Browse files Browse the repository at this point in the history
  • Loading branch information
xmdhs committed Nov 24, 2023
1 parent f121dca commit 8bdff06
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
5 changes: 3 additions & 2 deletions frontend/src/apis/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ export async function login(email: string, password: string, captchaToken: strin
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", {
method: "POST",
body: JSON.stringify({
"Email": email,
"Password": password,
"Name": username,
"CaptchaToken": captchaToken
"CaptchaToken": captchaToken,
"EmailJwt": code,
})
})
return await apiGet<tokenData>(v)
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/CheckInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import TextField from '@mui/material/TextField';
import { useState, useImperativeHandle, forwardRef } from 'react';
import type { TextFieldProps } from '@mui/material/TextField';
import { useControllableValue } from 'ahooks';

export type refType = {
verify: () => boolean
Expand All @@ -15,7 +16,8 @@ type prop = {

export const CheckInput = forwardRef<refType, prop>(({ required, checkList, ...textFied }, ref) => {
const [err, setErr] = useState("");
const [value, setValue] = useState("");
const [value, setValue] = useControllableValue<string>(textFied);


const check = (value: string) => {
if (required && (!value || value == "")) {
Expand Down
43 changes: 39 additions & 4 deletions frontend/src/views/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
import Typography from '@mui/material/Typography';
import Container from '@mui/material/Container';
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 { useRef, useState } from 'react';
import Alert from '@mui/material/Alert';
Expand All @@ -22,6 +22,7 @@ import useTitle from '@/hooks/useTitle';
import { ApiErr } from '@/apis/error';
import { useSetAtom } from 'jotai';
import { token, user } from '@/store/store';
import { useRequest } from 'ahooks';

export default function SignUp() {
const [regErr, setRegErr] = useState("");
Expand All @@ -32,6 +33,38 @@ export default function SignUp() {
useTitle("注册")
const setToken = useSetAtom(token)
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>())
Expand All @@ -42,7 +75,6 @@ export default function SignUp() {
if (loading) return
const data = new FormData(event.currentTarget);
const d = {
email: data.get('email')?.toString(),
password: data.get('password')?.toString(),
username: data.get("username")?.toString()
}
Expand All @@ -54,7 +86,7 @@ export default function SignUp() {
return
}
setLoading(true)
register(d.email ?? "", d.username ?? "", d.password ?? "", captchaToken).
register(email ?? "", d.username ?? "", d.password ?? "", captchaToken, code).
then(v => {
if (!v) return
setToken(v.token)
Expand All @@ -71,7 +103,7 @@ export default function SignUp() {
switch (v.code) {
case 10:
setRegErr("验证码错误")
return
return
case 3:
setRegErr("邮箱已存在")
return
Expand Down Expand Up @@ -116,6 +148,9 @@ export default function SignUp() {
fullWidth
name="email"
label="邮箱"
value={email}
disabled={disableEmail}
onChange={v => setEmail(v.target.value)}
autoComplete="email"
ref={(dom) => {
dom && checkList.current.set("1", dom)
Expand Down
9 changes: 7 additions & 2 deletions service/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,16 @@ func (e EmailService) SendVerifyUrl(ctx context.Context, email string, interval
return fmt.Errorf("SendVerifyUrl: %w", err)
}

q := url.Values{}
q.Set("code", code)
q.Set("email", email)

u := url.URL{
Host: host,
Scheme: "http",
Path: "/register?code=" + url.QueryEscape(code),
Path: "/register",
}
u.RawQuery = q.Encode()

if e.config.WebBaseUrl != "" {
webBase, err := url.Parse(e.config.WebBaseUrl)
Expand Down Expand Up @@ -156,7 +161,7 @@ var (

func (e EmailService) VerifyJwt(email, jwtStr string) 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 {
return fmt.Errorf("VerifyJwt: %w", err)
Expand Down

0 comments on commit 8bdff06

Please sign in to comment.