Skip to content

Commit

Permalink
feat: refresh_token api
Browse files Browse the repository at this point in the history
1. refresh token
  • Loading branch information
Chinlinlee committed Jan 14, 2022
1 parent e30ac2c commit 095efb7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
28 changes: 28 additions & 0 deletions api/user/controller/postRefreshToken.js
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);
}
}
10 changes: 9 additions & 1 deletion api/user/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ router.get(
router.delete(
'/token/:_id' ,
user.checkIsLoggedIn,
require('./controller/deleteToken'))
require('./controller/deleteToken'));

router.post(
'/token/refresh',
validateParams({
"refresh_token": Joi.string().required()
}, "body" , { allowUnknown: false }),
require("./controller/postRefreshToken")
)


module.exports = router;
45 changes: 45 additions & 0 deletions api/user/service/refreshTokenService.js
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
};
}
}

0 comments on commit 095efb7

Please sign in to comment.