SitePoint
  • Blog
  • Discord
  • Forum
  • Library
  • Login
Join Premium

User Authentication with the MEAN Stack

Close
  1. Preface
    • User Authentication with the MEAN Stack
    • Notice of Rights
    • Notice of Liability
    • Trademark Notice
    • About SitePoint
  2. 1User Authentication with the MEAN Stack
    • The MEAN Stack Authentication Flow
    • The Example Application
    • Creating the MongoDB Data Schema with Mongoose
    • Set up Passport to Handle the Express Authentication
    • Configure API Endpoints
    • Initialize the Angular App
    • Create an Angular Authentication Service
    • Apply Authentication to Angular App
    • Running the Angular App

You don't have any bookmarks for this book yet.

User Authentication with the MEAN Stack

Configure API Endpoints

With the API we’ve got two things to do:

  1. make the controllers functional
  2. 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:

  1. take the data from the submitted form and create a new Mongoose model instance
  2. call the setPassword method we created earlier to add the salt and the hash to the instance
  3. save the instance as a record to the database
  4. generate a JWT
  5. send the JWT inside the JSON response
End of PreviewSign Up to unlock the rest of this title.

Community Questions