Quick Start
Install dependencies
bash
npm install @duplojs/json-web-token@0 @duplojs/utils@1bash
yarn add @duplojs/json-web-token@0 @duplojs/utils@1bash
pnpm add @duplojs/json-web-token@0 @duplojs/utils@1bash
bun add @duplojs/json-web-token@0 @duplojs/utils@1bash
deno add npm:@duplojs/json-web-token@0 npm:@duplojs/utils@1Quick usage
ts
import { asyncPipe, D, DPE, E } from "@duplojs/utils";
import { Signer, createTokenHandler } from "@duplojs/json-web-token";
const tokenHandler = createTokenHandler({
maxAge: D.createTime(15, "minute"),
signer: Signer.createHS256({
secret: "my-secret",
}),
customPayloadShape: {
userId: DPE.string(),
},
});
const token = await tokenHandler.createOrThrow({
userId: "1",
});
// send to client ...
const result = await asyncPipe(
"receive-token",
tokenHandler.verify,
E.whenIsRight(
({ payload }) => {
const userId = payload.userId;
},
),
);createOrThrow returns the token string directly. verify returns an either result, so a received token can be rejected without throwing for normal authentication failures.
