Skip to content

Commit

Permalink
Create auth.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 9, 2024
1 parent b19feee commit 9a37ebf
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions projects/pi-nexus-iam/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import express, { Request, Response, NextFunction } from 'express';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { User } from '../models/user';
import { Role } from '../enums/role.enum';

const router = express.Router();

router.post('/register', async (req: Request, res: Response, next: NextFunction) => {
try {
const { username, email, password } = req.body;
const user = new User({ username, email, password, roles: [Role.USER] });
await user.save();
res.json({ message: 'User created successfully' });
} catch (error) {
next(error);
}
});

router.post('/login', async (req: Request, res: Response, next: NextFunction) => {
try {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user) {
return res.status(401).json({ error: 'Invalid email or password' });
}
const isValid = await user.comparePassword(password);
if (!isValid) {
return res.status(401).json({ error: 'Invalid email or password' });
}
const token = user.generateToken();
res.json({ token });
} catch (error) {
next(error);
}
});

router.post('/forgot-password', async (req: Request, res: Response, next: NextFunction) => {
try {
const { email } = req.body;
const user = await User.findOne({ email });
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
const token = await user.generatePasswordResetToken();
res.json({ token });
} catch (error) {
next(error);
}
});

router.post('/reset-password', async (req: Request, res: Response, next: NextFunction) => {
try {
const { token, password } = req.body;
const user = await User.findOne({ passwordResetToken: token });
if (!user) {
return res.status(404).json({ error: 'Invalid token' });
}
user.password = password;
await user.save();
res.json({ message: 'Password reset successfully' });
} catch (error) {
next(error);
}
});

export default router;

0 comments on commit 9a37ebf

Please sign in to comment.