This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cron from 'node-cron'' | |
import { TokenService } from 'src/services/TokenService' | |
cron.schedule('0 * * * *', () => { | |
TokenService.refreshConfiguration(); | |
}, { | |
scheduled: true, | |
timezone: "America/New_York" | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
} |