codestates / codestates/UDONDAM
[๐งError_log] ์ด์ ์ / 2021-12-16
- 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.