codestates / codestates/UDONDAM

[๐Ÿ”งError_log] ์ด์„ ์˜ / 2021-12-16

Open
#179 0 comments 0 reactions 0 assignees View on GitHub
Completed error.log
Dominant language
TypeScript
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

### ์–ด๋–ค ์—๋Ÿฌ์ธ๊ฐ€์š”?
> ๋กœ์ปฌ ํ™˜๊ฒฝ์—์„œ๋Š” ์ฟ ํ‚ค ์ƒ์„ฑ๊ณผ ์‚ญ์ œ๊ฐ€ ์ž˜ ์ด๋ฃจ์–ด์ง€๋‚˜, ๋ฐฐํฌ ํ™˜๊ฒฝ(ec2)์—์„œ๋Š” ์ƒ์„ฑ๋งŒ ๋˜๊ณ  ์‚ญ์ œ๋Š” ๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ.
> ์„œ๋ฒ„ ์ชฝ์—์„œ ์ฟ ํ‚ค๋Š” ์ฝํ˜€์ง€๋‚˜ ์‚ญ์ œ๋งŒ ์•ˆ ๋˜๊ณ  ์žˆ์Œ.
> `res.cookie('jwt', value, option)`์œผ๋กœ ์ฟ ํ‚ค๋ฅผ ์ƒ์„ฑํ•˜๊ณ 
> `clearCookie('jwt')` ๋ช…๋ น์–ด๋กœ ์ฟ ํ‚ค๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฑด๋ฐ ๋กœ์ปฌ์—์„œ๋Š” ์ž˜ ์‚ญ์ œ๋จ. ๋ฐฐํฌ ํ™˜๊ฒฝ์—์„œ ์•ˆ๋  ๋ฟ์ž„.
### ์—๋Ÿฌ ๋ฉ”์‹œ์ง€
> ๋”ฑํžˆ ์—†์Œ.
> ๊ทธ์ € ์ƒํ™ฉ์„ ๋‹ด์€ ์ฝ”๋“œ๋“ค์„ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Œ.
```jsx
// server index.js ์ผ๋ถ€
app.use(cors({
origin:['http://localhost:3000','https://udondam.com'],
credentials: true,
methods: ['GET', 'POST', 'PATCH', 'DELETE' ,'OPTIONS']
}));
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({extended:true}));

// client ๋กœ๊ทธ์ธ ๋ฒ„ํŠผ ์ชฝ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ์ผ๋ถ€
const body = { email: loginInfo.email, password: loginInfo.password };
const loginInfoPost = await axios.post(`${process.env.REACT_APP_API_URL}/login`, body, { withCredentials: true });

// server ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ ์ผ๋ถ€
const { id, nickname, area, area2, manager, socialType } = userInfo;
const userData = {
userId: id,
nickname: nickname,
area: area,
area2: area2,
manager: manager,
socialType: socialType
}
const token = generateAccessToken(userData);
sendAccessToken(res, token, userData);

// server generateAccessToken, sendAccessToken ํ•จ์ˆ˜
generateAccessToken: (data) => {
return sign(data, process.env.ACCESS_SECRET, { expiresIn: "4h" });
}
sendAccessToken: (res, token, userData) => {
res.status(200).cookie("jwt", token,{
sameSite: 'none',
domain: DOMAIN,
path: '/',
secure: true,
httpOnly: true,
expires: new Date(Date.now() + 1000 * 60 * 60 * 48),
}).json({ data: userData });
return ;
}

// client ๋กœ๊ทธ์•„์›ƒ ๋ฒ„ํŠผ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜ ์ผ๋ถ€
const logoutResult = await axios.get(`${process.env.REACT_APP_API_URL}/logout`, { withCredentials: true })

// server ๋กœ๊ทธ์•„์›ƒ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ์ „๋ฌธ
logout: async (req, res) => {
// console.log(req.cookie) >> { jwt : ~~~~} ์ด๋Ÿฐ ๋А๋‚Œ์œผ๋กœ ์ฝํ˜€์ง
try {
res.clearCookie('jwt');
// res.cookie('jwt','test',{expires:new Date(Date.now() - 1)}) >> ์‹œ๋„ํ–ˆ์œผ๋‚˜ ๋กœ์ปฌ์—์„  ์ž˜ ์ง€์›Œ์ง€๊ณ  ๋ฐฐํฌ์—์„  ์•ˆ๋จ
res.status(200).json({"message": "logout!"});
return;
}
catch (err) {
console.log(err);
return res.status(401).json({ "message": "Unauthorized"});
}
}

```

