@zvonimirsun/iszy-nuxt-auth-layer 0.2.3 → 0.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -47
- package/app/components/AccountSecurityPanel.vue +183 -6
- package/app/stores/user.ts +15 -2
- package/nuxt.config.ts +1 -0
- package/package.json +1 -1
- package/server/utils/authFetch.ts +3 -3
- package/server/utils/useApiOrigin.ts +4 -0
- package/server/api/auth/features.get.ts +0 -6
package/README.md
CHANGED
|
@@ -1,73 +1,36 @@
|
|
|
1
1
|
# ISZY Nuxt 认证 Layer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://nuxt.com/) [](https://deepwiki.com/ZvonimirSun/iszy-nuxt-auth-layer)
|
|
4
|
+
|
|
5
|
+
用于 ISZY Nuxt 应用的共享认证 layer,抽取前端 BFF、Redis session、登录登出、登录态检查、OAuth 回调和 SSO 账户完成等通用能力。
|
|
4
6
|
|
|
5
7
|
## 功能
|
|
6
8
|
|
|
7
9
|
- 基于 Redis 的 httpOnly session cookie。
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
- SSO 首次登录账户完成流程:可验证并绑定已有账户,或创建并绑定新账户。
|
|
12
|
-
- 通用用户 store、公开用户类型和用户信息裁剪工具。
|
|
10
|
+
- 后端 API 代理与 token 刷新。
|
|
11
|
+
- 本地登录、登出、登录态检查、OAuth 和 SSO 流程。
|
|
12
|
+
- 通用用户 store、公开认证类型和用户信息裁剪工具。
|
|
13
13
|
|
|
14
14
|
## 使用方式
|
|
15
15
|
|
|
16
|
-
安装依赖:
|
|
17
|
-
|
|
18
16
|
```bash
|
|
19
17
|
pnpm add @zvonimirsun/iszy-nuxt-auth-layer
|
|
20
18
|
```
|
|
21
19
|
|
|
22
|
-
在 Nuxt 应用中继承该 layer:
|
|
23
|
-
|
|
24
20
|
```ts
|
|
25
21
|
export default defineNuxtConfig({
|
|
26
22
|
extends: ['@zvonimirsun/iszy-nuxt-auth-layer'],
|
|
27
|
-
})
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
消费方应用需要按部署环境配置 `runtimeConfig.public.apiOrigin`、Redis 连接信息和 session cookie 选项。
|
|
31
|
-
|
|
32
|
-
## 运行时配置
|
|
33
|
-
|
|
34
|
-
```ts
|
|
35
|
-
export default defineNuxtConfig({
|
|
36
23
|
runtimeConfig: {
|
|
24
|
+
apiOrigin: '',
|
|
37
25
|
public: {
|
|
38
26
|
apiOrigin: '',
|
|
39
|
-
title: '',
|
|
40
|
-
features: {
|
|
41
|
-
publicRegister: false,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
redis: {
|
|
45
|
-
host: '',
|
|
46
|
-
port: 6379,
|
|
47
|
-
password: undefined,
|
|
48
|
-
},
|
|
49
|
-
session: {
|
|
50
|
-
cookieName: 'NUXT_SESSION_ID',
|
|
51
|
-
maxAge: '7d',
|
|
52
|
-
domain: '',
|
|
53
27
|
},
|
|
54
28
|
},
|
|
55
29
|
})
|
|
56
30
|
```
|
|
57
31
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
该 layer 只提供通用认证链路,不接管具体应用的业务权限:
|
|
61
|
-
|
|
62
|
-
- `iszy-admin` 仍应在消费方保留 admin/superadmin 鉴权和 403 页面语义。
|
|
63
|
-
- `iszy-tools-next` 仍应在消费方保留工具权限、工具目录刷新和设置页展示逻辑。
|
|
64
|
-
|
|
65
|
-
## SSO 首次登录
|
|
32
|
+
消费方应用需要按部署环境配置 API origin、Redis 连接信息和 session cookie 选项。服务端请求优先使用私有的 `apiOrigin`,未配置时回退到 `public.apiOrigin`;OAuth 浏览器跳转和客户端直连仍使用公开地址。
|
|
66
33
|
|
|
67
|
-
|
|
34
|
+
## 文档
|
|
68
35
|
|
|
69
|
-
-
|
|
70
|
-
- 选择“创建新账户”时,可以使用 SSO 建议的用户名、昵称和邮箱创建账户,并同时绑定 SSO。
|
|
71
|
-
- SSO 创建的账户始终为启用状态,不受 `publicRegister` 配置影响。
|
|
72
|
-
- 待完成凭证只保存在服务端 Redis 中;浏览器只持有独立的 opaque flow ID 与对应的 httpOnly 流程 cookie,多个并发登录流程不会相互覆盖。
|
|
73
|
-
- 当前流程不包含邮箱验证码、邀请码或头像同步。
|
|
36
|
+
详细架构、运行时配置和接入说明请查看 [DeepWiki](https://deepwiki.com/ZvonimirSun/iszy-nuxt-auth-layer)。
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import type { TableColumn } from '@nuxt/ui'
|
|
2
|
+
import type { FormSubmitEvent, TableColumn } from '@nuxt/ui'
|
|
3
3
|
import type { Device } from '@zvonimirsun/iszy-common'
|
|
4
4
|
import { RoleEnum } from '@zvonimirsun/iszy-common'
|
|
5
|
+
import * as z from 'zod'
|
|
5
6
|
import { useUserStore } from '##stores/user'
|
|
6
7
|
|
|
7
8
|
const props = defineProps<{
|
|
@@ -21,6 +22,96 @@ const showAdminLink = computed(() => {
|
|
|
21
22
|
})
|
|
22
23
|
})
|
|
23
24
|
|
|
25
|
+
/** ************** 账户资料 ***************/
|
|
26
|
+
|
|
27
|
+
const profileSaving = ref(false)
|
|
28
|
+
const passwordSaving = ref(false)
|
|
29
|
+
const profileState = reactive({
|
|
30
|
+
userName: '',
|
|
31
|
+
nickName: '',
|
|
32
|
+
email: '',
|
|
33
|
+
mobile: '',
|
|
34
|
+
})
|
|
35
|
+
const passwordState = reactive({
|
|
36
|
+
oldPasswd: '',
|
|
37
|
+
passwd: '',
|
|
38
|
+
confirmPasswd: '',
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
const profileSchema = z.object({
|
|
42
|
+
userName: z.string().trim().min(1, '请输入用户名'),
|
|
43
|
+
nickName: z.string().trim().min(1, '请输入昵称'),
|
|
44
|
+
email: z.string().trim().refine(value => !value || z.email().safeParse(value).success, '请输入有效的邮箱地址'),
|
|
45
|
+
mobile: z.string().trim().optional(),
|
|
46
|
+
})
|
|
47
|
+
const passwordSchema = z.object({
|
|
48
|
+
oldPasswd: z.string().optional(),
|
|
49
|
+
passwd: z.string().min(1, '请输入新密码'),
|
|
50
|
+
confirmPasswd: z.string().min(1, '请再次输入新密码'),
|
|
51
|
+
}).refine(data => data.passwd === data.confirmPasswd, {
|
|
52
|
+
message: '两次输入的新密码不一致',
|
|
53
|
+
path: ['confirmPasswd'],
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
type ProfileSchema = z.output<typeof profileSchema>
|
|
57
|
+
type PasswordSchema = z.output<typeof passwordSchema>
|
|
58
|
+
|
|
59
|
+
watch(
|
|
60
|
+
() => userStore.profile,
|
|
61
|
+
(profile) => {
|
|
62
|
+
profileState.userName = profile?.userName || ''
|
|
63
|
+
profileState.nickName = profile?.nickName || ''
|
|
64
|
+
profileState.email = profile?.email || ''
|
|
65
|
+
profileState.mobile = profile?.mobile || ''
|
|
66
|
+
},
|
|
67
|
+
{ immediate: true },
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
async function updateProfile(payload: FormSubmitEvent<ProfileSchema>) {
|
|
71
|
+
if (profileSaving.value) {
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
profileSaving.value = true
|
|
75
|
+
try {
|
|
76
|
+
await userStore.updateCurrentUser({
|
|
77
|
+
userName: payload.data.userName.trim(),
|
|
78
|
+
nickName: payload.data.nickName.trim(),
|
|
79
|
+
email: payload.data.email?.trim() || undefined,
|
|
80
|
+
mobile: payload.data.mobile?.trim() || undefined,
|
|
81
|
+
})
|
|
82
|
+
toast.add({ title: '账户资料已更新', color: 'success' })
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
toast.add({ title: '更新账户资料失败', description: getErrorMessage(error), color: 'error' })
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
profileSaving.value = false
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function updatePassword(payload: FormSubmitEvent<PasswordSchema>) {
|
|
93
|
+
if (passwordSaving.value) {
|
|
94
|
+
return
|
|
95
|
+
}
|
|
96
|
+
passwordSaving.value = true
|
|
97
|
+
try {
|
|
98
|
+
await userStore.updateCurrentUser({
|
|
99
|
+
oldPasswd: payload.data.oldPasswd || undefined,
|
|
100
|
+
passwd: payload.data.passwd,
|
|
101
|
+
})
|
|
102
|
+
passwordState.oldPasswd = ''
|
|
103
|
+
passwordState.passwd = ''
|
|
104
|
+
passwordState.confirmPasswd = ''
|
|
105
|
+
toast.add({ title: '登录密码已更新', color: 'success' })
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
toast.add({ title: '更新登录密码失败', description: getErrorMessage(error), color: 'error' })
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
passwordSaving.value = false
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
24
115
|
/** ************** 三方登录绑定 ***************/
|
|
25
116
|
|
|
26
117
|
type LoginProviderType = 'github' | 'linuxdo' | 'sso'
|
|
@@ -127,7 +218,7 @@ async function unbind(type: LoginProviderType) {
|
|
|
127
218
|
toast.add({ title: '解绑成功', color: 'success' })
|
|
128
219
|
}
|
|
129
220
|
catch (e) {
|
|
130
|
-
toast.add({ title: '解绑失败', description: (e
|
|
221
|
+
toast.add({ title: '解绑失败', description: getErrorMessage(e), color: 'error' })
|
|
131
222
|
}
|
|
132
223
|
}
|
|
133
224
|
|
|
@@ -205,6 +296,14 @@ async function removeDevice(options: {
|
|
|
205
296
|
}
|
|
206
297
|
catch (e) {}
|
|
207
298
|
}
|
|
299
|
+
|
|
300
|
+
function getErrorMessage(error: unknown) {
|
|
301
|
+
const normalized = error as {
|
|
302
|
+
data?: { message?: string }
|
|
303
|
+
message?: string
|
|
304
|
+
}
|
|
305
|
+
return normalized.data?.message || normalized.message || '操作失败'
|
|
306
|
+
}
|
|
208
307
|
</script>
|
|
209
308
|
|
|
210
309
|
<template>
|
|
@@ -247,9 +346,79 @@ async function removeDevice(options: {
|
|
|
247
346
|
</ULink>
|
|
248
347
|
</div>
|
|
249
348
|
<USeparator />
|
|
250
|
-
<
|
|
251
|
-
|
|
252
|
-
|
|
349
|
+
<div class="flex items-center gap-3">
|
|
350
|
+
<h3 class="text-xl text-pretty font-semibold text-highlighted">
|
|
351
|
+
账户信息
|
|
352
|
+
</h3>
|
|
353
|
+
<UModal title="编辑账户资料">
|
|
354
|
+
<UButton
|
|
355
|
+
size="sm"
|
|
356
|
+
color="neutral"
|
|
357
|
+
variant="outline"
|
|
358
|
+
icon="i-lucide:edit-3"
|
|
359
|
+
>
|
|
360
|
+
编辑资料
|
|
361
|
+
</UButton>
|
|
362
|
+
<template #body>
|
|
363
|
+
<UForm
|
|
364
|
+
:schema="profileSchema"
|
|
365
|
+
:state="profileState"
|
|
366
|
+
class="flex flex-col gap-4"
|
|
367
|
+
@submit="updateProfile"
|
|
368
|
+
>
|
|
369
|
+
<UFormField label="用户名" name="userName" required>
|
|
370
|
+
<UInput v-model="profileState.userName" class="w-full" autocomplete="username" />
|
|
371
|
+
</UFormField>
|
|
372
|
+
<UFormField label="昵称" name="nickName" required>
|
|
373
|
+
<UInput v-model="profileState.nickName" class="w-full" autocomplete="nickname" />
|
|
374
|
+
</UFormField>
|
|
375
|
+
<UFormField label="邮箱" name="email">
|
|
376
|
+
<UInput v-model="profileState.email" class="w-full" type="email" autocomplete="email" />
|
|
377
|
+
</UFormField>
|
|
378
|
+
<UFormField label="手机号" name="mobile">
|
|
379
|
+
<UInput v-model="profileState.mobile" class="w-full" autocomplete="tel" />
|
|
380
|
+
</UFormField>
|
|
381
|
+
<UButton block type="submit" :loading="profileSaving">
|
|
382
|
+
保存
|
|
383
|
+
</UButton>
|
|
384
|
+
</UForm>
|
|
385
|
+
</template>
|
|
386
|
+
</UModal>
|
|
387
|
+
<UModal title="设置或修改登录密码">
|
|
388
|
+
<UButton
|
|
389
|
+
size="sm"
|
|
390
|
+
color="neutral"
|
|
391
|
+
variant="outline"
|
|
392
|
+
icon="i-lucide:key-round"
|
|
393
|
+
>
|
|
394
|
+
设置密码
|
|
395
|
+
</UButton>
|
|
396
|
+
<template #body>
|
|
397
|
+
<UForm
|
|
398
|
+
:schema="passwordSchema"
|
|
399
|
+
:state="passwordState"
|
|
400
|
+
class="flex flex-col gap-4"
|
|
401
|
+
@submit="updatePassword"
|
|
402
|
+
>
|
|
403
|
+
<p class="text-sm text-muted">
|
|
404
|
+
已有登录密码时需要填写旧密码;历史 SSO 无密码账户可直接设置新密码。
|
|
405
|
+
</p>
|
|
406
|
+
<UFormField label="旧密码" name="oldPasswd">
|
|
407
|
+
<UInput v-model="passwordState.oldPasswd" class="w-full" type="password" autocomplete="current-password" />
|
|
408
|
+
</UFormField>
|
|
409
|
+
<UFormField label="新密码" name="passwd" required>
|
|
410
|
+
<UInput v-model="passwordState.passwd" class="w-full" type="password" autocomplete="new-password" />
|
|
411
|
+
</UFormField>
|
|
412
|
+
<UFormField label="确认新密码" name="confirmPasswd" required>
|
|
413
|
+
<UInput v-model="passwordState.confirmPasswd" class="w-full" type="password" autocomplete="new-password" />
|
|
414
|
+
</UFormField>
|
|
415
|
+
<UButton block type="submit" :loading="passwordSaving">
|
|
416
|
+
保存
|
|
417
|
+
</UButton>
|
|
418
|
+
</UForm>
|
|
419
|
+
</template>
|
|
420
|
+
</UModal>
|
|
421
|
+
</div>
|
|
253
422
|
<div class="flex flex-col items-start gap-2">
|
|
254
423
|
<div class="flex items-center gap-2">
|
|
255
424
|
<div class="w-20 text-right">
|
|
@@ -272,7 +441,15 @@ async function removeDevice(options: {
|
|
|
272
441
|
邮箱:
|
|
273
442
|
</div>
|
|
274
443
|
<div>
|
|
275
|
-
{{ userStore.profile.email }}
|
|
444
|
+
{{ userStore.profile.email || '-' }}
|
|
445
|
+
</div>
|
|
446
|
+
</div>
|
|
447
|
+
<div class="flex items-center gap-2">
|
|
448
|
+
<div class="w-20 text-right">
|
|
449
|
+
手机号:
|
|
450
|
+
</div>
|
|
451
|
+
<div>
|
|
452
|
+
{{ userStore.profile.mobile || '-' }}
|
|
276
453
|
</div>
|
|
277
454
|
</div>
|
|
278
455
|
<div class="flex items-center gap-2">
|
package/app/stores/user.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Device, ResultDto } from '@zvonimirsun/iszy-common'
|
|
2
|
-
import type { AuthFeatures, PublicSimpleUser } from '##shared/types/auth'
|
|
1
|
+
import type { Device, PublicUser, ResultDto } from '@zvonimirsun/iszy-common'
|
|
2
|
+
import type { AuthFeatures, PublicSimpleUser, UpdateUser } from '##shared/types/auth'
|
|
3
3
|
import type { Fetcher } from '##shared/types/fetcher'
|
|
4
4
|
import { acceptHMRUpdate, defineStore } from 'pinia'
|
|
5
5
|
|
|
@@ -237,6 +237,18 @@ export const useUserStore = defineStore('user', () => {
|
|
|
237
237
|
await pullProfile(true)
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
+
async function updateCurrentUser(profile: UpdateUser) {
|
|
241
|
+
const res = await $fetch<ResultDto<PublicUser>>('/api/user/me', {
|
|
242
|
+
method: 'PUT',
|
|
243
|
+
body: profile,
|
|
244
|
+
})
|
|
245
|
+
if (!res.success || !res.data) {
|
|
246
|
+
throw new Error(res.message || '更新账户信息失败')
|
|
247
|
+
}
|
|
248
|
+
await updateProfile(res.data)
|
|
249
|
+
return res.data
|
|
250
|
+
}
|
|
251
|
+
|
|
240
252
|
async function getDevices(): Promise<(Device & {
|
|
241
253
|
createTime?: string
|
|
242
254
|
lastLoginTime?: string
|
|
@@ -290,6 +302,7 @@ export const useUserStore = defineStore('user', () => {
|
|
|
290
302
|
pullProfile,
|
|
291
303
|
updateProfile,
|
|
292
304
|
removeProfile,
|
|
305
|
+
updateCurrentUser,
|
|
293
306
|
thirdPartyUnbind,
|
|
294
307
|
getDevices,
|
|
295
308
|
removeDevice,
|
package/nuxt.config.ts
CHANGED
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@ import { getProxyRequestHeaders } from 'h3'
|
|
|
7
7
|
|
|
8
8
|
export async function proxyFetch(event: H3Event) {
|
|
9
9
|
const sessionId = getSessionId(event)
|
|
10
|
-
const
|
|
10
|
+
const apiOrigin = useApiOrigin()
|
|
11
11
|
const target = apiOrigin + event.path.slice(4)
|
|
12
12
|
|
|
13
13
|
const headers = _getForwardRequestHeaders(event)
|
|
@@ -51,7 +51,7 @@ export function authFetch(event: H3Event): Fetcher {
|
|
|
51
51
|
|
|
52
52
|
function createAuthFetch(event: H3Event): Fetcher {
|
|
53
53
|
const sessionId = getSessionId(event)
|
|
54
|
-
const
|
|
54
|
+
const apiOrigin = useApiOrigin()
|
|
55
55
|
const baseFetch = $fetch as (request: unknown, options?: Record<string, unknown>) => Promise<unknown>
|
|
56
56
|
|
|
57
57
|
const authFetcher = async (url: unknown, opts: Record<string, unknown> = {}) => {
|
|
@@ -96,7 +96,7 @@ async function _refreshToken(event: H3Event): Promise<SessionData> {
|
|
|
96
96
|
throw new Error('REFRESH_FAILED')
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
const
|
|
99
|
+
const apiOrigin = useApiOrigin()
|
|
100
100
|
const res = await $fetch<ResultDto<{
|
|
101
101
|
access_token: string
|
|
102
102
|
refresh_token: string
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { ResultDto } from '@zvonimirsun/iszy-common'
|
|
2
|
-
import type { AuthFeatures } from '##shared/types/auth'
|
|
3
|
-
|
|
4
|
-
export default defineEventHandler(async (event): Promise<ResultDto<AuthFeatures>> => {
|
|
5
|
-
return await authFetch(event)<ResultDto<AuthFeatures>>('/auth/features')
|
|
6
|
-
})
|