feat:移除了弹窗,服务器添加sls
This commit is contained in:
36
apps/telemetry-dashboard/server/middleware/admin-auth.ts
Normal file
36
apps/telemetry-dashboard/server/middleware/admin-auth.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { getHeader } from 'h3'
|
||||
|
||||
import { toAdminHttpError } from '../utils/admin-http-error'
|
||||
import {
|
||||
accessSettings,
|
||||
authenticateAccessToken,
|
||||
mockAuthEnabled,
|
||||
mockScenario,
|
||||
mockSession,
|
||||
} from '../utils/auth'
|
||||
import { AdminApiError, unauthorized, unavailable } from '../utils/errors'
|
||||
import type { AdminEventContext } from '../utils/service'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
if (!event.path.startsWith('/api/admin/')) return
|
||||
try {
|
||||
const config = useRuntimeConfig(event)
|
||||
if (mockAuthEnabled(config)) {
|
||||
;(event.context as AdminEventContext).adminSession = mockSession(mockScenario(config))
|
||||
return
|
||||
}
|
||||
const settings = accessSettings(config)
|
||||
if (!settings) {
|
||||
if (process.env.NODE_ENV === 'production') throw unavailable()
|
||||
throw unauthorized()
|
||||
}
|
||||
const token = getHeader(event, 'cf-access-jwt-assertion')
|
||||
;(event.context as AdminEventContext).adminSession = await authenticateAccessToken(
|
||||
token,
|
||||
settings,
|
||||
)
|
||||
} catch (error) {
|
||||
if (error instanceof AdminApiError) throw toAdminHttpError(error)
|
||||
throw error
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user