@gadgetinc/auth
v0.4.0
Published
`@gadgetinc/auth` is a `fastify` plugin that helps manage on platform authentication in [Gadget](https://gadget.dev).
Downloads
34
Keywords
Readme
@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
andsession
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 clientredirectToSignIn
- if a user is not signed in using thepreValidation
check, then redirect the user to the path specified bysignInPath
. Defaults tofalse
signInPath
- the path to your login page. This is where users will be redirected to ifredirectToSignIn
is set totrue
. Defaults to/signin
providers
- 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