Skip to content

Commit

Permalink
Merge pull request #60 from Murakano/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
jjikky authored Jul 14, 2024
2 parents d62ba94 + b66c461 commit 7413521
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/common/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const conf = {
// server
port: process.env.PORT,
corsWhiteList: process.env.CORS_WHITELIST,
corsUserAgent: process.env.CORS_USERAGENT,

// database
mongoURL: process.env.MONGO_URL,
Expand Down
16 changes: 15 additions & 1 deletion src/common/modules/express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,28 @@ module.exports = expressLoader = (app) => {
next();
});

// Content Security Policy 설정, 위 난수 활용
app.use((req, res, next) => {
res.setHeader('Content-Security-Policy', `script-src 'self' 'nonce-${res.locals.nonce}';`);
next();
});

// CORS 설정
app.use((req, res, next) => {
cors({
credentials: true,
origin: (origin, callback) => {
if (origin === undefined || (origin && conf.corsWhiteList?.indexOf(origin) !== -1)) {
if (
// whitelist에 있는 origin 허용
(origin && conf.corsWhiteList.indexOf(origin) !== -1) ||
// postman 허용
(!origin &&
conf.corsUserAgent.split(',').some((agent) => req.headers['user-agent'].includes(agent)))
) {
return callback(null, true);
}

console.error(`Blocked CORS request from: ${origin}`);
callback(new Error('CORS ERROR'));
},
})(req, res, next);
Expand Down
12 changes: 7 additions & 5 deletions src/routes/user/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ exports.localLogin = async (req, res, next) => {
const accessToken = generateAccessToken(user);
const refreshToken = generateRefreshToken(user);

await redisClient.set(user.email, refreshToken);
await redisClient.set(user.email, refreshToken, 'EX', 60 * 60 * 12);

res.cookie('refreshToken', refreshToken, config.cookieInRefreshTokenOptions);

Expand Down Expand Up @@ -137,9 +137,8 @@ exports.kakaoLogin = async (req, res) => {

const accessToken = generateAccessToken(user);
const refreshToken = generateRefreshToken(user);
console.log(user.email);
const re = await redisClient.set(user.email, refreshToken);
console.log('hh', re);

await redisClient.set(user.email, refreshToken, 'EX', 60 * 60 * 12);
res.cookie('refreshToken', refreshToken, config.cookieInRefreshTokenOptions);

sendResponse.ok(res, {
Expand Down Expand Up @@ -172,6 +171,9 @@ exports.refreshToken = async (req, res) => {
const storedRefreshToken = await redisClient.get(user.email);

if (storedRefreshToken !== refreshToken) {
console.error('Refresh token mismatch');
await redisClient.del(user.email);
res.clearCookie('refreshToken', config.cookieInRefreshTokenDeleteOptions);
return sendResponse.unAuthorized(res, {
message: ErrorMessage.REFRESH_TOKEN_MISMATCH,
});
Expand All @@ -188,7 +190,7 @@ exports.refreshToken = async (req, res) => {
email: user.email,
});

await redisClient.set(user.email, newRefreshToken);
await redisClient.set(user.email, newRefreshToken, 'EX', 60 * 60 * 12);
res.cookie('refreshToken', newRefreshToken, config.cookieInRefreshTokenOptions);

sendResponse.ok(res, {
Expand Down

0 comments on commit 7413521

Please sign in to comment.