-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
How are Cookies used and What are Sessions in ExpressJS & 2526 56837 …
…7652626
- Loading branch information
1 parent
ada1704
commit bd1bc63
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
## Cookies | ||
|
||
- Data sent to the client side from the server and stored on the client side | ||
- To use Cookies in ExpressJS we use a library called cookie-parser | ||
- To create a cookie I must set a route for it | ||
|
||
```js | ||
var ckpars = require("cookie-parser"); | ||
var app = require("express"); | ||
app.get("/", function(request, response){ | ||
response.cookie("name","express").send("cookie-set"); | ||
}); | ||
|
||
app.listen(3000); | ||
|
||
``` | ||
|
||
|
||
### How To Add Cookies with an expiration date | ||
```js | ||
var ckpars = require("cookie-parser"); | ||
var app = require("express"); | ||
app.get("/", function(request, response){ | ||
response.cookie(name,"val",{expire: 420000 + Date.now()});//expire 7 mins from now | ||
}); | ||
|
||
app.listen(3000); | ||
``` | ||
|
||
### How To Delete Existing Cookie | ||
```js | ||
var express = require('express'); | ||
var app = express(); | ||
|
||
app.get('/clear_cookie', function(request, response){ | ||
response.clearCookie('foo'); | ||
response.send('cookie foo cleared'); | ||
}); | ||
|
||
app.listen(3000); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
### Sessions | ||
|
||
- Cookies and URL are good ways to transport data in between the frontend and the backend | ||
- The downside of cookies is that they are read on the frontend which is very dangerous | ||
- Sessions helps us solve this security flaw and instead of outputing the credentials it gives us just an id | ||
- This information is stored in the backend and is encrypted | ||
- Whenever a user hits a certain endpoint it creates the session for the user and gives them a cookie | ||
- Any proceeding visit of the user will be checked and the user is granted access and a new session is created for him | ||
- |