GithubHelp home page GithubHelp logo

plugin-auth0's Introduction

nexus-plugin-auth0

npm npm (tag) PRs Welcome

Contents


Installation

npm install nexus-plugin-auth0

How it Works

The plugin currently expects the "UsersAccessToken" to be in the following format on the header of the incoming request.

{
  "authorization": "Bearer UsersAccessToken"
}

There are two main ways to use this plugin.

  1. Using the protectedPaths to deny access to certain paths.
  2. Using it to only validate and decode then to using the decoded token (available as ctx.token) to control access using another plugin such as nexus-plugin-sheild

The decoded token will be added to Nexus Context under ctx.token which has the following type

type DecodedAccessToken = {
  iss: string
  sub: string
  aud: string[]
  iat: number
  exp: number
  azp: string
  scope: string
}
// ctx.token
type ContextToken = DecodedAccessToken | null

Examples

Protected Paths

If protectedPaths is passed, then only valid access tokens will be allowed to access these paths

import { use } from 'nexus'
import { auth } from 'nexus-plugin-auth0'

use(
  auth({
    auth0Audience: 'nexus-plugin-auth0',
    auth0Domain: 'graphql-nexus.eu.auth0.com',
    protectedPaths: ['Query.posts'],
  })
)

Usage with nexus-plugin-shield

All paths will have the decoded token added to ctx only if the token is validated but will not deny access. The token can then be used by nexus-plugin-shield to control access.

import { use } from 'nexus'
import { auth } from 'nexus-plugin-auth0'
import { rule } from 'nexus-plugin-shield'


const isAuthenticated = rule({ cache: 'contextual' })(async (parent, args, ctx: NexusContext, info) => {
  const userId = ctx?.token?.sub
  return Boolean(userId)
})

const rules = {
  Query: {
    posts: isAuthenticated,
  },
  Mutation: {
    deletePost: isAuthenticated,
  },
}

use(
  auth({
    auth0Audience: 'nexus-plugin-auth0',
    auth0Domain: 'graphql-nexus.eu.auth0.com',
  })
)

use(
  shield({
    rules,
  })
)

Plugin Settings

type Settings = {
  auth0Domain: string
  auth0Audience: string
  protectedPaths?: string[]
  debug?: boolean
}

plugin-auth0's People

Contributors

jasonkuhrt avatar renovate[bot] avatar williamluke4 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.