connect-mongo

markus·2021년 7월 10일
0

Youtube Clone

목록 보기
6/16

connect-mongo?

cookie와 session을 통해 로그인을 구현할 수 있게 됐다. 그러나 문제가 있다. session이 서버에 저장된다는 것이다.

더 정확히 말하면 session ID는 cookie에 저장되고, session data는 서버에 저장된다.

즉, 서버를 다시 시작하면 session 정보가 사라진다는 뜻이다. 따라서 session을 서버가 아니라 DB에 저장해야 안전하다.

사용방법

app.use(
  session({
    secret: "비밀",
    resave: true,
    saveUninitialized: false,
    store: MongoStore.create({ mongoUrl: "mongodb://localhost/test" }),
  })
);

store option에 추가해주면 끝.

기타

resave

Forces the session to be saved back to the session store, even if the session was never modified during the request.

true는 로그인할 때마다 session이 저장되는 것이고
false는 똑같은 session은 한 번만 저장하도록 한다.

saveUninitialized

Forces a session that is "uninitialized" to be saved to the store. A session is uninitialized when it is new but not modified.

true는 로그인 안된 상태에서도 session을 DB에 저장하고
false는 로그인이 된 상태에서만 session을 DB에 저장한다.

0개의 댓글