@kingworldjs/jwt
v0.0.0-experimental.0
Published
Plugin for kingworld for using JWT Authentication
Downloads
2
Maintainers
Readme
@kingworldjs/static
Plugin for kingworld for using JWT Authentication.
Installation
bun add @kingworldjs/jwt
Example
import { KingWorld, t } from 'kingworld'
import { jwt } from '@kingworldjs/jwt'
import { cookie } from '@kingworldjs/cookie'
const app = new KingWorld()
.use(
jwt({
name: 'jwt',
// This should be Environment Variable
secret: 'MY_SECRETS'
})
)
.use(cookie)
.get('/sign/:name', async ({ jwt, cookie, setCookie, params }) => {
setCookie('auth', await jwt.sign(params))
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(8080)
Config
This package extends jose, most config is inherited from Jose.
Below are configurable properties for using JWT plugin
name
Name to decorate method as:
For example, jwt
will decorate Context with Context.jwt
secret
JWT secret key
schema
Type strict validation for JWT payload
Jose's config
Below is the config inherits from jose
alg
@default 'HS256'
Algorithm to sign JWT with
crit
Critical Header Parameter.
iss
JWT Issuer
sub
JWT Subject
aud
JWT Audience
jti
JWT ID
nbf
JWT Not Before
exp
JWT Expiration Time
iat
JWT Issued At