# nexus-plugin-auth0

![npm](https://img.shields.io/npm/v/nexus-plugin-auth0?style=flat-square) ![npm (tag)](https://img.shields.io/npm/v/nexus-plugin-auth0/next?style=flat-square) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)

**Contents**

* [Installation](#installation)
* [How it Works](#how-it-works)
* [Examples](#examples)
  * [Protected Paths](#protected-paths)
  * [Usage with **nexus-plugin-shield**](#usage-with-nexus-plugin-shield)
* [Plugin Settings](#plugin-settings)

<br>

## Installation

```
npm install nexus-plugin-auth0
```

<br>

## How it Works

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

```json
{
  "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

```ts
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

```ts
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.

```ts
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

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

<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mediamagic.dev/product-docs/services/graphql-gateway/node_modules/nexus-plugin-auth0.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
