1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-31 14:28:04 +00:00
_/frourio/server/api/user/hooks.ts

26 lines
535 B
TypeScript
Raw Normal View History

2020-10-12 13:35:33 +09:00
import { defineHooks } from './$relay'
import { getUserIdByToken } from '$/service/user'
export type 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'))
}
}
])
}))