@zvonimirsun/iszy-nuxt-auth-layer 0.1.4 → 0.1.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/app/stores/user.ts +4 -3
- package/app/utils/authEvents.ts +2 -1
- package/package.json +3 -3
- package/server/api/auth/check.get.ts +1 -1
- package/server/api/auth/login.post.ts +2 -2
- package/server/api/auth/logout.post.ts +1 -1
- package/server/api/oauth/[provider]/bind.get.ts +1 -1
- package/server/api/oauth/[provider]/callback.get.ts +2 -2
- package/server/utils/authFetch.ts +40 -36
- package/shared/types/fetcher.ts +3 -0
package/app/stores/user.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Device, ResultDto } from '@zvonimirsun/iszy-common'
|
|
2
2
|
import type { PublicSimpleUser } from '##shared/types/auth'
|
|
3
|
+
import type { Fetcher } from '##shared/types/fetcher'
|
|
3
4
|
import dayjs from 'dayjs'
|
|
4
5
|
import { acceptHMRUpdate, defineStore } from 'pinia'
|
|
5
6
|
|
|
@@ -98,7 +99,7 @@ export const useUserStore = defineStore('user', () => {
|
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
101
|
|
|
101
|
-
async function pullProfile(force?: boolean, fetcher:
|
|
102
|
+
async function pullProfile(force?: boolean, fetcher: Fetcher = $fetch) {
|
|
102
103
|
// 1. 非强制刷新且已拉取成功,直接返回
|
|
103
104
|
if (profilePulled.value && !force) {
|
|
104
105
|
return true
|
|
@@ -171,7 +172,7 @@ export const useUserStore = defineStore('user', () => {
|
|
|
171
172
|
return pullProfilePromise
|
|
172
173
|
}
|
|
173
174
|
|
|
174
|
-
async function updateProfile(data?: PublicSimpleUser, fetcher?:
|
|
175
|
+
async function updateProfile(data?: PublicSimpleUser, fetcher?: Fetcher) {
|
|
175
176
|
profile.value = data
|
|
176
177
|
authEvents.emit('profileUpdated', {
|
|
177
178
|
profile: data,
|
|
@@ -179,7 +180,7 @@ export const useUserStore = defineStore('user', () => {
|
|
|
179
180
|
})
|
|
180
181
|
}
|
|
181
182
|
|
|
182
|
-
async function removeProfile(fetcher?:
|
|
183
|
+
async function removeProfile(fetcher?: Fetcher) {
|
|
183
184
|
await updateProfile(undefined, fetcher)
|
|
184
185
|
authEvents.emit('profileRemoved', {
|
|
185
186
|
fetcher,
|
package/app/utils/authEvents.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { PublicSimpleUser } from '##shared/types/auth'
|
|
2
|
+
import type { Fetcher } from '##shared/types/fetcher'
|
|
2
3
|
import mitt from 'mitt'
|
|
3
4
|
|
|
4
5
|
export interface AuthEventPayload {
|
|
5
6
|
profile?: PublicSimpleUser
|
|
6
|
-
fetcher?:
|
|
7
|
+
fetcher?: Fetcher
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
export type AuthEvents = Record<string | symbol, unknown> & {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zvonimirsun/iszy-nuxt-auth-layer",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "ISZY Nuxt 应用共享认证 layer。",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
|
-
"
|
|
55
|
-
"typecheck": "nuxt
|
|
54
|
+
"postinstall": "nuxt prepare",
|
|
55
|
+
"typecheck": "nuxt typecheck"
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -17,7 +17,7 @@ export default defineEventHandler(async (event): Promise<ResultDto<{
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
try {
|
|
20
|
-
const res = await authFetch<ResultDto<PublicUser>>(
|
|
20
|
+
const res = await authFetch(event)<ResultDto<PublicUser>>('/user/me')
|
|
21
21
|
if (res.success && res.data) {
|
|
22
22
|
return {
|
|
23
23
|
success: true,
|
|
@@ -26,11 +26,11 @@ export default defineEventHandler(async (event): Promise<ResultDto<PublicSimpleU
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
try {
|
|
29
|
-
const res = await authFetch<ResultDto<{
|
|
29
|
+
const res = await authFetch(event)<ResultDto<{
|
|
30
30
|
access_token: string
|
|
31
31
|
refresh_token: string
|
|
32
32
|
profile: PublicUser
|
|
33
|
-
}>>(
|
|
33
|
+
}>>('/auth/login', {
|
|
34
34
|
method: 'POST',
|
|
35
35
|
body: {
|
|
36
36
|
username: body.userName.trim(),
|
|
@@ -5,7 +5,7 @@ export default defineEventHandler(async (event) => {
|
|
|
5
5
|
const { provider } = event.context.params as { provider: string }
|
|
6
6
|
const url = getRequestURL(event)
|
|
7
7
|
|
|
8
|
-
const res = await authFetch<ResultDto<string>>(
|
|
8
|
+
const res = await authFetch(event)<ResultDto<string>>('/oauth/code', {
|
|
9
9
|
method: 'POST',
|
|
10
10
|
})
|
|
11
11
|
const code = res.data!
|
|
@@ -60,11 +60,11 @@ export default defineEventHandler(async (event) => {
|
|
|
60
60
|
|
|
61
61
|
if (!errorMessage) {
|
|
62
62
|
try {
|
|
63
|
-
const res = await authFetch<ResultDto<{
|
|
63
|
+
const res = await authFetch(event)<ResultDto<{
|
|
64
64
|
access_token: string
|
|
65
65
|
refresh_token: string
|
|
66
66
|
profile: PublicUser
|
|
67
|
-
}>>(
|
|
67
|
+
}>>('/oauth/token', {
|
|
68
68
|
method: 'POST',
|
|
69
69
|
query: {
|
|
70
70
|
access_token: code,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { PublicUser, ResultDto } from '@zvonimirsun/iszy-common'
|
|
2
2
|
import type { H3Event } from 'h3'
|
|
3
|
-
import type { NitroFetchRequest, TypedInternalResponse } from 'nitropack'
|
|
4
3
|
import type { FetchError } from 'ofetch'
|
|
5
4
|
import type { SessionData } from '##shared/types/auth'
|
|
5
|
+
import type { Fetcher } from '##shared/types/fetcher'
|
|
6
6
|
import { getProxyRequestHeaders } from 'h3'
|
|
7
7
|
|
|
8
8
|
export async function proxyFetch(event: H3Event) {
|
|
@@ -10,7 +10,7 @@ export async function proxyFetch(event: H3Event) {
|
|
|
10
10
|
const { apiOrigin } = usePublicConfig()
|
|
11
11
|
const target = apiOrigin + event.path.slice(4)
|
|
12
12
|
|
|
13
|
-
const headers =
|
|
13
|
+
const headers = _getForwardRequestHeaders(event)
|
|
14
14
|
headers['accept-encoding'] = 'identity'
|
|
15
15
|
|
|
16
16
|
const body = ['GET', 'HEAD'].includes(event.method) ? undefined : await readRawBody(event)
|
|
@@ -34,59 +34,63 @@ export async function proxyFetch(event: H3Event) {
|
|
|
34
34
|
let res = await doRequest()
|
|
35
35
|
if (sessionId && res.status === 401) {
|
|
36
36
|
try {
|
|
37
|
-
useRedisSession(event, await
|
|
37
|
+
useRedisSession(event, await _refreshWithLock(sessionId, async () => _refreshToken(event)))
|
|
38
38
|
}
|
|
39
39
|
catch {
|
|
40
40
|
await destroyRedisSession(event)
|
|
41
|
-
return
|
|
41
|
+
return _pipeResponse(event, res)
|
|
42
42
|
}
|
|
43
43
|
res = await doRequest()
|
|
44
44
|
}
|
|
45
|
-
return
|
|
45
|
+
return _pipeResponse(event, res)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
export
|
|
48
|
+
export function authFetch(event: H3Event): Fetcher {
|
|
49
|
+
return createAuthFetch(event)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createAuthFetch(event: H3Event): Fetcher {
|
|
49
53
|
const sessionId = getSessionId(event)
|
|
50
54
|
const { apiOrigin } = usePublicConfig()
|
|
51
|
-
const
|
|
52
|
-
const headers: Record<string, string> = {
|
|
53
|
-
...getForwardRequestHeaders(event),
|
|
54
|
-
...(opts.headers as Record<string, string> | undefined),
|
|
55
|
-
}
|
|
55
|
+
const baseFetch = $fetch as (request: unknown, options?: Record<string, unknown>) => Promise<unknown>
|
|
56
56
|
|
|
57
|
-
opts
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
57
|
+
const authFetcher = async (url: unknown, opts: Record<string, unknown> = {}) => {
|
|
58
|
+
const headers: Record<string, string> = {
|
|
59
|
+
..._getForwardRequestHeaders(event),
|
|
60
|
+
...(opts.headers as Record<string, string> | undefined),
|
|
61
|
+
}
|
|
62
|
+
opts.headers = headers
|
|
63
|
+
opts.baseURL = apiOrigin
|
|
62
64
|
if (sessionId) {
|
|
63
65
|
const sessionData = await getRedisSession(event)
|
|
64
66
|
if (sessionData) {
|
|
65
67
|
headers.authorization = `Bearer ${sessionData.access_token}`
|
|
66
68
|
}
|
|
67
69
|
}
|
|
68
|
-
return
|
|
70
|
+
return baseFetch(url, opts)
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
73
|
+
return (async (url: unknown, opts?: Record<string, unknown>) => {
|
|
74
|
+
try {
|
|
75
|
+
return await authFetcher(url, opts)
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (sessionId && (error as FetchError)?.response?.status === 401) {
|
|
79
|
+
try {
|
|
80
|
+
useRedisSession(event, await _refreshWithLock(sessionId, async () => _refreshToken(event)))
|
|
81
|
+
return await authFetcher(url, opts)
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
await destroyRedisSession(event)
|
|
85
|
+
throw error
|
|
86
|
+
}
|
|
83
87
|
}
|
|
88
|
+
throw error
|
|
84
89
|
}
|
|
85
|
-
|
|
86
|
-
}
|
|
90
|
+
}) as Fetcher
|
|
87
91
|
}
|
|
88
92
|
|
|
89
|
-
async function
|
|
93
|
+
async function _refreshToken(event: H3Event): Promise<SessionData> {
|
|
90
94
|
const sessionData = await getRedisSession(event)
|
|
91
95
|
if (!sessionData) {
|
|
92
96
|
throw new Error('REFRESH_FAILED')
|
|
@@ -100,7 +104,7 @@ async function refreshToken(event: H3Event): Promise<SessionData> {
|
|
|
100
104
|
}>>(`${apiOrigin}/auth/refresh`, {
|
|
101
105
|
method: 'POST',
|
|
102
106
|
headers: {
|
|
103
|
-
...
|
|
107
|
+
..._getForwardRequestHeaders(event),
|
|
104
108
|
Authorization: `Bearer ${sessionData.refresh_token}`,
|
|
105
109
|
},
|
|
106
110
|
})
|
|
@@ -117,7 +121,7 @@ async function refreshToken(event: H3Event): Promise<SessionData> {
|
|
|
117
121
|
|
|
118
122
|
const locks = new Map<string, Promise<SessionData>>()
|
|
119
123
|
|
|
120
|
-
async function
|
|
124
|
+
async function _refreshWithLock(sessionId: string, fn: () => Promise<SessionData>) {
|
|
121
125
|
if (locks.has(sessionId)) {
|
|
122
126
|
return locks.get(sessionId)!
|
|
123
127
|
}
|
|
@@ -135,7 +139,7 @@ async function refreshWithLock(sessionId: string, fn: () => Promise<SessionData>
|
|
|
135
139
|
return promise
|
|
136
140
|
}
|
|
137
141
|
|
|
138
|
-
function
|
|
142
|
+
function _getForwardRequestHeaders(event: H3Event) {
|
|
139
143
|
const headers = getProxyRequestHeaders(event, { host: false })
|
|
140
144
|
const remoteAddr = getRequestIP(event, { xForwardedFor: true }) || ''
|
|
141
145
|
const xForwardedFor = headers['x-forwarded-for'] || remoteAddr
|
|
@@ -149,7 +153,7 @@ function getForwardRequestHeaders(event: H3Event) {
|
|
|
149
153
|
return headers
|
|
150
154
|
}
|
|
151
155
|
|
|
152
|
-
async function
|
|
156
|
+
async function _pipeResponse(event: H3Event, res: Response) {
|
|
153
157
|
setResponseStatus(event, res.status)
|
|
154
158
|
for (const [key, value] of res.headers) {
|
|
155
159
|
if (key.toLowerCase() === 'set-cookie') {
|