>
### ์—๋Ÿฌ ํ•ธ๋“ค๋ง ๋ฐฉ๋ฒ•
> * ์ฝ”๋“œ์Šคํ…Œ์ด์ธ ์˜ ์•„๊ณ ๋ผ์Šคํ…Œ์ด์ธ ์— ๋ฌธ์˜ํ•˜์—ฌ ํžŒํŠธ๋ฅผ ์–ป๊ณ  ์ˆ˜์ •ํ–ˆ๋‹ค.
> [Express ๊ณต์‹ ๋ฌธ์„œ](http://expressjs.com/en/4x/api.html#:~:text=res.clearCookie(name%20%5B%2C%20options%5D))์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์•ˆ๋‚ดํ•˜๊ณ  ์žˆ๋‹ค.

```
Web browsers and other compliant clients will only clear the cookie if the given options is identical to those given to res.cookie(), excluding expires and maxAge.
```
> ์ฆ‰ ์›น ๋ธŒ๋ผ์šฐ์ € ๋ฐ ํด๋ผ์ด์–ธํŠธ๋Š” ์ง€์ •๋œ ์˜ต์…˜์ด expires, maxAge๋ฅผ ์ œ์™ธํ•˜๊ณ  res.cookie()์—์„œ ์ œ๊ณต๋œ ์˜ต์…˜๊ณผ ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋งŒ ์ฟ ํ‚ค๋ฅผ ์ง€์šด๋‹ค๊ณ  ํ•œ๋‹ค.
> ๊ทธ๋ž˜์„œ clearCookie์— ์ฟ ํ‚ค์˜ ์ด๋ฆ„('jwt')๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ๊ฐ’๊ณผ expires, maxAge๋ฅผ ์ œ์™ธํ•œ ์˜ต์…˜๋“ค๋„ ์ถ”๊ฐ€ํ–ˆ๋‹ค.
> ๊ทธ๋žฌ๋”๋‹ˆ ๋กœ์ปฌ๋ฟ๋งŒ์ด ์•„๋‹ˆ๋ผ ๋ฐฐํฌ ํ™˜๊ฒฝ์—์„œ๋„ ์ฟ ํ‚ค ์‚ญ์ œ๊ฐ€ ์ž˜ ๋๋‹ค.

>
```jsx
// ์ฟ ํ‚ค ๋งŒ๋“ค ๋•Œ
res.status(200).cookie("jwt", token,{
sameSite: 'none',
domain: DOMAIN,
path: '/',
secure: true,
httpOnly: true,
expires: new Date(Date.now() + 1000 * 60 * 60 * 48),
}).json({ data: userData });

// ์ฟ ํ‚ค ์—†์•จ ๋•Œ - ์ด์ „ .ver
res.clearCookie('jwt'); // ๋กœ์ปฌ์—์„œ๋Š” ์—†์–ด์ง€๋‚˜, ๋ฐฐํฌ ํ™˜๊ฒฝ์—์„œ๋Š” ์—†์–ด์ง€์ง€ ์•Š์Œ

// ์ฟ ํ‚ค ์—†์•จ ๋•Œ - ์ˆ˜์ •.ver
// ์ฟ ํ‚ค ์‚ญ์ œ๋จ!
res.clearCookie('jwt',{
sameSite: 'none',
domain: DOMAIN,
path: '/',
secure: true,
httpOnly: true
}
);
```
>
### ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ์œ„ํ•ด ์ฐธ๊ณ ํ•œ ๋ ˆํผ๋Ÿฐ์Šค ๋งํฌ
> [Express ๊ณต์‹ ๋ฌธ์„œ](http://expressjs.com/en/4x/api.html#:~:text=res.clearCookie(name%20%5B%2C%20options%5D))

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.