4 글 보임 - 1 에서 4 까지 (총 4 중에서)
-
글쓴이글
-
2023년 11월 25일 14:59 #105041
김동현참가자프론트를 react로 짜고있습니다.
//cors 설정
app.use(cors({ origin:"http://localhost:3000", methods:["GET","POST","PUT","DELETE"], credentials: true, }));
//session 설정 router.use(session({ name: "session ID", secret: process.env.SESSION_SECRIT, resave : false, saveUninitialized : false, store : new FileStore(), cookie : { maxAge : 60 * 60 * 1000, //유지시간 1시간 httpOnly : false, secure : false, } })) POST 요청(react) axios.post("http://localhost:8080/users", {withCredentials:true} ) .then((result)=>{ console.log(result.data); setLoading(false); }) .catch((e)=>{ console.log('get Session 요청 실패:'+e); setLoading(false); })
//get Session router.post('',(req,res)=>{ try{ res.setHeader('Access-Control-Allow-Credentials', 'true'); console.log('session info', req.session); res.status(200).json(req.session) }catch(e){ console.log('get 세션 오류:'+e); } })
현재 코드 짜임세는 얼추 이정도입니다. 로그인을 하고 나면 application의 쿠키 탭에서 세션이 잘 저장이 되어있고
get요청을 보낼때에는 네트워크탭에서 request header를 보면 쿠키(세션 식별자값)가 잘 담아져서 전송이 됩니다.
post요청으로 바꾸면 request header에 쿠키가 담겨지지 않아 로그인한 유저의 세션을 알수가 없는 상황입니다.
뭐가 문제인지 한참 찾고있는데 도저히 못찾겠어서 여쭤봅니다...
2023년 11월 25일 15:44 #105044
김동현참가자localhost 일때에는 get 요청을 보낼때에는 전송이 되고 post요청만 안되었는데
http://127.0.0.1:8080 이걸로 수정후에는 둘다 안됩니다!!
혹시 도움이 될까해서 header 보내드리겠습니다 /////// localhost로 할시
Request URL: http://localhost:8080/users Request Method: GET Status Code: 200 OK Remote Address: [::1]:8080 Referrer Policy: strict-origin-when-cross-origin Accept: application/json, text/plain, */* Accept-Encoding: gzip, deflate, br Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7 Connection: keep-alive Cookie: session ID=s%3AVEBJQ17-NmghSfRR4vlaszqfZcCCTfre.pVZRTvvsxijX4sgxESOrWlyUSUq%2FgkfoS9kXh9dIrK8 Host: localhost:8080 If-None-Match: W/"c8-CA1BunPlY3SGbAI0DmuNFzK8jV8" Origin: http://localhost:3000 Referer: http://localhost:3000/ ////// http로 할시 Request URL: http://127.0.0.1:8080/users Request Method: GET Status Code: 200 OK Remote Address: 127.0.0.1:8080 Referrer Policy: strict-origin-when-cross-origin Accept: application/json, text/plain, */* Accept-Encoding: gzip, deflate, br Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7 Connection: keep-alive Host: 127.0.0.1:8080 If-None-Match: W/"75-934xVn0ei6ytB1bo6dQGkWYF68s" Origin: http://localhost:3000 Referer: http://localhost:3000/
2023년 11월 25일 18:34 #105054
codingapple키 마스터서버 cors() 안의 설정도 127.0.0.1로 바꾸고 리액트 미리보기도 127.0.0.1로 접속해봅시다
-
글쓴이글
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
- 답변은 로그인 후 가능합니다.