JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q8191F
  • License MIT

This package provide an auth to your app.

Package Exports

  • resolve-auth

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (resolve-auth) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

resolve-auth

npm version

Provides an authentication mechanism with several possible variants:

  • local strategy
  • github
  • google

Usage

First of all, create file with your strategy and add auth section into resolve.config.json for your application (see auth example). Next, choose type of auth strategy (now we support local, github and google) and define options fo it - add auth routes that configure the strategy routing, describe strategy specified params and write code for handlers: Return array of tuples { strategy, options } for every used auth algo.

Examples

Local strategy example

import { Strategy as strategy } from 'passport-local' // <- strategy type
import jwt from 'jsonwebtoken'
import uuid from 'uuid'

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

    return jwt.sign(user, process.env.SECRET_JWT)
  },
  failureCallback: (error, redirect) => {
    redirect(`/`) // in case of fail
  }
}

export default [{ strategy, options }]

Github authectication strategy example

import { Strategy as strategy } from 'passport-github'
import jwt from 'jsonwebtoken'

const options = {
  strategy: {
    clientID: 'MyClientID',
    clientSecret: 'MyClientSecret',
    callbackURL: 'http://localhost:3000/auth/github/callback',
    successRedirect: null
  },
  routes: {
    auth: {
      path: '/auth/github',
      method: 'get'
    },
    callback: {
      path: '/auth/github/callback',
      method: 'get'
    }
  },
  authCallback: async ({ resolve, body }, profile) => {
    // your code to authenticate a user
    return jwt.sign(user, process.env.SECRET_JWT)
  },
  (error, redirect, { resolve, body }) => {
    // in case of fail
  }
}

export default [{ strategy, options }]

Google authectication strategy example

import { Strategy as strategy } from 'passport--google-oauth2'
import jwt from 'jsonwebtoken'

const options = {
  strategy: {
    clientID: 'MyClientID',
    clientSecret: 'MyClientSecret',
    callbackURL: 'http://localhost:3000/auth/google/callback',
    successRedirect: null
  },
  routes: {
    auth: {
      path: '/auth/google',
      method: 'get'
    },
    callback: {
      path: '/auth/google/callback',
      method: 'get'
    }
  },
  authCallback: async ({ resolve, body }, profile) => {
    // your code to authenticate a user
  },
  (error, redirect, { resolve, body }) => {
    // in case of fail
  }
}

export default [{ strategy, options }]