You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
673 B
30 lines
673 B
4 years ago
|
import { getRepository } from 'typeorm';
|
||
|
import { User } from '../entity/User';
|
||
|
import { Middleware } from '../types/express';
|
||
|
|
||
|
export const checkUser: Middleware = async (req, _res, next) => {
|
||
|
if (req.session?.userId) {
|
||
|
const userRepository = getRepository(User);
|
||
|
|
||
|
const user = await userRepository.findOne({
|
||
|
where: { id: req.session.userId },
|
||
|
});
|
||
|
|
||
|
if (user) {
|
||
|
req.user = user;
|
||
|
}
|
||
|
}
|
||
|
next();
|
||
|
};
|
||
|
|
||
|
export const isAuthenticated: Middleware = async (req, res, next) => {
|
||
|
if (!req.user) {
|
||
|
res.status(403).json({
|
||
|
status: 403,
|
||
|
error: 'You do not have permisson to access this endpoint',
|
||
|
});
|
||
|
} else {
|
||
|
next();
|
||
|
}
|
||
|
};
|