From 74fd0902ac75bef72f7f8671fb02dfbe460a9c73 Mon Sep 17 00:00:00 2001 From: xmdhs Date: Mon, 9 Oct 2023 20:58:33 +0800 Subject: [PATCH] =?UTF-8?q?web=20=E4=BD=BF=E7=94=A8=E4=B8=93=E7=94=A8?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/authlibskin/main.go | 23 +++++++++++++ config/yaml.go | 16 +++++++++ frontend/src/apis/apis.ts | 17 ++++------ frontend/src/apis/model.ts | 8 ++--- frontend/src/views/Login.tsx | 32 +++++++++++++++--- go.mod | 1 + go.sum | 1 + handle/admin.go | 19 +++-------- handle/error.go | 41 +++++++++++++++++++++++ handle/user.go | 57 +++++++++++++++++--------------- model/const.go | 2 ++ model/model.go | 14 +++++++- server/route/route.go | 3 +- service/admin.go | 9 ++++++ service/captcha.go | 12 +++++-- service/user.go | 41 +++++++++++++++++------ service/utils/auth.go | 59 ++++++++++++++++++++++++++++++++++ service/web.go | 10 ++++++ service/yggdrasil/user.go | 34 ++------------------ service/yggdrasil/yggdrasil.go | 19 +---------- 20 files changed, 293 insertions(+), 125 deletions(-) create mode 100644 cmd/authlibskin/main.go create mode 100644 config/yaml.go diff --git a/cmd/authlibskin/main.go b/cmd/authlibskin/main.go new file mode 100644 index 0000000..5bfee1d --- /dev/null +++ b/cmd/authlibskin/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "context" + "flag" + "os" + + "github.com/xmdhs/authlib-skin/config" +) + +var configPath string + +func init() { + flag.StringVar(&configPath, "c", "", "") + flag.Parse() +} + +func main() { + ctx := context.Background() + os.ReadFile(configPath) + + config.YamlDeCode() +} diff --git a/config/yaml.go b/config/yaml.go new file mode 100644 index 0000000..b471736 --- /dev/null +++ b/config/yaml.go @@ -0,0 +1,16 @@ +package config + +import ( + "fmt" + + "gopkg.in/yaml.v3" +) + +func YamlDeCode(b []byte) (Config, error) { + var c Config + err := yaml.Unmarshal(b, &c) + if err != nil { + return c, fmt.Errorf("YamlDeCode: %w", err) + } + return c, nil +} diff --git a/frontend/src/apis/apis.ts b/frontend/src/apis/apis.ts index 669140c..0ad2eb2 100644 --- a/frontend/src/apis/apis.ts +++ b/frontend/src/apis/apis.ts @@ -1,24 +1,21 @@ import type { tokenData, ApiUser, ApiServerInfo, YggProfile, ApiConfig, List, UserInfo, EditUser } from '@/apis/model' import { apiGet } from '@/apis/utils' -export async function login(username: string, password: string) { - const v = await fetch(import.meta.env.VITE_APIADDR + "/api/yggdrasil/authserver/authenticate", { +export async function login(email: string, password: string, captchaToken: string) { + const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/login", { method: "POST", body: JSON.stringify({ - "username": username, + "email": email, "password": password, + "CaptchaToken": captchaToken }) }) - const data = await v.json() - if (!v.ok) { - throw new Error(data?.errorMessage) - } - return data as tokenData + return await apiGet(v) } export async function register(email: string, username: string, password: string, captchaToken: string) { const v = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/user/reg", { - method: "PUT", + method: "POST", body: JSON.stringify({ "Email": email, "Password": password, @@ -119,7 +116,7 @@ export async function ListUser(page: number, token: string, email: string, name: } export async function editUser(u: EditUser, token: string, uid: string) { - const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/admin/user/" + uid,{ + const r = await fetch(import.meta.env.VITE_APIADDR + "/api/v1/admin/user/" + uid, { method: "POST", headers: { "Authorization": "Bearer " + token diff --git a/frontend/src/apis/model.ts b/frontend/src/apis/model.ts index 835fd72..516237d 100644 --- a/frontend/src/apis/model.ts +++ b/frontend/src/apis/model.ts @@ -1,9 +1,7 @@ export interface tokenData { - accessToken: string - selectedProfile: { - name: string - id: string - } + token: string + name: string + uuid: string } export interface Api { diff --git a/frontend/src/views/Login.tsx b/frontend/src/views/Login.tsx index e2052c3..db3dcfa 100644 --- a/frontend/src/views/Login.tsx +++ b/frontend/src/views/Login.tsx @@ -18,6 +18,9 @@ import { Link as RouterLink, useNavigate } from "react-router-dom"; import Loading from '@/components/Loading' import CheckInput, { refType } from '@/components/CheckInput' import useTitle from '@/hooks/useTitle'; +import CaptchaWidget from '@/components/CaptchaWidget'; +import type { refType as CaptchaWidgetRef } from '@/components/CaptchaWidget' +import { ApiErr } from '@/apis/error'; @@ -29,6 +32,8 @@ export default function SignIn() { const checkList = React.useRef>(new Map()) const navigate = useNavigate(); useTitle("登录") + const captchaRef = React.useRef(null) + const [captchaToken, setCaptchaToken] = useState(""); const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); @@ -44,17 +49,33 @@ export default function SignIn() { if (loading) return setLoading(true) - login(postData.email!, postData.password ?? ""). + login(postData.email!, postData.password ?? "", captchaToken). then(v => { if (!v) return - setToken(v.accessToken) + setToken(v.token) setUserInfo({ - uuid: v.selectedProfile.id, - name: v.selectedProfile.name, + uuid: v.uuid, + name: v.name, }) navigate("/profile") }). - catch(v => [setErr(String(v)), console.warn(v)]). + catch(v => { + captchaRef.current?.reload() + + if (v instanceof ApiErr) { + switch (v.code) { + case 6: + setErr("错误的密码") + break + case 9: + setErr("用户已被禁用") + break + } + return + } + setErr(String(v)) + console.warn(v) + }). finally(() => setLoading(false)) }; @@ -104,6 +125,7 @@ export default function SignIn() { id="password" autoComplete="current-password" /> +