fix: extend request to include user
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Router } from 'express';
|
import { Router, Request, Response } from 'express';
|
||||||
import { hashPassword, comparePassword } from '../utils/auth';
|
import { hashPassword, comparePassword } from '../utils/auth';
|
||||||
import User from '../models/User';
|
import User from '../models/User';
|
||||||
import bcrypt from 'bcrypt';
|
import bcrypt from 'bcrypt';
|
||||||
@@ -6,6 +6,10 @@ import jwt from 'jsonwebtoken';
|
|||||||
// Todo: DB
|
// Todo: DB
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
|
||||||
|
interface AuthenticatedRequest extends Request {
|
||||||
|
user?: { id: string };
|
||||||
|
}
|
||||||
|
|
||||||
router.post('/register', async (req, res) => {
|
router.post('/register', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { email, password } = req.body
|
const { email, password } = req.body
|
||||||
@@ -63,8 +67,11 @@ router.get('/logout', async (req, res) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
router.get('/current-user', async (req, res) => {
|
router.get('/current-user', async (req: AuthenticatedRequest, res) => {
|
||||||
try {
|
try {
|
||||||
|
if (!req.user) {
|
||||||
|
return res.status(401).send('Unauthorized');
|
||||||
|
}
|
||||||
const user = await User.findByPk(req.user.id, {
|
const user = await User.findByPk(req.user.id, {
|
||||||
attributes: { exclude: ['password'] },
|
attributes: { exclude: ['password'] },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user