From ba813ffcc825bb6dce2d19833f45709aae05daf9 Mon Sep 17 00:00:00 2001 From: brianling Date: Sat, 22 Jun 2024 12:19:52 +0800 Subject: [PATCH] feat(/api/user): Compete login-code and auth-middleware --- components/Default/Header.vue | 15 ++- composables/logout.ts | 14 +++ middleware/admin.ts | 0 middleware/auth.ts | 27 ++++++ middleware/unauth.ts | 7 ++ pages/admin/loginlogs/index.vue | 23 +++++ pages/apply/index.vue | 39 +------- pages/index.vue | 1 + pages/login/index.vue | 63 ------------- pages/user/index.vue | 5 + pages/user/login/index.vue | 111 +++++++++++++++++++++++ pages/user/logout.ts | 9 ++ pages/{ => user}/register/index.vue | 43 +++++---- prisma/schema.prisma | 9 +- server/api/admin/loginlogs.ts | 38 ++++++++ server/api/{test => reg}/code.ts | 1 + server/api/{test/reg.ts => reg/index.ts} | 4 +- server/api/{test => reg}/sms.ts | 3 + server/api/test/index.ts | 35 +------ server/api/user/auth.ts | 35 +++++++ server/api/user/create.ts | 49 +++++----- server/api/user/login.ts | 26 +++++- server/api/user/logout.ts | 15 +++ types/Log/LoginLog.ts | 6 ++ types/Log/index.d.ts | 1 + 25 files changed, 396 insertions(+), 183 deletions(-) create mode 100644 composables/logout.ts create mode 100644 middleware/admin.ts create mode 100644 middleware/auth.ts create mode 100644 middleware/unauth.ts create mode 100644 pages/admin/loginlogs/index.vue delete mode 100644 pages/login/index.vue create mode 100644 pages/user/login/index.vue create mode 100644 pages/user/logout.ts rename pages/{ => user}/register/index.vue (85%) create mode 100644 server/api/admin/loginlogs.ts rename server/api/{test => reg}/code.ts (97%) rename server/api/{test/reg.ts => reg/index.ts} (88%) rename server/api/{test => reg}/sms.ts (96%) create mode 100644 server/api/user/auth.ts create mode 100644 server/api/user/logout.ts create mode 100644 types/Log/LoginLog.ts create mode 100644 types/Log/index.d.ts diff --git a/components/Default/Header.vue b/components/Default/Header.vue index d38dc4f..3239354 100644 --- a/components/Default/Header.vue +++ b/components/Default/Header.vue @@ -24,11 +24,12 @@ 游戏服务器 --> - 论坛 - 托管 + 论坛 + 托管
- 登录 - 注册 + 登录 + 注册 + 注销 @@ -36,9 +37,13 @@ diff --git a/pages/user/logout.ts b/pages/user/logout.ts new file mode 100644 index 0000000..53ac5c8 --- /dev/null +++ b/pages/user/logout.ts @@ -0,0 +1,9 @@ +const auth = useCookie("auth"); +$fetch("/api/user/logout", { + method: "POST", + body: { + auth: auth.value, + }, +}); +auth.value = undefined; +navigateTo("/"); diff --git a/pages/register/index.vue b/pages/user/register/index.vue similarity index 85% rename from pages/register/index.vue rename to pages/user/register/index.vue index ff7ff7e..ca1f6f6 100644 --- a/pages/register/index.vue +++ b/pages/user/register/index.vue @@ -17,6 +17,7 @@ view="tabs" :model-value="form" @update:model-value="form = $event" + @success="RegSuccess" validate-on="change" style="padding: 5%" > @@ -31,18 +32,14 @@ rules="required|min:3|max:64" @input="Check()" /> - @@ -95,7 +92,6 @@ > 已阅读并同意《用户协议》 - 希望获取最新资讯 { if (!res.phone) { - phone$.value.messageBag.append("手机号已经注册"); + username$.value.messageBag.append("手机号已经注册"); canSubmit.value = true; + return 0; } else { + username$.value.messageBag.clear("errors"); canSubmit.value = false; } if (!res.username) { username$.value.messageBag.append("昵称已存在"); canSubmit.value = true; } else { + username$.value.messageBag.clear("errors"); canSubmit.value = false; } }); @@ -170,10 +169,10 @@ function Send() { Check(); if (!canSubmit.value) { - $fetch("/api/test/sms", { + $fetch("/api/reg/sms", { method: "POST", body: { - phone: form.value.phone.replace("+86", ""), + phone: form.value.phone, }, }).then((res) => { switch (res.code) { @@ -199,7 +198,7 @@ function Send() { } function CheckCode() { if (!code$.value.invalid) { - $fetch("/api/test/code", { + $fetch("/api/reg/code", { method: "POST", body: { phone: form.value.phone.replace("+86", ""), @@ -217,7 +216,7 @@ function CheckCode() { ElMessage({ message: res.msg, type: "success" }); codeBtnRef.value.disabled = true; codeBtnRef.value.text = "已验证"; - phoneChecked.value = ture; + phoneChecked.value = true; break; case 2: ElMessage({ message: res.msg, type: "warning" }); @@ -231,6 +230,14 @@ function CheckCode() { }); } } +function RegSuccess(response: { data: { code: number; msg: string } }, form$) { + if (response.data.code == 1) { + ElMessage({ message: response.data.msg, type: "success" }); + navigateTo("/user/login"); + } else { + ElMessage({ message: response.data.msg, type: "warning" }); + } +}