Skip to content

Commit ae01e33

Browse files
committed
more notes on session and cookies
1 parent 1c07ada commit ae01e33

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
[connect-mongo](https://github.com/jdesboeufs/connect-mongo) package stores MongoDB session for Express and Connect
1+
### [connect-mongo](https://github.com/jdesboeufs/connect-mongo) package stores MongoDB session for Express and Connect
2+
3+
So, implemented sessions using Passport, but for storing sessions into my mongodb I use connect-mongo using a mongoose connection to connect to the mongodb database.
4+
5+
Then most standard implementation code is given in the [officila doc](https://github.com/jdesboeufs/connect-mongo#express-or-connect-integration)
6+
7+
```js
8+
const session = require('express-session');
9+
const MongoStore = require('connect-mongo')(session);
10+
11+
app.use(session({
12+
secret: 'foo',
13+
store: new MongoStore({ mongooseConnection: connection })
14+
}));
15+
16+
```
17+
connect-mongo stores sessions in the ["sessions" collection by default](https://github.com/jdesboeufs/connect-mongo/blob/bca754cc6ccded953f85ca37f647619f26b6783d/lib/connect-mongo.js#L22).
18+
19+
https://stackoverflow.com/questions/23773537/how-are-connect-mongo-mongostore-sessions-actually-saved

Node-Express/express-session-how-it-works.md

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,43 @@
1+
## What is a session?
2+
3+
A session is a place to store data that you want access to across requests. Each user that visits your website has a unique session. You can use sessions to store and access user data as they browse your application. Sessions are integral to web application development because they allow the application to store state. Based on what action a user took on Page A, we can show a different Page B. Without them, applications would be stateless, and not very useful.
4+
5+
Sessions can store their information in different ways. The popular ways to store session data is:
6+
7+
- In application memory
8+
- In a cookie
9+
- In a memory cache
10+
- In a database
11+
12+
#### The module like express-session will provide you with a nice API to work with sessions (letting you get & set data to the session), but under the hood, it will save and retrieve this data using a cookie.
13+
14+
15+
### Storing Session Data in Application Memory
16+
17+
One way to store session data is in Application memory. This is often the simplest way, but not used in production.
18+
19+
Storing session data in application memory essentially means that the data is stored for the lifetime of your application runtime. If your web application server crashes or is stopped, all session data is removed.
20+
21+
Storing session data in memory also causes memory leaks. As your application stays running, more and more memory is used, until your app runs out of memory.
22+
23+
For development purposes, it is often useful to store sessions in application memory. Otherwise, there are better ways of storing session data. We’ll explore these below.
24+
25+
### Storing Session Data in Cookies
26+
27+
A cookie is usually a small piece of data that gets sent between a web server to your web browser. It allows the server to store information relevant to a specific user.
28+
29+
One common use for cookies is to store session data. This works in the following way.
30+
31+
The server issues a cookie that gets sent to the web browser and stored for a period of time (called the expiration time).
32+
When a user makes a subsequent request to the web server, this cookie gets sent along with the request, and the server can read the information that is in it.
33+
The server can manipulate the cookie if it needs to, and then sends it back to the browser.
34+
Until the cookie expires, every time you make a request, your browser will send the cookies back to the server.
35+
36+
#### The module like express-session will provide you with a nice API to work with sessions (letting you get & set data to the session), but under the hood, it will save and retrieve this data using a cookie.
37+
38+
39+
40+
141
### We use sessions to maintain state between user requests and we use cookies to transport the session ID between those requests.
242

343
Every user interaction with your application is an isolated and individual request and response. The need to persist information between requests is vital for maintaining the ultimate experience for the user.
@@ -8,12 +48,26 @@ So I have to securely set up sessions in my application to mitigate risks such a
848

949
[express-session](https://www.npmjs.com/package/express-session) (https://github.com/expressjs/session ) - A very popular session module that has been highly vetted by the community and constantly improved.
1050

51+
```js
52+
const session = require('express-session')
53+
const dbConnection = require('./database')
54+
const MongoStore = require('connect-mongo')(session)
1155

56+
app.use(
57+
session({
58+
secret: 'fraggle-rock', //pick a random string to make the hash that is generated secure
59+
store: new MongoStore({ mongooseConnection: dbConnection }),
60+
resave: false, //required
61+
saveUninitialized: false //required
62+
})
63+
)
1264

65+
```
1366

1467
## What’s Going On Here
1568

16-
We're importing the [session function](https://github.com/expressjs/session/blob/master/session/session.js#L24) from the express-session NPM module and passing the session function a configuration object to set properties such as:
69+
We're importing the [session function](https://github.com/expressjs/session/blob/master/session/session.js#L24) from the express-session NPM module and passing the session function a configuration object to set properties inside the object passed to express-session. Note **express-session**, requires an object as an argument to initialize it.
70+
1771

1872
**Secret**. Required option. This is a value used in the signing of the session ID cookie, that is stored in the cookie.
1973

@@ -37,9 +91,7 @@ Forces a session that is "uninitialized" to be saved to the store. A session is
3791

3892

3993

40-
41-
42-
4394
#### Good resources
4495
- https://dzone.com/articles/securing-nodejs-managing-sessions-in-expressjs
45-
-
96+
97+
- https://nodewebapps.com/2017/06/18/how-do-nodejs-sessions-work/

0 commit comments

Comments
 (0)