Auth: added
This commit is contained in:
@@ -6,7 +6,7 @@ import User from '../models/User' // uncomment this
|
||||
|
||||
export function requireAuth(req: Request, res: Response, next: NextFunction) {
|
||||
const token = req.cookies.jwt
|
||||
new Err(500, 'uncomment code in authMiddleware before using!')
|
||||
//new Err(500, 'uncomment code in authMiddleware before using!')
|
||||
if (token) {
|
||||
jwt.verify(token, env.JWT_SECRET, async (err: any, decodedToken: any) => {
|
||||
if (err) {
|
||||
@@ -14,25 +14,16 @@ export function requireAuth(req: Request, res: Response, next: NextFunction) {
|
||||
res.status(401).json(new Err(401, 'user is not authenticated'))
|
||||
}
|
||||
if (!err) {
|
||||
const user = (async () => {
|
||||
if (env.NORK.db.orm) {
|
||||
if (env.NORK.db.orm == 'sequelize') {
|
||||
return await User.findByPk(decodedToken.id)
|
||||
}
|
||||
if (env.NORK.db.orm == 'mongoose') {
|
||||
return await User.findById(decodedToken.id)
|
||||
}
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
})()
|
||||
|
||||
const user = await User.findByPk(decodedToken.id)
|
||||
console.log('TADY', user)
|
||||
if (user === null) {
|
||||
console.log('1')
|
||||
res.status(401).json(new Err(401, 'user is not authenticated'))
|
||||
return
|
||||
}
|
||||
|
||||
res.locals.user = user
|
||||
console.log('2')
|
||||
new Succ(100, 'user is authenticated')
|
||||
next()
|
||||
}
|
||||
@@ -40,6 +31,7 @@ export function requireAuth(req: Request, res: Response, next: NextFunction) {
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
console.log('3')
|
||||
res.status(401).json(new Err(401, 'user is not authenticated'))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user