0.1.1 • Published 8 years ago

rethink-token v0.1.1

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

rethink-token

A non prescriptive token authentication module for RethinkDB (RethinkDBDash)

By Chris Cates :star:

Usage Example

//Add RethinkDBDash and initialize it
var r = require("rethinkdbdash")();

//Configure your database with rethink-config
require("rethink-config")({
  "r": r,
  "database": "tokens",
  "tables": ["user", "token"]
})

//Add the rethink-token package
var token = require("rethink-token");

//Initialize the package
token.init({
  "r": r,
  "db": "tokens",
  "userTable": "user",
  "tokenTable": "token"
})

//Add express
var express = require("express");
var app = express();

//Body Parser to parse JSON and HTML forms
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post("/register", function(req,res,next) {
  var user = {
    id: req.body.id,
    password: req.body.password
  }

  token.generate(user, function(response) {
    if (response.status == "ERR") {
      return res.status(400).send(response)
    } else {
      return res.status(200).send(response)
    }
  })
})

app.post("/login", function(req,res,next) {
  var user = {
    id: req.body.id,
    password: req.body.password
  }

  token.access(user, function(response) {
    if (response.status == "ERR") {
      return res.status(403).send(response)
    } else {
      return res.status(200).send(response)
    }
  })
})

app.get("/restrict", token.authenticate(), function(req,res,next) {
  return res.status(200).send("Look ma, Im authenticated!");
})