2.0.1 • Published 6 years ago

sphynx v2.0.1

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

SPHYNX

An authentication middleware for express microservices.

How to use

You can install it with npm:

$ npm install sphynx --save

Then you are a simple require away from sphynx:

const {sphynx} = require('sphynx')

It assumes you have a route like:

// assuming your express() variable is called 'app'
app.get('/auth', (req, res) => {
  let token = req.header('x-auth')

  // just make sure your model have a method that returns a user by token
  User.returnByToken(token).then((user) => {
    res.status(200).send(user)
  }).catch((e) => {
    res.status(401).send('Unable to find user')
  })
})

Or, if you're using async/await:

app.get('/auth', async (req, res) => {
  const token = req.headers('x-auth')
  try {
    const user = await User.returnByToken(tone)
    res.status(200).send(user)
  }
  catch(e) {
    res.status(401).send('Unable to find user')
  }
})

Real world example

You can easely plug it into your system and use it, as in:

const express  = require('express')
// If you're not using ES6, you can change the line below 
const {sphynx} = require('sphynx')
let app = express()

// This one line will take care of requesting your auth server to know if the user is logged in
app.use(sphynx)

// If you want to use it on specific routes, you can add it like:
app.get('/foo', sphynx, (req, res, next) => {
  // your code here
})

You can access the user object sent over the request if you want to

Got questions?

ulissescaon@gmail.com

2.0.1

6 years ago

2.0.0

6 years ago

1.0.26

7 years ago

1.0.25

7 years ago

1.0.24

7 years ago

1.0.23

7 years ago

1.0.22

7 years ago

1.0.21

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago