Skip to main content
Home

Built and signed on GitHub Actions

A collection of auth related helpers to be used with supabase auth.

This package works with Node.jsIt is unknown whether this package works with Cloudflare Workers, Deno, Bun
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
JSR Score
94%
Published
10 months ago (0.3.1)

supabase-auth-helpers

A collection of auth related helpers to be used with supabase auth.

Installation

Published on jsr.io - https://jsr.io/@butttons/supabase-auth-helpers

npm

npx jsr add @butttons/supabase-auth-helpers

pnpm

pnpm dlx jsr add @butttons/supabase-auth-helpers

yarn

yarn dlx jsr add @butttons/supabase-auth-helpers

deno

deno add @butttons/supabase-auth-helpers

bun

bunx jsr add @butttons/supabase-auth-helpers

Setup

Frontend

Backend

  • Get your Supabase project ID at the project settings page. Under General settings > Reference ID.
    • If you are using custom domains, this should be the subdomain. For example, if your custom domain is auth.example.com, the supabaseId should be auth. You can find out supabase ID by looking at the cookies in the browser. The cookie name should be in the format sb-[SUPABASE_ID]-auth-token.[NUMBER].
    • On localhost, this ID is typically 127.
  • Get your JWT secret at configuration page. Under JWT Settings > JWT Secret.
  • Get the supabase project URL as well on the configuration page. Under Project URL > URL.

Usage

  • Set up the auth helper somewhere in your application:
// file: src/lib/supabase-auth-helper.ts
import { SupabaseAuthHelper } from '@butttons/supabase-auth-helpers';

export const supabaseAuthHelper = new SupabaseAuthHelper({
  supabaseId: 'your-supabase-id',
  supabaseUrl: 'your-supabase-url',
  jwtSecret: 'your-jwt-secret',
});

Now, you can import it safely in server components, route handlers, middleware to validate the incoming requests easily.

Middleware

// file: src/middleware.ts
import { NextResponse } from 'next/server';
import { supabaseAuthHelper } from '@/lib/supabase-auth-helper';

export async function middleware(req: NextRequest) {
  const user = await supabaseAuthHelper.getUser(req);

  if (!user) {
    return new Response('Unauthorized', {
      status: 401,
    });
  }

  return NextResponse.next({
    request: {
      headers: req.headers,
    },
  });
}

Route handler

// file: src/app/api/me.ts
import { supabaseAuthHelper } from '@/lib/supabase-auth-helper';

export const GET = async (req: Request) => {
  const user = await supabaseAuthHelper.getUser(req);

  if (!user) {
    return new Response('Unauthorized', {
      status: 401,
    });
  }

  return Response.json(user);
};

Server actions

// file: src/app/actions.ts
"use server"
import { cookies } from 'next/headers'; 

import { supabaseAuthHelper } from '@/lib/supabase-auth-helper';

export const updateProfile = async (input: UserProfile) => {
  const user = await supabaseAuthHelper.getUser(cookies());

  if (!user) {
    return new Response('Unauthorized', {
      status: 401,
    });
  }

  // Update the user

  return { user }
}

Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@butttons/supabase-auth-helpers

Import symbol

import * as supabase_auth_helpers from "@butttons/supabase-auth-helpers";
or

Import directly with a jsr specifier

import * as supabase_auth_helpers from "jsr:@butttons/supabase-auth-helpers";

Add Package

pnpm i jsr:@butttons/supabase-auth-helpers
or (using pnpm 10.8 or older)
pnpm dlx jsr add @butttons/supabase-auth-helpers

Import symbol

import * as supabase_auth_helpers from "@butttons/supabase-auth-helpers";

Add Package

yarn add jsr:@butttons/supabase-auth-helpers
or (using Yarn 4.8 or older)
yarn dlx jsr add @butttons/supabase-auth-helpers

Import symbol

import * as supabase_auth_helpers from "@butttons/supabase-auth-helpers";

Add Package

vlt install jsr:@butttons/supabase-auth-helpers

Import symbol

import * as supabase_auth_helpers from "@butttons/supabase-auth-helpers";

Add Package

npx jsr add @butttons/supabase-auth-helpers

Import symbol

import * as supabase_auth_helpers from "@butttons/supabase-auth-helpers";

Add Package

bunx jsr add @butttons/supabase-auth-helpers

Import symbol

import * as supabase_auth_helpers from "@butttons/supabase-auth-helpers";