Giter Club home page Giter Club logo

auth0-node's Introduction

CircleCI codecov npm version GuardRails badge Security Responsible Disclosure

Auth0 Node

Simple Auth0 module for token verification.

Prereqs & Install

  • Node >=9.10.0
  • npm >=6.1.0

Please note that the TypeScript target is ES6.

npm install @usefultools/auth0-node

Usage

1) Initialise the client ๐Ÿ‘พ

import { Auth0 } from "@usefultools/auth0-node" 

const auth0 = new Auth0("test.auth0.com")

2) Verify, verify, verify ๐Ÿ˜Ž

import { Auth0, Session } from "@usefultools/auth0-node" 
import { isNonEmptyString } from "@usefultools/utils"

const auth0 = new Auth0("test.auth0.com")

async function isAuthorised(req: Request, res: Response, next: Next): Promise<void> {
  const { headers } = req

  const bearerStr = "Bearer "
  const authHeader = headers[HeaderKey.Authorization]

  if (isNonEmptyString(authHeader) && authHeader.startsWith(bearerStr)) {
    const token = authHeader.substring(bearerStr.length, authHeader.length)

    try {
      const decoded = await auth0.verifyToken(token)

      Object.assign(req, { ctx: {
        ...req.ctx,
        session: decoded,
      }})

      next()
    } catch (err) {
      res.error(unauthorized(err))
    }
  } else {
    res.error(unauthorized("Invalid or missing token"))
  }
}

Contributing

If you have comments, complaints, or ideas for improvements, feel free to open an issue or a pull request! See Contributing guide for details about project setup, testing, etc.

Author and license

This library was created by @LITCHI.IO. Main author and maintainer is Slavo Vojacek.

Contributors: Slavo Vojacek

@usefultools/auth0-node is available under the ISC license. See the LICENSE file for more info.

auth0-node's People

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

auth0-node's Issues

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.