@gadgetinc/auth
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

@gadgetinc/auth

@gadgetinc/auth is a fastify plugin that helps manage on platform authentication in Gadget.

Features

This library implements the following features:

  • registers route handlers for OAuth start, callback, and sign out.
  • provides a utility for protecting HTTP routes using preValidation hooks.
  • manages sessions and users in your Gadget app's user and session model.

Installation

yarn add @gadgetinc/auth
# or
npm install --save @gadgetinc/auth

Usage

This plugin can be registered with Gadget in either a boot plugin or route plugin.

To register the plugin:

// routes/+auth.js
import { Auth } from "@gadgetinc/auth";
import { api } from "gadget-server";

export default function (server) {
  server.register(Auth, {
    api,
    providers: [
      {
        type: "google",
        clientId: process.env.GOOGLE_CLIENT_ID,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      },
    ],
  });
}

To protect a route:

// routes/GET-protected-route.js
const { preValidation } = require("@gadgetinc/auth");

module.exports = async function ({ reply }) {
  await reply.send("this is a protected route!");
};

module.exports.options = {
  preValidation,
};

Options

Plugin options

  • api - your Gadget api client
  • redirectToSignIn - if a user is not signed in using the preValidation check, then redirect the user to the path specified by signInPath. Defaults to false
  • signInPath - the path to your login page. This is where users will be redirected to if redirectToSignIn is set to true. Defaults to /signin
  • providers - an array of authentication providers
    • type - currently the only available type is "google"
    • clientId - Google OAuth client id
    • clientSecret - Google OAuth client secret
    • scopes - optional OAuth scopes to request from the user. Defaults to ["email", "profile"] for Google

Readme

Keywords

none

Package Sidebar

Install

npm i @gadgetinc/auth

Homepage

gadget.dev

Weekly Downloads

44

Version

0.4.0

License

MIT

Unpacked Size

58.6 kB

Total Files

6

Last publish

Collaborators

  • jasong689
  • scott-rc
  • alexangelini
  • gedge
  • kristianpd
  • airhorns