Plugin for Elysia for using JWT Authentication.
bun add @elysiajs/jwt
import { Elysia, t } from 'elysia';
import { jwt } from '@elysiajs/jwt';
import { cookie } from '@elysiajs/cookie';
const app = new Elysia()
.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), {
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(8080);
This package extends jose, most config is inherited from Jose.
Below are configurable properties for using JWT plugin
Name to decorate method as:
For example, jwt
will decorate Context with Context.jwt
JWT secret key
Type strict validation for JWT payload
Below is the config inherits from jose
@default 'HS256'
Algorithm to sign JWT with
Critical Header Parameter.
JWT Issuer
JWT Subject
JWT Audience
JWT ID
JWT Not Before
JWT Expiration Time
JWT Issued At