mirror of
https://github.com/kou029w/_.git
synced 2025-01-31 06:18:07 +00:00
27 lines
564 B
TypeScript
27 lines
564 B
TypeScript
import { defineHooks } from './$relay'
|
|
import { getUserIdByToken } from '$/service/user'
|
|
|
|
export type AdditionalRequest = {
|
|
user: {
|
|
id: string
|
|
}
|
|
}
|
|
|
|
export default defineHooks((fastify) => ({
|
|
preHandler: fastify.auth([
|
|
(req, _, done) => {
|
|
const user =
|
|
typeof req.headers.token === 'string' &&
|
|
getUserIdByToken(req.headers.token)
|
|
|
|
if (user) {
|
|
// eslint-disable-next-line
|
|
// @ts-expect-error
|
|
req.user = user
|
|
done()
|
|
} else {
|
|
done(new Error('Unauthorized'))
|
|
}
|
|
}
|
|
])
|
|
}))
|