Skip to content

Not able to pass exp to sign function #25

Open
@jeremyw189

Description

@jeremyw189

It would be nice to be able to pass custom expiration times when generating new tokens (a gateway app that generates tokens for multiple APIs, for example).

Current:

if (exp)
    jwt = jwt.setExpirationTime(exp);

Suggestion:

if (exp ?? morePayload.exp)
    jwt = jwt.setExpirationTime(morePayload.exp ?? exp);

That way the provided expiration time overrides the default expiration time defined in the constructor, if provided.

Example:

import { Elysia } from 'elysia';
import { jwt } from '@elysiajs/jwt';

const app = new Elysia();

// Default JWT expiration is 1 hour.
app.use(jwt({ name: 'jwt', secret: Bun.env.MY_JWT_SECRET!, exp: '1hr' }));

app.get('/sign/:name', async (context: any) => {
  // Default, 1 hour expiration.
  context.setCookie('auth', await context.jwt.sign(context.params), {
    httpOnly: true,
  });

  return `Sign in as ${context.params.name}`;
});

app.get('/sign-longer/:name', async (context: any) => {
  // 24 hour expiration.
  context.setCookie('auth', await context.jwt.sign({ ...context.params, exp: '24hr' }), {
    httpOnly: true,
  });

  return `Sign in as ${context.params.name}`;
});

app.listen(3000);
console.log(
  `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions