@zvonimirsun/iszy-nuxt-auth-layer 0.2.4 → 0.2.6

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 CHANGED
@@ -1,73 +1,36 @@
1
1
  # ISZY Nuxt 认证 Layer
2
2
 
3
- 用于 ISZY Nuxt 应用的共享认证 layer,抽取前端 BFF、Redis session、登录登出、认证检查和 OAuth 回调等通用能力。
3
+ [![Nuxt 4.x](https://img.shields.io/badge/Nuxt-4.x-brightgreen)](https://nuxt.com/) [![Ask DeepWiki](https://deepwiki.com/badge.svg)](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
- - session 轮换与短 TTL tombstone,降低并发刷新 token 时的竞态影响。
9
- - 后端 API 代理工具,自动注入 access token,并在 401 时尝试 refresh token。
10
- - 本地登录、登出、登录态检查和 OAuth 回调路由。
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
- 当 SSO subject 尚未关联本地用户时,回调不会直接创建账户,而是进入短期的账户完成流程:
34
+ ## 文档
68
35
 
69
- - 选择“绑定已有账户”时,通过用户名或 SSO 建议邮箱及密码验证本地账户,验证成功后绑定 SSO。
70
- - 选择“创建新账户”时,可以使用 SSO 建议的用户名、昵称和邮箱创建账户,并同时绑定 SSO。
71
- - SSO 创建的账户始终为启用状态,不受 `publicRegister` 配置影响。
72
- - 待完成凭证只保存在服务端 Redis 中;浏览器只持有独立的 opaque flow ID 与对应的 httpOnly 流程 cookie,多个并发登录流程不会相互覆盖。
73
- - 当前流程不包含邮箱验证码、邀请码或头像同步。
36
+ 详细架构、运行时配置和接入说明请查看 [DeepWiki](https://deepwiki.com/ZvonimirSun/iszy-nuxt-auth-layer)。
@@ -209,7 +209,7 @@ async function bindCallback(e: MessageEvent<{
209
209
  }
210
210
  }
211
211
 
212
- async function unbind(type: LoginProviderType) {
212
+ async function unbind(type: Exclude<LoginProviderType, 'sso'>) {
213
213
  if (!isThirdPartyBound(type)) {
214
214
  return
215
215
  }
@@ -401,7 +401,7 @@ function getErrorMessage(error: unknown) {
401
401
  @submit="updatePassword"
402
402
  >
403
403
  <p class="text-sm text-muted">
404
- 已有登录密码时需要填写旧密码;历史 SSO 无密码账户可直接设置新密码。
404
+ 已有登录密码时需要填写旧密码;无密码账户可直接设置新密码。
405
405
  </p>
406
406
  <UFormField label="旧密码" name="oldPasswd">
407
407
  <UInput v-model="passwordState.oldPasswd" class="w-full" type="password" autocomplete="current-password" />
@@ -488,9 +488,9 @@ function getErrorMessage(error: unknown) {
488
488
  <div class="flex gap-2 items-center">
489
489
  <UIcon :name="ssoProvider.icon" class="size-5" />
490
490
  </div>
491
- <UButton v-if="isThirdPartyBound(ssoProvider.type)" variant="link" class="p-0 cursor-pointer" @click="unbind(ssoProvider.type)">
492
- 解绑
493
- </UButton>
491
+ <UBadge v-if="isThirdPartyBound(ssoProvider.type)" color="success" variant="subtle">
492
+ 已绑定
493
+ </UBadge>
494
494
  <UButton v-else variant="link" class="p-0 cursor-pointer" @click="bind(ssoProvider.type, ssoProvider.title)">
495
495
  绑定
496
496
  </UButton>
@@ -70,7 +70,7 @@ const createSchema = z.object({
70
70
  userName: z.string().trim().min(1, '请输入用户名'),
71
71
  nickName: z.string().trim().min(1, '请输入昵称'),
72
72
  email: z.string().trim().refine(value => !value || z.email().safeParse(value).success, '请输入有效的邮箱地址'),
73
- passwd: z.string().min(1, '请输入密码'),
73
+ passwd: z.string().optional(),
74
74
  })
75
75
 
76
76
  type BindSchema = z.output<typeof bindSchema>
@@ -275,8 +275,8 @@ function getErrorMessage(error: unknown, fallback: string) {
275
275
  <UFormField label="邮箱" name="email">
276
276
  <UInput v-model="createState.email" class="w-full" type="email" autocomplete="email" placeholder="请输入邮箱(可选)" />
277
277
  </UFormField>
278
- <UFormField label="密码" name="passwd" required>
279
- <UInput v-model="createState.passwd" class="w-full" type="password" autocomplete="new-password" placeholder="请输入密码" />
278
+ <UFormField label="密码(可选)" name="passwd" hint="不设置时仅可通过 SSO 登录">
279
+ <UInput v-model="createState.passwd" class="w-full" type="password" autocomplete="new-password" placeholder="可稍后在账户设置中添加" />
280
280
  </UFormField>
281
281
  <UAlert v-if="submitError" color="error" icon="i-lucide:info" :title="submitError" />
282
282
  <div class="grid grid-cols-2 gap-3">
package/nuxt.config.ts CHANGED
@@ -22,6 +22,7 @@ export default defineNuxtConfig({
22
22
  },
23
23
  compatibilityDate: '2026-06-30',
24
24
  runtimeConfig: {
25
+ apiOrigin: '',
25
26
  public: {
26
27
  title: '',
27
28
  apiOrigin: '',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zvonimirsun/iszy-nuxt-auth-layer",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "description": "ISZY Nuxt 应用共享认证 layer。",
@@ -14,10 +14,10 @@ export default defineEventHandler(async (event): Promise<ResultDto<PublicSimpleU
14
14
  const nickName = body?.nickName?.trim()
15
15
  const email = body?.email?.trim() || undefined
16
16
 
17
- if (!userName || !nickName || !body?.passwd) {
17
+ if (!userName || !nickName) {
18
18
  throw createError({
19
19
  statusCode: 400,
20
- message: '用户名、昵称和密码不能为空',
20
+ message: '用户名和昵称不能为空',
21
21
  })
22
22
  }
23
23
 
@@ -30,7 +30,7 @@ export default defineEventHandler(async (event): Promise<ResultDto<PublicSimpleU
30
30
  userName,
31
31
  nickName,
32
32
  email,
33
- passwd: body.passwd,
33
+ passwd: body.passwd || undefined,
34
34
  },
35
35
  })
36
36
  }
@@ -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 { apiOrigin } = usePublicConfig()
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 { apiOrigin } = usePublicConfig()
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 { apiOrigin } = usePublicConfig()
99
+ const apiOrigin = useApiOrigin()
100
100
  const res = await $fetch<ResultDto<{
101
101
  access_token: string
102
102
  refresh_token: string
@@ -0,0 +1,4 @@
1
+ export function useApiOrigin() {
2
+ const config = useRuntimeConfig()
3
+ return config.apiOrigin || config.public.apiOrigin
4
+ }
@@ -41,7 +41,7 @@ export interface SsoCompletionCreateBody {
41
41
  userName: string
42
42
  nickName: string
43
43
  email?: string
44
- passwd: string
44
+ passwd?: string
45
45
  }
46
46
 
47
47
  export interface AuthTokenData {