mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 20:13:49 +00:00
feat: create confirm user page, option to resend email if link expires
This commit is contained in:
@@ -2,22 +2,31 @@ import { BasicUserInfoSchema } from '@/config/auth/types';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { z } from 'zod';
|
||||
import { CONFIRMATION_TOKEN_SECRET } from '../env';
|
||||
import ServerError from '../util/ServerError';
|
||||
|
||||
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 generateConfirmationToken = (user: z.infer<typeof BasicUserInfoSchema>) => {
|
||||
return jwt.sign(user, CONFIRMATION_TOKEN_SECRET, { expiresIn: '3m' });
|
||||
};
|
||||
|
||||
export const verifyConfirmationToken = (token: string) => {
|
||||
const decoded = jwt.verify(token, CONFIRMATION_TOKEN_SECRET);
|
||||
export const verifyConfirmationToken = async (token: string) => {
|
||||
try {
|
||||
const decoded = jwt.verify(token, CONFIRMATION_TOKEN_SECRET);
|
||||
|
||||
const parsed = BasicUserInfoSchema.safeParse(decoded);
|
||||
const parsed = BasicUserInfoSchema.safeParse(decoded);
|
||||
|
||||
if (!parsed.success) {
|
||||
throw new Error('Invalid token');
|
||||
if (!parsed.success) {
|
||||
throw new Error('Invalid token');
|
||||
}
|
||||
|
||||
return parsed.data;
|
||||
} catch (error) {
|
||||
if (error instanceof Error && error.message === 'jwt expired') {
|
||||
throw new ServerError(
|
||||
'Your confirmation token is expired. Please generate a new one.',
|
||||
401,
|
||||
);
|
||||
}
|
||||
|
||||
throw new ServerError('Something went wrong', 500);
|
||||
}
|
||||
|
||||
return parsed.data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user