0.17.4 • Published 6 years ago

resolve-auth v0.17.4

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

resolve-auth

npm version

Provides an authentication mechanism using Passport authentication strategy.

Usage

First of all, create file with your strategy and add auth section into config.app.json for your application (see hacker-news example). Next, choose a type of authentication strategy (see passport-local or passport-google-oauth(https://github.com/jaredhanson/passport-google-oauth for example) ) and define options of it. Create your strategy in strategyConstructor function using options. Return an array of tuples { strategy, options } for every used route in your app.

Examples

Here is the simple example of local strategy, but you can use any other Passport strategy similarly.

import { Strategy } from 'passport-local'
import jwt from 'jsonwebtoken'
import jwtSecret from './jwtSecret'

const options = routes.map(({ path, method, callback }) => ({
  strategy: {
    usernameField: 'username',
    passwordField: 'username',
    successRedirect: null
  },
  route: {
    path: '/register',
    method: 'POST'
  },
  callback: async (_, username) =>
    jwt.sign(           // here fake use is created
      {                   // for user storing into db see HakerNews example
        name: username    // https://github.com/reimagined/resolve/tree/master/examples/hacker-news
      },
      jwtSecret
    )
}))

const strategyConstructor = options =>
  new Strategy(
    {
      ...options.strategy,
      passReqToCallback: true
    },
    async (req, username, password, done) => {
      try {
        done(null, await options.callback(req, username, password))
      } catch (error) {
        done(error)
      }
    }
  )

export default [{ options, strategyConstructor }]

Analytics

0.17.4

6 years ago

0.17.3

6 years ago

0.17.2

6 years ago

0.17.1

6 years ago

0.17.0

6 years ago

0.16.1

6 years ago

0.16.0

6 years ago

0.15.2

6 years ago

0.15.1

6 years ago

0.15.0

6 years ago

0.14.4

6 years ago

0.14.3

6 years ago

0.14.2

6 years ago

0.14.1

6 years ago

0.14.0

6 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.3

6 years ago

0.12.1

6 years ago

0.11.0

6 years ago

0.10.2

6 years ago

0.10.1

6 years ago

0.10.0

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago