@elysia/jwt (英语)
爱莉西亚 集成 JSON Web Tokens (JWT) 的插件.
安装
bun add @elysia/jwt
示例
import { Elysia, t } from 'elysia'
import { jwt } from '@elysia/jwt'
const app = new Elysia()
.use(
jwt({
name: 'jwt',
// This should be Environment Variable
secret: 'MY_SECRETS'
})
)
.get('/sign/:name', async ({ jwt, cookie: { auth }, params }) => {
auth.set({
value: await jwt.sign(params),
httpOnly: true
})
return `Sign in as ${params.name}`
})
.get('/profile', async ({ jwt, set, cookie: { auth } }) => {
const profile = await jwt.verify(auth)
if (!profile) {
set.status = 401
return 'Unauthorized'
}
return `Hello ${profile.name}`
})
.listen(3000)
见 文档 更多细节.