User Authentication with the MEAN Stack
Configure API Endpoints
With the API we’ve got two things to do:
- make the controllers functional
- secure the
/api/profile
route so that only authenticated users can access it
Code the Register and Login API Controllers
In the example app, the register and login controllers are in /api/controllers/authentication.js. In order for the controllers to work, the file needs to require Passport, Mongoose and the user model:
const mongoose = require('mongoose');const passport = require('passport');const User = mongoose.model('User');
The Register API Controller
The register controller needs to do the following:
- take the data from the submitted form and create a new Mongoose model instance
- call the
setPassword
method we created earlier to add the salt and the hash to the instance - save the instance as a record to the database
- generate a JWT
- send the JWT inside the JSON response