-2

Please help me about authentication section for user authentication for node.js. I am new for node.js. This is flow.

  1. login.html page
  2. user put the data in the user name and password text box field.
  3. send that data to the back end (Node.js)
  4. check that data are correct or not (properties or with database)
  5. create session
  6. put session
  7. go to next page(authentication page)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    Possible duplicate of [this question](http://stackoverflow.com/questions/3498005/user-authentication-libraries-for-node-js)? – Twisol Feb 14 '12 at 07:26
  • A true duplicate, just use exppress + passport/everyauth, everything will be taken care by them, question closed! – Farid Nouri Neshat Feb 14 '12 at 12:18

1 Answers1

1

Here I have used the express framework to make easier.

The html form "index.html"

<html>
  <head><title>Login</title></head>
  <body>
    <form action="myaction">
      <input type="text" name="mytext"/>
      <input type="password" name="mypass"/>
      <input type="submit" value="submit"/>
    </form>
  </body>
</html>

and on the server side on "app.js"

var express = require('express');
var fs = require('fs')
var app = express.createServer();

app.get("/", function(req, res) {
  fs.readFile("index.html", function (err, data) {
    if (err) {
      res.contentType("html");
      res.send("File not found");
      }
    else{ 
      res.contentType("html");
      res.send(data);
    }
  });      
});

app.get("/myaction", function(req, res) {
  if(req.param("mytext") === "admin" && req.param("mypass") === "admin"){
    res.contentType("html");
    res.send("You are authenticated");
  }
  else{
    res.contentType("html");
    res.send("Invalid Username or password");
  }
});

app.listen(8000);
console.log("Server running on localhost:8000");
Hem Shrestha
  • 479
  • 7
  • 14
  • Hi Hem Shrestha. I know this one. But I would like to know how to create session and cookies. I just confused..Thank for your answer. – Aung Mon Tun Feb 17 '12 at 03:00