Home

Awesome

PocketBase OTP Hook

This is a One Time Password (OTP) authentication hook for PocketBase. It enables email-based OTP authentication for users.

Why use One Time Passwords?

Benefits

Use Cases

Caveats and Drawbacks

Installation

  1. Install the package:

    npm install pocketbase-otp
    
  2. Copy the migrations into your pb_migrations:

    cp node_modules/pocketbase-otp/pb_migrations pb_migrations
    
  3. Copy the hook file into your pb_hooks::

    cp node_modules/pocketbase-otp/pb_hooks pb_hooks
    

Usage

Requesting a Code

First, make sure your PocketBase instance has SMTP configured and tested for sending emails.

To request an OTP code:

const client = new PocketBase(...)
const res = await client.send(`/api/otp/auth`, {
  body: { email },
  method: 'POST',
})

The PocketBase instance will send the OTP code to the provided email address.

Verifying the Code

Once the user receives the OTP code, use it to trade for the authentication token and user record:

const res = await client.send(`/api/otp/verify`, {
  body: { email, code },
  method: 'POST',
})
client.authStore.save(res.token, res.record)

This will authenticate the PocketBase client using the OTP.

License

MIT License