mirror change from https://github.com/binhkid2/FullStack-Blog-Nestjs-Nextjs-Postgres
This commit is contained in:
46
backend/src/common/helpers/jwt.helper.ts
Normal file
46
backend/src/common/helpers/jwt.helper.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import * as crypto from 'crypto';
|
||||
import * as jwt from 'jsonwebtoken';
|
||||
|
||||
export interface AccessTokenPayload {
|
||||
sub: string;
|
||||
email: string;
|
||||
role: string;
|
||||
type: 'access';
|
||||
}
|
||||
|
||||
export interface RefreshTokenPayload {
|
||||
sub: string;
|
||||
type: 'refresh';
|
||||
}
|
||||
|
||||
export function signAccessToken(
|
||||
payload: Omit<AccessTokenPayload, 'type'>,
|
||||
secret: string,
|
||||
expiresIn: string,
|
||||
): string {
|
||||
return jwt.sign({ ...payload, type: 'access' }, secret, { expiresIn } as any);
|
||||
}
|
||||
|
||||
export function signRefreshToken(
|
||||
payload: Omit<RefreshTokenPayload, 'type'>,
|
||||
secret: string,
|
||||
expiresIn: string,
|
||||
): string {
|
||||
return jwt.sign({ ...payload, type: 'refresh' }, secret, { expiresIn } as any);
|
||||
}
|
||||
|
||||
export function verifyAccessToken(token: string, secret: string): AccessTokenPayload {
|
||||
return jwt.verify(token, secret) as AccessTokenPayload;
|
||||
}
|
||||
|
||||
export function verifyRefreshToken(token: string, secret: string): RefreshTokenPayload {
|
||||
return jwt.verify(token, secret) as RefreshTokenPayload;
|
||||
}
|
||||
|
||||
export function hashToken(raw: string): string {
|
||||
return crypto.createHash('sha256').update(raw).digest('hex');
|
||||
}
|
||||
|
||||
export function generateRawToken(): string {
|
||||
return crypto.randomBytes(48).toString('hex');
|
||||
}
|
||||
45
backend/src/common/helpers/mailer.helper.ts
Normal file
45
backend/src/common/helpers/mailer.helper.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import * as nodemailer from 'nodemailer';
|
||||
|
||||
export interface MailOptions {
|
||||
to: string;
|
||||
subject: string;
|
||||
html: string;
|
||||
}
|
||||
|
||||
let transporter: nodemailer.Transporter | null = null;
|
||||
|
||||
function getTransporter(): nodemailer.Transporter {
|
||||
if (transporter) return transporter;
|
||||
|
||||
const host = process.env.SMTP_HOST;
|
||||
if (!host) {
|
||||
// Console fallback for development
|
||||
transporter = nodemailer.createTransport({ jsonTransport: true });
|
||||
return transporter;
|
||||
}
|
||||
|
||||
transporter = nodemailer.createTransport({
|
||||
host,
|
||||
port: parseInt(process.env.SMTP_PORT || '587', 10),
|
||||
auth: {
|
||||
user: process.env.SMTP_USER,
|
||||
pass: process.env.SMTP_PASS,
|
||||
},
|
||||
});
|
||||
return transporter;
|
||||
}
|
||||
|
||||
export async function sendMail(options: MailOptions): Promise<void> {
|
||||
const from = process.env.MAIL_FROM || 'no-reply@blog.local';
|
||||
const t = getTransporter();
|
||||
|
||||
if (!process.env.SMTP_HOST) {
|
||||
console.log('[MAIL CONSOLE FALLBACK]');
|
||||
console.log(` TO: ${options.to}`);
|
||||
console.log(` SUBJECT: ${options.subject}`);
|
||||
console.log(` BODY: ${options.html}`);
|
||||
return;
|
||||
}
|
||||
|
||||
await t.sendMail({ from, ...options });
|
||||
}
|
||||
14
backend/src/common/helpers/slug.helper.ts
Normal file
14
backend/src/common/helpers/slug.helper.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
export function generateSlug(title: string): string {
|
||||
const base = title
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^\w\s-]/g, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-')
|
||||
.substring(0, 200);
|
||||
|
||||
const suffix = uuidv4().split('-')[0]; // 8-char UUID segment
|
||||
return `${base}-${suffix}`;
|
||||
}
|
||||
Reference in New Issue
Block a user