userToJwtPayload (fastify/jwt.ts)

typedai · src/fastify/jwt.ts · typescript · MIT
auth

Module exposing: userToJwtPayload, getPayloadUserId

// Source: https://github.com/TrafficGuard/typedai | License: MIT | Copyright Daniel Campagnoli
// Provided via justclaw.it.com; keep this notice with the code.
import type { User } from '#shared/user/user.model';

export interface JWTPayload {
	userId: string;
	email: string;
}

// Helper to convert User to JWT payload
export function userToJwtPayload(user: User): JWTPayload {
	return {
		userId: user.id,
		email: user.email,
	};
}

// Helper to extract userId from JWT verify payload
export function getPayloadUserId(payload: any): string {
	if (typeof payload === 'string') {
		return payload; // Handle string payload case
	}
	return payload.userId;
}

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key