Authentication

Overview

subZero Auth library can be used to create a self-standing API service for handling user registration and authentication for Jamstack projects.

It's based on OAuth2 and JWT and will handle user signup, authentication and custom user data.

The features are:

  • Issuing JWTs
  • User management
  • Sign in with email, password, magic link, phone number
  • Sign in with external providers (Google, Apple, Facebook, Discord, ...)

Internally the library is mostly a wrapper around GoTrue to compile it to WASM in order to expose the functionality to Node.js.

The library behavior is customizable through environment variables.

For a full list of environment variables, see here

Importing

import {
    init as authInit,
    getRequestHandler as auth
} from '@subzerocloud/auth';
import nodemailer from 'nodemailer';
import debug from 'debug';
import { Client } from 'pg'; // when using postgresql as a database
// import Client from 'better-sqlite3'; // when using sqlite as a database

Initializing

const dbDialect = 'postgresql'; // or 'sqlite'
await authInit('postgresql', Client, nodemailer, {
    logFn: debug('subzero:auth'),
    // uncomment this line if you get the error:
    // "total length of command line and environment variables exceeds limit"
    // customEnv: env.parsed,
});

Handling requests

const authHandler = auth();

// use the handler in express
app.use('/auth', authHandler);

// use it directly with http in node
// const server = http.createServer(authHandler);
Previous
Language Bindings