-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.js
29 lines (22 loc) · 789 Bytes
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const jwt = require('jsonwebtoken')
const { customError } = require('./wrap')
// const mongoose = require('mongoose')
require('dotenv').config()
function signUser(user) {
return jwt.sign(user, process.env.tokenSecret, { expiresIn: 60*30}) // 60detik * 30 = 30 menit
}
async function authenticateToken(req,res,next) {
const authHeader = req.headers['authorization']
const token = authHeader && authHeader.split(' ')[1]
console.log(token)
if (token == null) return next(customError('Authentication tidak ditemukan',401))
jwt.verify(token, process.env.tokenSecret, (err,user) => {
if (err) return next(customError('Authentication has expired',419))
req.user = user
next()
})
}
module.exports = {
signUser,
authenticateToken
}