0.4.0 • Published 2 years ago
@gadgetinc/auth v0.4.0
@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
preValidationhooks. - manages sessions and users in your Gadget app's
userandsessionmodel.
Installation
yarn add @gadgetinc/auth
# or
npm install --save @gadgetinc/authUsage
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 clientredirectToSignIn- if a user is not signed in using thepreValidationcheck, then redirect the user to the path specified bysignInPath. Defaults tofalsesignInPath- the path to your login page. This is where users will be redirected to ifredirectToSignInis set totrue. Defaults to/signinproviders- an array of authentication providerstype- currently the only available type is"google"clientId- Google OAuth client idclientSecret- Google OAuth client secretscopes- optional OAuth scopes to request from the user. Defaults to["email", "profile"]for Google