Skip to content

mcalthrop/auth-mongo-express-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

mongo-express-node-auth

Express server with MongoDB and authentication and authorisation

Overview

This is a codebase that can be used as a starting point for an API that provides endpoints for authentication.

Routes

  • /api/signup – create a new user account
  • /api/login – log in to an existing account
  • /api/logout – log out

Additionally, two routes are provided that require authorisation:

  • /api/me – allow logged-in user (with any role) to read and update their details
  • /api/users – allow a user with the admin role to see a list of all users (further routes can be added to implement full CRUD functionality)

See API docs for full details.

Features

The following features and technologies are used:

Setup

Install yarn

Follow the instructions here:

yarnpkg.com/en/docs/install

Install NodeJS packages

Install the NodeJS packages:

$ yarn

Install Mongo

Follow the instructions here:

www.mongodb.com/download-center#community

Running the API

API code is in the api directory.

During development

In a terminal tab, start the MongoDB daemon:

$ mongod

In another terminal tab, you can use nodemon:

$ yarn nodemon

To check if the API is running, browse to:

localhost:4001/api

Once deployed

Ensure that the following environment variables are set in the deployed environment:

  • MONGODB_URI – points to a valid MongoDB instance
  • APP_SECRET – a randomly-generated secret (see passwordsgenerator.net/)

To start the API:

$ yarn start

Tests

All

To run all tests:

$ yarn test

Linting

Check the .eslint file for the Eslint rules.

The Javascript code can be linted as follows:

$ yarn lint

And to auto-correct any errors that can be corrected:

$ yarn lint:fix

API

API tests are in the spec directory.

To run the API tests:

$ yarn jasmine

API docs

To generate the documentation for the API endpoints:

$ yarn apidoc

And to view the output, open the apidocs/index.html file in your browser.

Seeding the database

Database-related code is in the db directory.

To seed the Mongo database with sample data:

$ yarn db:seed

When developing locally, running the following command will seed the local database:

$ yarn db:seed:local

License

MIT