Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
louity committed Mar 2, 2016
0 parents commit 5985395
Show file tree
Hide file tree
Showing 19 changed files with 910 additions and 0 deletions.
194 changes: 194 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"parser" : "babel-eslint",
"env": {
"node" : 1,
"browser" : 1,
"mocha" : 1
},
"ecmaFeatures": {
"jsx": 1,
"classes": 1,
"destructuring": 1
},
"globals": {
"__DEV__": false,
"__PROD__": false,
"__DEBUG__": false,
"__CLIENT__": false,
"__SERVER__": false,
"__CORDOVA__": false,
"__WEB__" : false,
"__VERSION__" : false,
"__API__": false,
"__ANALYTICS_TOKEN__" : false,
"superagent": false,
"Media": false
},
"plugins": [],
"rules": {
"comma-dangle": [1, "never"],
"no-cond-assign": 2,
"no-constant-condition": 1,
"no-control-regex": 1,
"no-debugger": 1,
"no-dupe-args": 2,
"no-dupe-keys": 1,
"no-empty": 1,
"no-ex-assign": 2,
"no-extra-boolean-cast": 1,
"no-extra-parens": 0,
"no-extra-semi": 1,
"no-func-assign": 2,
"no-inner-declarations": 1,
"no-invalid-regexp": 2,
"no-irregular-whitespace": 2,
"no-negated-in-lhs": 1,
"no-obj-calls": 2,
"no-regex-spaces": 2,
"no-reserved-keys": 0,
"no-sparse-arrays": 2,
"no-unreachable": 2,
"use-isnan": 2,
"valid-jsdoc": 1,
"valid-typeof": 2,
"no-unexpected-multiline": 2,

"block-scoped-var": 2,
"complexity": 0,
"curly": 1,
"default-case": 0,
"dot-notation": 1,
"eqeqeq": [2, "smart"],
"guard-for-in": 2,
"no-alert": 1,
"no-caller": 2,
"no-div-regex": 1,
"no-else-return": 0,
"no-eq-null": 1,
"no-eval": 1,
"no-extend-native": 2,
"no-extra-bind": 1,
"no-fallthrough": 1,
"no-floating-decimal": 1,
"no-implied-eval": 1,
"no-iterator": 1,
"no-labels": 2,
"no-lone-blocks": 2,
"no-loop-func": 2,
"no-multi-spaces": [0, {
"exceptions": {}
}],
"no-multi-str": 2,
"no-native-reassign": 2,
"no-new": 1,
"no-new-func": 2,
"no-new-wrappers": 2,
"no-octal": 2,
"no-octal-escape": 2,
"no-param-reassign": 1,
"no-proto": 2,
"no-redeclare": 2,
"no-return-assign": 2,
"no-script-url": 2,
"no-self-compare": 2,
"no-sequences": 2,
"no-unused-expressions": 1,
"no-void": 2,
"no-with": 2,
"radix": 0,
"wrap-iife": 2,
"yoda": 2,

"strict": 0,

"no-catch-shadow": 1,
"no-delete-var": 1,
"no-label-var": 1,
"no-shadow": 2,
"no-shadow-restricted-names": 2,
"no-undef": 2,
"no-undef-init": 0,
"no-undefined": 2,
"no-unused-vars": 0,

"array-bracket-spacing": 0,
"indent": [2, 2, {
"SwitchCase": 1
}],
"brace-style": 1,
"camelcase": 0,
"comma-spacing": 1,
"comma-style": [2, "last"],
"consistent-this": 0,
"eol-last": 1,
"func-names": 0,
"func-style": 0,
"key-spacing": [0, {
"beforeColon": true,
"afterColon": true
}],
"lines-around-comment": 0,
"max-nested-callbacks": [2, 6],
"new-cap": 0,
"new-parens": 2,
"newline-after-var": 1,
"no-array-constructor": 2,
"no-continue": 0,
"no-inline-comments": 0,
"no-lonely-if": 1,
"no-mixed-spaces-and-tabs": 2,
"no-multiple-empty-lines": [2, {
"max" : 2
}],
"no-nested-ternary": 2,
"no-new-object": 1,
"semi-spacing": [2, {
"before": false,
"after": true
}],
"no-spaced-func": 2,
"no-ternary": 0,
"no-trailing-spaces": 1,
"no-underscore-dangle": 0,
"no-unneeded-ternary": 2,
"object-curly-spacing": [1, "always"],
"one-var": [0, {
"var" : "always",
"let" : "always",
"const" : "always"
}],
"operator-assignment": [0, "always"],
"padded-blocks": [2, "never"],
"quotes": [2, "single", "avoid-escape"],
"quote-props": [1, "as-needed"],
"sort-vars": 0,
"keyword-spacing": 1,
"space-before-blocks": [2, "always"],
"space-before-function-paren": [2, "always"],
"space-in-brackets": 0,
"space-in-parens": [2, "never"],
"space-infix-ops": 1,
"space-unary-ops": [2, {
"words" : true,
"nonwords" : false
}],
"spaced-comment": [2, "always", {
"exceptions" : ["-", "+"]
}],
"wrap-regex": 0,

"constructor-super": 2,
"generator-star-spacing": [2, {
"before" : true,
"after" : false
}],
"no-this-before-super": 2,
"no-var": 0,
"object-shorthand": 0,
"prefer-const": 1,

"max-depth": [2, 3],
"max-len": [2, 120, 2],
"max-params": [2, 6]
}
}
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.env
public
*.swp
*.swo
dump
.stage
24 changes: 24 additions & 0 deletions .release.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"non-interactive": false,
"dry-run": false,
"verbose": true,
"force": false,
"pkgFiles": ["package.json"],
"increment": "patch",
"commitMessage": "Release %s",
"tagName": "%s",
"tagAnnotation": "Release %s",
"changelogCommand": "conventional-changelog -p angular -i CHANGELOG.md -w",
"dist": {
"repo": "[email protected]:repos/amoeda-api.git",
"stageDir": ".stage",
"baseDir": "dist",
"files": ["**/*"],
"pkgFiles": null
},
"github": {
"release": true,
"releaseName": "Release %s",
"tokenRef": "GITHUB_API_TOKEN"
}
}
2 changes: 2 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Copyright Get a cup of Coffee

