Dev

Supabase Server Public Beta: Auth Và Client Tự Động Cho Edge Functions

08/05/2026 5 phút đọc Supabase, Edge Functions, Auth
Supabase Server

TL;DR

Supabase vừa phát hành @supabase/server public beta — gói xử lý auth verification, client setup, request context và boilerplate server-side cho bạn. Hoạt động trên Edge Functions, Vercel Functions, Cloudflare Workers, Hono và Bun. Không còn copy/paste _shared/*.ts giữa các functions.

📰 Vấn đề: 25.000 Edge Functions đều giống nhau

Supabase phân tích 25.000 Edge Functions đã deploy và thấy cùng một pattern: dev đang rebuild cùng đoạn setup code đi đi lại lại chỉ để đến business logic thực sự.

Đa số functions cần:

  • Tạo Supabase client với SUPABASE_ANON_KEY
  • Tạo admin client với SUPABASE_SERVICE_ROLE_KEY để bypass RLS
  • Verify JWT
  • Parse claims
  • Handle CORS
  • Wire up auth context
  • Copy/paste _shared/*.ts giữa các functions

🚀 Giải pháp: @supabase/server

Với @supabase/server, bạn chỉ cần khai báo ai có thể gọi endpoint và nhận về context đã khởi tạo đầy đủ:

import { withSupabase } from 'npm:@supabase/server'

export default {
  fetch: withSupabase({ auth: 'user' }, async (req, ctx) => {
    const { data } = await ctx.supabase.from('todos').select()
    return Response.json(data)
  }),
}

🔧 Hoạt động thế nào?

Core của @supabase/serverSupabaseContext — request context bao gồm mọi thứ Edge Functions cần, đã config sẵn:

  • User-scoped Supabase client (tự động respect RLS policies)
  • Admin client với service role access
  • Verified user identity
  • JWT claims
  • Auth metadata
  • Built-in request/auth helpers

🎛️ Linh hoạt khi cần control

Nếu cần control error handling và responses, dùng createSupabaseContext trực tiếp:

import { createSupabaseContext } from 'npm:@supabase/server'

export default {
  fetch: async (req) => {
    const { data: ctx, error } = await createSupabaseContext(req, { auth: 'user' })
    if (error) return Response.json({ error: error.message }, { status: error.status })

    const { data } = await ctx.supabase.from('todos').select()
    return Response.json(data)
  },
}

🌍 Cross-platform

export default { fetch } tương đương Deno.serve(...) — đều define request handler. Dùng export default vì nó work across Edge Functions, Workers và Bun. Nếu thích Deno.serve, vẫn dùng được.

Chốt một câu

@supabase/server không phải feature sexy, nhưng giải quyết đúng pain point: setup code lặp đi lặp lại trong Edge Functions. Khi bạn viết function thứ 10, thứ 20, sự khác biệt giữa "có server package" và "không có" sẽ rõ ràng. Đây là loại công cụ giúp bạn focus vào business logic thay vì boilerplate.

Đọc tiếp Anthropic NLAs: Dịch Suy Nghĩ Của Claude Sang Tiếng Người
Xem tất cả bài viết
SupabaseEdge FunctionsAuthDev