Skip to content

Instantly share code, notes, and snippets.

@antonkalik
Created March 26, 2024 21:01
Show Gist options
  • Save antonkalik/6eea5e6d275e574e8f189f1b2b82fe6e to your computer and use it in GitHub Desktop.
Save antonkalik/6eea5e6d275e574e8f189f1b2b82fe6e to your computer and use it in GitHub Desktop.
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,
}: {
email: string;
} = req.body;
const user = await UserModel.findByEmail(email);
if (user) {
const token = await TokenService.sign(
{
id: user.id,
},
{
expiresIn: '1 day',
}
);
await user.context.update({ forgot_password_token: token });
await EmailService.sendPasswordResetEmail(email, token);
}
return res.sendStatus(200);
} catch (error) {
return res.sendStatus(500);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment