-first commit
This commit is contained in:
14
exthernal-login-api/src/middlewares/auth.js
Normal file
14
exthernal-login-api/src/middlewares/auth.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import { verifyToken } from '../utils/token.js'
|
||||
import { sendError } from '../utils/response.js'
|
||||
|
||||
export function authMiddleware(req, res, next) {
|
||||
const authHeader = req.headers['authorization']
|
||||
const token = authHeader && authHeader.split(' ')[1]
|
||||
if (!token) return sendError('ไม่พบ Token', 'Missing token', 401)
|
||||
|
||||
const decoded = verifyToken(token)
|
||||
if (!decoded) return sendError('Token ไม่ถูกต้อง', 'Invalid token', 403)
|
||||
|
||||
req.user = decoded
|
||||
next()
|
||||
}
|
||||
Reference in New Issue
Block a user