-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
const jwt = require('jsonwebtoken'); | ||
const uuid = require('uuid'); | ||
const UIDGenerator = require('uid-generator'); | ||
const uidGenerator = new UIDGenerator(256); | ||
const mongodb = require('../../../models/mongodb'); | ||
const refreshTokenService = require('../service/refreshTokenService'); | ||
|
||
/** | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
module.exports = async function (req , res) { | ||
try { | ||
let refreshToken = req.body.refresh_token; | ||
let tokenObj = await refreshTokenService(refreshToken); | ||
if (tokenObj.status) { | ||
return res.status(tokenObj.code).send({ | ||
token: tokenObj.data.id, | ||
refresh_token: tokenObj.data.refresh_token | ||
}); | ||
} | ||
return res.status(tokenObj.code).send(tokenObj.data); | ||
} catch(err) { | ||
console.error(err); | ||
return res.status(500).json(err); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const uuid = require('uuid'); | ||
const UIDGenerator = require('uid-generator'); | ||
const uidGenerator = new UIDGenerator(256); | ||
const mongodb = require('../../../models/mongodb'); | ||
|
||
module.exports = async function (refresh_token,expiresIn='1y') { | ||
try { | ||
let hitTokenObj = await mongodb.issuedToken.findOne({ | ||
refresh_token: refresh_token | ||
}); | ||
if (!hitTokenObj) { | ||
return { | ||
status: false, | ||
code: 404, | ||
data: "Not found with refresh token" | ||
} | ||
} | ||
let decodedTokenObj = jwt.decode(hitTokenObj.token, { complete:true }); | ||
let id = await uidGenerator.generate(); | ||
let refresh_token = await uidGenerator.generate(); | ||
let token = jwt.sign(decodedTokenObj, process.env.JWT_SECRET_KEY , {expiresIn: expiresIn, algorithm: "HS256"}); | ||
|
||
let tokenObj = { | ||
token: token, | ||
id: `Bearer ${id}`, | ||
refresh_token: refresh_token | ||
} | ||
await mongodb.issuedToken.findOneAndUpdate({ | ||
refresh_token: refresh_token | ||
} , tokenObj); | ||
return { | ||
status : true, | ||
code: 200, | ||
data: tokenObj | ||
}; | ||
} catch(e) { | ||
console.error(e); | ||
return { | ||
status: false, | ||
code : 500, | ||
data: e | ||
}; | ||
} | ||
} |