MEAN stack user authentication example – I’ve split the login and registration pages out from the angular application in order to secure access to the angular client files, so all front end angular files (including javascript, css, images etc) are only available to authenticated users. To run MEAN stack applications locally you need NodeJS installed and MongoDB running on your system.

It’s also possible to include the login and registration pages within the angular application and allow public access to the angular client files, this is how I’ve built previous examples and is fine as long as you don’t have any sensitive data stored in your client files. The main benefit of securing the client files is just a bit of extra piece of mind that you’re not going to accidentally leak any secure information in your angular app files.

The express js application is structured using an MVC-ish pattern, there are controllers and views but rather than models I’ve gone with a services layer for data access & business logic. The services use mongoskin as the mongodb driver, it’s a thin wrapper for the native mongodb driver that provides a simpler interface for performing CRUD operations.

we have also develop Single page application user authentication in angularjs. just check here to get the free source code.

Run the project

Once you’ve downloaded the code you can run the command ‘npm install’ from the project root folder (where the package.json file is located) to download all node package dependencies.

Then run ‘node server.js’ from the same location to start the web server and browse to http://localhost:3000 to access the application.

MEAN stack user authentication
MEAN stack user authentication

MEAN stack user authentication example

Step 1:

First create index.html file.

Step 2:

Express/NodeJS Users API Controller

The express users api controller defines the routes responsible for user related operations such as authentication, registration, retrieving, updating and deleting user data.

Step 3:

Express/NodeJS App Controller

The express app controller controls access to the angular app client files. It uses session/cookie authentication to secure the angular files, and also exposes a JWT token to be used by the angular app to make authenticated api requests.

Connect the MongoDB

The express config file contains configuration data used by the nodejs application.

Click to download
Click to download