1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node index.js
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# koa-node-mongoose API starter kit

Starter kit for building API based on node, koa and mongoose.
Authentication routes (register and login) based on [Passport](https://github.com/jaredhanson/passport) are implemented (see `routers/auth.js`) with cookie based sessions.
Examples of authenticated and custom routes are given (see `routers/example.js`).

## Installation

Requires node >=4.2.0 (usage of ES6).

Install packages dependencies with `npm install`.

## Dependencies

- bluebird : lib for js promise.
- cpy : lib for copying files.
- dotenv : read environnement variables in .env file and access it in node via `process.env.<variable>`.
- got : friendly http lib.
- koala : koa server in ES6 with utils
- koa-... : utils for koala.
- mongodb : database.
- mongoose : database access lib.
- koa-passport : [Passport](https://github.com/jaredhanson/passport) middleware for Koa
- bcrypt: lib to help you hash passwords
- debug : debug console logs
- eslint : js linter
- http-errors : create HTTP errors for Express, Koa, Connect, etc. with ease.
- passport-local : local authentication strategy for passport

## Usage

### Define environnement variables

First you need to define environnement variables for dotenv lib.
You must create a .env file at the root of the project. The following variables are necessary :
```
PORT = <listening port, default 5000>
NODE_ENV = <development/test>
DEBUG = <true/false>
MONGOLAB_URI = <uri of the mongodb database>
MONGOTEST_URI = <uri of the mongodb test database>
```

### Run
Run it with command `node index.js`.

### Test in command line using curl
You can test the register routes and the example routes using curl.
First register:
```
curl -X POST --data '{"username": "<username>", "password": "<password>"}' http://localhost:<PORT>/auth/register
```
Then login . You must give a filename to store cookies :
```
curl -c <cookie_store_filename> -X POST --data '{"username": "<username>", "password": "<password>"}' http://localhost:<PORT>/auth/register
```
Test the home route, which is not authenticated (cookie not needed) :
```
curl -X GET http://localhost:<PORT>/home
```
Test the authenticated route :
```
curl -b <cookie_store_filename> -X GET http://localhost:<PORT>/authenticated
```
Retry without giving the cookies file name, you will be unauthorized :
```
curl -X GET http://localhost:<PORT>/authenticated
```
You can also test the custom route which accessible for user whose username begin with the letter 'a' :
```
curl -b <cookie_store_filename> -X GET http://localhost:<PORT>/cutom-authenticated
```
or the personalised route that only the user can use :
```
curl -b <cookie_store_filename> -X GET http://localhost:<PORT>/authenticated/<username>
```

## Contributing

## History

## Credits


## License

65 changes: 65 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// authentication module
require('./auth')

// libs
const koala = require('koala');
const cors = require('koa-cors');
const path = require('path');
const json = require('koa-json');
const passport = require('koa-passport')
const session = require('koa-generic-session')
const debug = require('debug')('app')

var app = koala()

app.keys = ['azertytreza']
app.use(session())

app.use(require('koa-file-server')({
root: path.join(__dirname, 'public')
}));

app.use(cors({
origin: '',
credentials: false
}));

const bouncer = require('koa-bouncer');

app.use(function *(next) {
this.request.body = yield* this.request.json();
yield* next
})

app.use(bouncer.middleware());

app.use(json());

app.use(passport.initialize());
app.use(passport.session());

app.use(function *(next) {
try {
yield* next;
} catch (err) {
this.status = err.status || 500;

if (this.status === 500) {
debug('Internal error', err)
}

if (err instanceof bouncer.ValidationError) {
this.status = 400; // bad request
}

this.body = err.message || 'The server made a boo';
}
});

require('./routers')(app);

module.exports = function (database) {
app.db = database;
return app;
}

Loading

0 comments on commit 5985395

Please sign in to comment.