Oouhammmourachid /
Public
ouhammmourachid / saas-starter
PublicMulti-tenant SaaS backend: teams with owner/admin/member roles, email invites, and a subscription status you update from your payment provider's webhook.
- SaaS starter
- 0 deploys
- 0 forks
SaaS Starter: teams, roles, invites and billing status
Multi-tenant SaaS backend: teams with owner/admin/member roles, email invites, and a subscription status you update from your payment provider's webhook.
What you get
- Creating a team makes you its owner, with an owner membership added automatically
- Members see only the teams they belong to (API rules via
memberships_via_team) - Owners invite by email; invite tokens are hidden from the API and accepted at
POST /api/invites/{token}/accept - Plan and subscription fields can only be changed by the server, never by the client
POST /api/billing/webhookupdates a team's plan from your payment provider
Collections
teams
| Field | Type | Notes |
|---|---|---|
name (required) | text | Team name |
owner (required) | relation → users | Set by a hook to the signed-in user |
plan | select | Set by the billing webhook |
subscription_status | select | Set by the billing webhook |
current_period_end | date | Set by the billing webhook |
| Action | Who can do it |
|---|---|
| List / view | memberships_via_team.user ?= @request.auth.id |
| Create | signed-in users |
| Update | owner = @request.auth.id |
| Delete | owner = @request.auth.id |
memberships
Created by hooks only (team creation, invite acceptance).
| Field | Type | Notes |
|---|---|---|
team (required) | relation → teams | |
user (required) | relation → users | |
role (required) | select | owner, admin or member |
| Action | Who can do it |
|---|---|
| List / view | team.memberships_via_team.user ?= @request.auth.id |
| Create | superusers only |
| Update | team.owner = @request.auth.id |
| Delete | team.owner = @request.auth.id || user = @request.auth.id |
invites
| Field | Type | Notes |
|---|---|---|
team (required) | relation → teams | |
email (required) | Who is invited | |
role (required) | select | admin, member |
token | text | Generated by a hook; hidden from the API |
accepted | bool | |
invited_by | relation → users | Set by a hook |
| Action | Who can do it |
|---|---|
| List / view | team.owner = @request.auth.id |
| Create | signed-in users |
| Update | superusers only |
| Delete | team.owner = @request.auth.id |
Hooks
All hooks are in pb_hooks/main.pb.js and commented.
Setup
- Set Application URL and SMTP in Settings so invite emails send and link to your app (
<appURL>/invite/<token>). - Set the environment variable
BILLING_WEBHOOK_SECRET: Shared secret your webhook caller sends inX-Webhook-Secret
Quick start
import PocketBase from "pocketbase"
const pb = new PocketBase("https://YOUR-INSTANCE.pocketshub.com")
const team = await pb.collection("teams").create({ name: "Acme" })
// Invite someone (owner only). They get an email with a link to /invite/<token>.
await pb.collection("invites").create({ team: team.id, email: "sam@example.com", role: "member" })
// On your /invite/:token page, once Sam is signed in with a verified email:
await pb.send(`/api/invites/${token}/accept`, { method: "POST" })
// Teams the signed-in user belongs to:
const teams = await pb.collection("teams").getFullList()
Notes
- The billing webhook takes a simple JSON body. Verify your provider's signature (Stripe, Creem, Paddle...) in your own handler, then forward the result here with the shared secret.
- Accepting an invite needs a verified email that matches the invite.
- The
adminrole is stored for your own collections' rules (e.g.team.memberships_via_team.role ?= 'admin'). Out of the box, only the owner can rename the team, invite, and manage members.
License
MIT
3 collections, 2 relations. Hover a collection to trace its relations