Skip to content

Commit

Permalink
Merge pull request #33 from hirushaph/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
hirushaph authored Mar 15, 2024
2 parents 810b109 + 84e55fc commit d6acc56
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ jobs:
- run: npm test
env:
MONGODB_URL_TEST: ${{ secrets.MONGODB_URL_TEST }}
JWT_ACCESS_SECRET: ${{ secrets.JWT_ACCESS_SECRET }}
JWT_REFRESH_SECRET: ${{ secrets.JWT_REFRESH_SECRET }}
3 changes: 2 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const router = require("./router/route");
const cookieParser = require("cookie-parser");
const { default: rateLimit } = require("express-rate-limit");
const helmet = require("helmet");
const { MAX_API_REQUEST_PER_IP_FOR_MINUTE } = require("./config");

require("dotenv").config();

Expand All @@ -16,7 +17,7 @@ app.use(helmet());
// Rate limit
const limiter = rateLimit({
windowMs: 1 * 60 * 1000, //1 min
max: process.env.MAX_API_REQUEST_PER_IP_FOR_MINUTE || 100,
max: MAX_API_REQUEST_PER_IP_FOR_MINUTE || 100,
});

// Middlewares
Expand Down
9 changes: 9 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const ACCESS_TOKEN_EXPIRE_TIME = "30m";
const REFRESH_TOKEN_EXPIRE_TIME = "10d";
const MAX_API_REQUEST_PER_IP_FOR_MINUTE = 100;

module.exports = {
ACCESS_TOKEN_EXPIRE_TIME,
REFRESH_TOKEN_EXPIRE_TIME,
MAX_API_REQUEST_PER_IP_FOR_MINUTE,
};
7 changes: 3 additions & 4 deletions src/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
convertToMilliseconds,
} = require("../utils/helpers");
const { sendEmail, emailBodyGenerate } = require("../utils/emai");
const { REFRESH_TOKEN_EXPIRE_TIME } = require("../config");

// bcrypt salt rounds
const saltRounds = 10;
Expand Down Expand Up @@ -80,9 +81,7 @@ async function registerUser(req, res) {
httpOnly: true, // only accessible by web server
secure: false, // https
sameSite: "lax", //cross-site cookie
maxAge: convertToMilliseconds(
process.env.REFRESH_TOKEN_EXPIRE_TIME
), // expiry time
maxAge: convertToMilliseconds(REFRESH_TOKEN_EXPIRE_TIME), // expiry time
};

if (status === "production") {
Expand Down Expand Up @@ -132,7 +131,7 @@ async function login(req, res) {
httpOnly: true, // only accessible by web server
secure: false, // https
sameSite: "lax", //cross-site cookie
maxAge: convertToMilliseconds(process.env.REFRESH_TOKEN_EXPIRE_TIME), // expiry time
maxAge: convertToMilliseconds(REFRESH_TOKEN_EXPIRE_TIME), // expiry time
};

if (status === "production") {
Expand Down
8 changes: 6 additions & 2 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const jwt = require("jsonwebtoken");
const {
REFRESH_TOKEN_EXPIRE_TIME,
ACCESS_TOKEN_EXPIRE_TIME,
} = require("../config");

function createToken(data) {
const token = jwt.sign(data, process.env.JWT_ACCESS_SECRET, {
expiresIn: process.env.ACCESS_TOKEN_EXPIRE_TIME,
expiresIn: ACCESS_TOKEN_EXPIRE_TIME,
});
return token;
}

function createRefreshToken(data) {
const token = jwt.sign(data, process.env.JWT_REFRESH_SECRET, {
expiresIn: process.env.REFRESH_TOKEN_EXPIRE_TIME,
expiresIn: REFRESH_TOKEN_EXPIRE_TIME,
});
return token;
}
Expand Down

0 comments on commit d6acc56

Please sign in to comment.