mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Adds a validation schema for the application's environment variables using the Zod library. The parsed environment variables are then exported as constants that can be imported throughout the application, replacing the direct use of process.env.
24 lines
655 B
TypeScript
24 lines
655 B
TypeScript
import { BasicUserInfoSchema } from '@/config/auth/types';
|
|
import jwt from 'jsonwebtoken';
|
|
import { z } from 'zod';
|
|
import { CONFIRMATION_TOKEN_SECRET } from '../env';
|
|
|
|
type User = z.infer<typeof BasicUserInfoSchema>;
|
|
|
|
export const generateConfirmationToken = (user: User) => {
|
|
const token = jwt.sign(user, CONFIRMATION_TOKEN_SECRET, { expiresIn: '30m' });
|
|
return token;
|
|
};
|
|
|
|
export const verifyConfirmationToken = (token: string) => {
|
|
const decoded = jwt.verify(token, CONFIRMATION_TOKEN_SECRET);
|
|
|
|
const parsed = BasicUserInfoSchema.safeParse(decoded);
|
|
|
|
if (!parsed.success) {
|
|
throw new Error('Invalid token');
|
|
}
|
|
|
|
return parsed.data;
|
|
};
|