An open API service indexing awesome lists of open source software.

https://github.com/mcalthrop/auth-mongo-express-node

Express server with MongoDB and authentication and authorisation
https://github.com/mcalthrop/auth-mongo-express-node

apidoc authorisation es6 eslint jasmine json-api learning learning-materials mongo mongodb mongoose passport-js supertest yarn

Last synced: 25 days ago
JSON representation

Express server with MongoDB and authentication and authorisation

Awesome Lists containing this project

README

        

[![Build Status](https://travis-ci.org/mcalthrop/auth-mongo-express-node.svg?branch=master)](https://travis-ci.org/mcalthrop/auth-mongo-express-node)

# 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](#api-docs) for full details.

### Features

The following features and technologies are used:

- [Yarn](https://yarnpkg.com/en/)
- [ES2015/ES6](https://babeljs.io/learn-es2015/)
- [Eslint](http://eslint.org/)
- MVC implementation using [Node](https://nodejs.org/en/), [Express](http://expressjs.com/), [Mongo](https://www.mongodb.com/) and [Mongoose](http://mongoosejs.com/)
- [Travis](https://travis-ci.org/mcalthrop/auth-mongo-express-node) for CI testing
- JSON data returned in [JSON API](http://jsonapi.org/) format
- [PassportJS](http://passportjs.org/) for authentication and [Permission](https://www.npmjs.com/package/permission) for authorisation
- [APIDOC](http://apidocjs.com/) for documenting the API endpoints
- [Jasmine](https://jasmine.github.io/) and [Supertest](https://www.npmjs.com/package/supertest) for testing the API endpoints

## Setup

### Install yarn

Follow the instructions here:

[yarnpkg.com/en/docs/install](https://yarnpkg.com/en/docs/install)

### Install NodeJS packages

Install the NodeJS packages:

``` sh
$ yarn
```

### Install Mongo

Follow the instructions here:

[www.mongodb.com/download-center#community](https://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:

``` sh
$ mongod
```

In another terminal tab, you can use `nodemon`:

``` sh
$ yarn nodemon
```

To check if the API is running, browse to:

[localhost:4001/api](http://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/](https://passwordsgenerator.net/))

To start the API:

``` sh
$ yarn start
```

## Tests

### All

To run all tests:

``` sh
$ yarn test
```

### Linting

> Check the `.eslint` file for the Eslint rules.

The Javascript code can be linted as follows:

``` sh
$ yarn lint
```

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

``` sh
$ yarn lint:fix
```

### API

> API tests are in the `spec` directory.

To run the API tests:

``` sh
$ yarn jasmine
```

## API docs

To generate the documentation for the API endpoints:

``` sh
$ 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:

``` sh
$ yarn db:seed
```

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

``` sh
$ yarn db:seed:local
```

## License

[MIT](LICENSE)