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 / 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,
@antonkalik
antonkalik / forgotPasswordRouter.ts
Created March 25, 2024 23:45
Forgot Password Router
import { Router } from 'express';
import { forgotPasswordController } from 'src/controllers/forgotPasswordController';
import { resetPasswordController } from 'src/controllers/resetPasswordController';
export const forgotPasswordRouter = Router();
forgotPasswordRouter.post('/', forgotPasswordController);
forgotPasswordRouter.post('/reset/:token', resetPasswordController);
@antonkalik
antonkalik / add_field_forgot_password_token.ts
Created March 25, 2024 23:19
Forgot Password Token Migration
import type { Knex } from 'knex';
export async function up(knex: Knex): Promise<void> {
return knex.schema.alterTable('users', table => {
table.string('forgot_password_token').unique();
});
}
export async function down(knex: Knex): Promise<void> {
return knex.schema.alterTable('users', table => {
@antonkalik
antonkalik / index.ts
Created March 10, 2024 00:06
Cron job for refreshConfiguration
import cron from 'node-cron''
import { TokenService } from 'src/services/TokenService'
cron.schedule('0 * * * *', () => {
TokenService.refreshConfiguration();
}, {
scheduled: true,
timezone: "America/New_York"
});
@antonkalik
antonkalik / TokenService.ts
Created March 9, 2024 23:50
Token Service with refreshing
export class TokenService {
private static jwt_secret = process.env.JWT_SECRET!;
public static refreshConfig = () => {
this.jwt_secret = process.env.JWT_SECRET!;
if (!this.jwt_secret) {
throw new Error('JWT secret not found in environment variables!');
}
};
@antonkalik
antonkalik / TokenService.ts
Last active March 10, 2024 10:58
Token Service WIth Initialization
require('dotenv').config();
import jsonwebtoken from 'jsonwebtoken';
export class TokenService {
private static jwt_secret: string = process.env.JWT_SECRET!;
static initialize = () => {
if (!this.jwt_secret) {
throw new Error('JWT secret not found in environment variables!');
}
@antonkalik
antonkalik / TokenService.ts
Created March 9, 2024 23:31
Token service with constructor
export class TokenService {
private jwt_secret: string;
constructor() {
if (!process.env.JWT_SECRET) {
throw new Error('JWT secret not found in environment variables!');
}
this.jwt_secret = process.env.JWT_SECRET;
}
@antonkalik
antonkalik / tokenServiceHandler.ts
Created March 9, 2024 23:23
Token Service Handler
const tokenServiceHandler = {
get(target, propKey, receiver) {
const originalMethod = target[propKey];
if (typeof originalMethod === 'function') {
return function(...args) {
if (!TokenService.jwt_secret) {
throw new Error('Secret not found in environment variables!');
}
@antonkalik
antonkalik / TokenService.ts
Created March 9, 2024 23:21
Token Service without check of secret
require('dotenv').config();
import jsonwebtoken from 'jsonwebtoken';
export class TokenService {
private static jwt_secret = process.env.JWT_SECRET!;
public static verify<TokenPayload>(token: string): Promise<TokenPayload> {
return new Promise((resolve, reject) => {
jsonwebtoken.verify(token, TokenService.jwt_secret, (error, decoded) => {
if (error) {
@antonkalik
antonkalik / TokenService.ts
Created March 9, 2024 23:13
Token handling service
require('dotenv').config();
import jsonwebtoken from 'jsonwebtoken';
export class TokenService {
private static jwt_secret = process.env.JWT_SECRET!;
private static checkSecret() {
if (!TokenService.jwt_secret) {
throw new Error('JWT token not found in environment variables!');
}