Most applications need a way to authenticate users. In this recipe, we will create a simple form to create and authenticate users. We will keep our authenticated users in the session so that we can maintain our authenticated state even when the page is refreshed. In order to get our users, we will pass a random token to the socket, once it authenticates, that it can use to retrieve the authenticated profile.
For this recipe, we will use MongoDB to persist our users. We will also use the the md5 npm
module to hash our passwords before we save them to the database.
To implement basic authentication, follow these steps:
First, we will need to create our server. The server will require several additional modules that we will build next. Use the following code:
var express = require('express'), app = express(), http = require('http'), socketIO = require('socket.io'), server, io; app.get('/', function (req, res) { res...