Skip to content

Instantly share code, notes, and snippets.

View antonkalik's full-sized avatar
💻
coding...

Anton Kalik antonkalik

💻
coding...
View GitHub Profile
@antonkalik
antonkalik / resetPasswordController.ts
Created March 27, 2024 21:54
Reset Password Controller
import bcrypt from 'bcrypt';
import { Request, Response } from 'express';
import { TokenService } from 'src/services/TokenService';
import { UserModel } from 'src/models/UserModel';
import type { User } from 'src/@types';
export const resetPasswordController = async (req: Request, res: Response) => {
try {
const token = req.params.token;
@antonkalik
antonkalik / generateAttachments.ts
Created March 27, 2024 21:19
Generate Attachements
import path from 'path';
import { Extension } from 'src/@types/enums';
type AttachmentFile = {
name: string;
ext?: Extension;
cid?: string;
};
export const generateAttachments = (files: AttachmentFile[] = []) =>
@antonkalik
antonkalik / generateTemplate.ts
Created March 27, 2024 21:15
Generate Template Helper
import path from 'path';
import fs from 'fs';
import handlebars from 'handlebars';
export const generateTemplate = <T>(name: string, props: T): string => {
const templatePath = path.join(__dirname, '..', 'src/templates', `${name}.hbs`);
const templateSource = fs.readFileSync(templatePath, 'utf8');
const template = handlebars.compile(templateSource);
return template(props);
};
@antonkalik
antonkalik / passwordResetTemplate.hbs
Created March 27, 2024 21:14
Password Reset Template
<!-- passwordResetTemplate.hbs -->
<html lang='en'>
<head>
<style>
a { color: #372aff; } .token { font-weight: bold; }
</style>
<title>Forgot Password</title>
</head>
<body>
<p>You requested a password reset. Please use the following link to reset your password:</p>
@antonkalik
antonkalik / getHost.ts
Created March 27, 2024 21:10
Get Host
import * as dotenv from 'dotenv';
import process from 'process';
dotenv.config();
export const getHost = (): string => {
const isProduction = process.env.NODE_ENV === 'production';
const protocol = isProduction ? 'https' : 'http';
const port = isProduction ? '' : `:${process.env.CLIENT_PORT}`;
return `${protocol}://${process.env.WEB_HOST}${port}`;
@antonkalik
antonkalik / EmailService.ts
Created March 27, 2024 21:05
Email Service sendPasswordResetEmail method
import process from 'process';
import * as nodemailer from 'nodemailer';
import * as dotenv from 'dotenv';
import { generateAttachments } from 'src/helpers/generateAttachments';
import { generateTemplate } from 'src/helpers/generateTemplate';
import { getHost } from 'src/helpers/getHost';
dotenv.config();
export class EmailService {
@antonkalik
antonkalik / EmailService.ts
Created March 27, 2024 20:53
Email Service with initialization
import process from 'process';
import * as nodemailer from 'nodemailer';
import * as dotenv from 'dotenv';
dotenv.config();
export class EmailService {
private static transporter: nodemailer.Transporter;
private static env = {
USER: process.env.MAIL_USER,
@antonkalik
antonkalik / initialize.ts
Created March 27, 2024 20:51
Initializing services
import { TokenService } from 'src/services/TokenService';
import { RedisService } from 'src/services/RedisService';
import { EmailService } from 'src/services/EmailService';
export const initialize = async () => {
await RedisService.initialize();
TokenService.initialize();
EmailService.initialize();
};
@antonkalik
antonkalik / EmailService.ts
Last active March 27, 2024 20:13
Email Service
import process from 'process';
import * as nodemailer from 'nodemailer';
import * as dotenv from 'dotenv';
dotenv.config();
export class EmailService {
private static transporter: nodemailer.Transporter;
private static env = {
USER: process.env.MAIL_USER,
@antonkalik
antonkalik / forgotPasswordController.ts
Created March 26, 2024 21:01
Forgot Password Controller
import { Request, Response } from 'express';
import { UserModel } from 'src/models/UserModel';
import type { User } from 'src/@types';
import { TokenService } from 'src/services/TokenService';
import { EmailService } from 'src/services/EmailService';
export const forgotPasswordController = async (req: Request, res: Response) => {
try {
const {
email,