I can not install the default cookie express-session - connect.sid
Created new files: test.html and test.js
Here is the code: test.js
var express = require("express"); var session = require("express-session"); var bodyParser = require("body-parser"); var cookieParser = require("cookie-parser"); var app = express(); var port = 4000; app.use(bodyParser()); app.use(cookieParser()); app.use(session({ secret: "Secret word" })); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); app.use("/login",function (req,res) { var login = req.body.user; var pass = req.body.password; console.log(login); console.log("Work"); res.send("Work"); }); app.listen(port);
but test.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.min.js"></script> </head> <body> <form action="http://localhost:4000/login" name="login" id="login"> <input type="text" name="user"> <input type="password" name="password"/> <input type="button" id="btn" value="Send"> </form> <script> var btn = document.getElementById("btn"); $("#btn").click(function () { var data = $("#login").serializeArray(); $.ajax({ url:"http://localhost:4000/login", data: data, type: "POST", success: function (res) { console.log(res); } }) }) </script> </body> </html>
If you manually open a new tab, and go on this path http://localhost/4000/login
then the connect.sid
cookie is created.
And I also wanted to ask about the sessions what they would explain to me.
I think it should be like this:
Enter the login and password, it is sent to the server, is it checked whether there is such a user in the database, and whether the password is suitable. If yes, then create a key (session id). And, for example, return the user.auth = true
object to the client, check on the client if this object = true
, then redirect to the MyOffice.html
page, for MyOffice.html
. And here is the question. How on the page MyOffice.html
to check whether the user is authorized? I think of the connect.sid
cookie, right? Sorry for the errors, and please explain to me :)