mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
37 lines
No EOL
1,001 B
JavaScript
37 lines
No EOL
1,001 B
JavaScript
const jwt = require('jsonwebtoken')
|
|
const dotenv = require('dotenv')
|
|
const AppError = require('./AppError.js');
|
|
const { UNAUTHORIZED_NO_TOKEN_GIVEN, UNAUTHORIZED_INVALID_TOKEN } = require('../constants/errorCodes.js');
|
|
|
|
dotenv.config();
|
|
|
|
class Token {
|
|
|
|
create(email, userId) {
|
|
return jwt.sign({ email, userId }, process.env.JWT_SECRET);
|
|
}
|
|
|
|
authenticate(req, res, next) {
|
|
try {
|
|
const token = req.header('Authorization') && req.header('Authorization').split(' ')[1];
|
|
if (!token) {
|
|
throw new AppError(UNAUTHORIZED_NO_TOKEN_GIVEN);
|
|
}
|
|
|
|
jwt.verify(token, process.env.JWT_SECRET, (error, payload) => {
|
|
if (error) {
|
|
throw new AppError(UNAUTHORIZED_INVALID_TOKEN)
|
|
}
|
|
|
|
req.user = payload;
|
|
});
|
|
|
|
} catch (error) {
|
|
return next(error);
|
|
}
|
|
|
|
return next();
|
|
}
|
|
}
|
|
|
|
module.exports = new Token(); |