Skip to content

Repository files navigation

πŸ” Supabase Auth API Server

A lightweight, production-ready Node.js REST API server that wraps Supabase Authentication, providing a clean interface for user authentication flows. Features include email/password signup, OTP verification, password reset, Google OAuth, and user management. Includes built-in API tester UI for testing all endpoints.

Node.js Express Supabase


πŸ‘€ Portfolio (Author)

Author
Name Fazla Rabbi
Role Software Developer
Portfolio / Website https://devfazla.com
Contact contact@devfazla.com

Socials:

WebsiteΒ Β  GitHubΒ Β  LinkedInΒ Β  FacebookΒ Β  XΒ Β 

Key Skills in This Project

  • Backend: Node.js, Express.js
  • Auth and Backend: Supabase (Email/Password, OTP, Google OAuth)
  • Environment: dotenv (configuration management)
  • HTTP & Networking: node-fetch, cors
  • Frontend (API Tester): HTML, CSS, JavaScript (vanilla)
  • API Testing: Postman, curl

✨ Features

  • πŸ”‘ Complete Authentication Flow

    • Email/password signup with OTP verification
    • Email/password sign-in
    • OTP resend functionality
    • Password reset with OTP verification
    • Google OAuth integration
  • πŸ‘€ User Management

    • Check if user exists (no admin key required)
    • Get user by access token or email (admin)
    • User existence validation before password reset
  • 🎨 Built-in API Tester

    • Interactive web UI for testing all endpoints
    • Real-time response display
    • No external tools needed
  • πŸ“š Developer-Friendly

    • Comprehensive documentation
    • Postman collection included
    • curl examples provided
    • TypeScript-ready structure
  • πŸ”’ Security

    • Environment-based configuration
    • CORS enabled
    • Input validation
    • Error handling

πŸš€ Quick Start

Prerequisites

  • Node.js 18+ and npm
  • Supabase Account (sign up)

Installation

  1. Clone or download this repository
git clone https://github.com/devfazla/Supabase-Auth.git
cd Supabase/Auth
  1. Install dependencies
npm install
  1. Configure environment variables

Copy env.example to .env:

cp env.example .env

Edit .env with your Supabase credentials:

SUPABASE_URL=https://your-project.supabase.co
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_SERVICE_ROLE_KEY=your-service-role-key  # Optional, for admin endpoints
PORT=3024  # Optional, defaults to 3024

Where to find your keys:

  • Go to Supabase Dashboard
  • Select your project β†’ Settings β†’ API
  • Copy Project URL β†’ SUPABASE_URL
  • Copy anon public key β†’ SUPABASE_ANON_KEY
  • Copy service_role key β†’ SUPABASE_SERVICE_ROLE_KEY (keep secret!)
  1. Start the server
# Development mode (auto-reload on file changes)
npm run dev

# Production mode
npm start
  1. Access the API Tester

Open your browser to: http://localhost:3024

You'll see an interactive UI to test all endpoints!


πŸ“– API Documentation

Base URL: http://localhost:3024 (or your configured PORT)

Health Check

GET /health

Check if the server is running.

Response:

{
  "status": "ok",
  "time": "2026-01-29T12:00:00.000Z"
}

Authentication Endpoints

Sign Up

POST /signUp

Register a new user with email and password. Sends OTP to email if email confirmation is enabled.

Request Body:

{
  "email": "user@example.com",
  "password": "securePassword123",
  "data": {  // Optional: user metadata
    "display_name": "John Doe"
  }
}

Response:

{
  "status": "ok",
  "message": "Signup successful. Please verify OTP sent to email.",
  "data": {
    "user": { ... },
    "session": null  // Session created after OTP verification
  }
}

Verify Sign-Up OTP

POST /signUpVerify

Verify the OTP code sent to the user's email after signup.

Request Body:

{
  "email": "user@example.com",
  "token": "123456",
  "type": "signup"  // Optional: "signup" (default), "magiclink", "recovery"
}

Response:

{
  "status": "ok",
  "message": "Email verified successfully",
  "session": {
    "access_token": "...",
    "refresh_token": "...",
    "user": { ... }
  },
  "user": { ... }
}

Resend OTP

POST /resendOtp

Resend a new OTP code to the user's email. Invalidates the previous OTP.

Request Body:

{
  "email": "user@example.com"
}

Response:

{
  "status": "ok",
  "message": "OTP resent successfully",
  "data": { ... }
}

Sign In

POST /signIn

Authenticate a user with email and password.

Request Body:

{
  "email": "user@example.com",
  "password": "securePassword123"
}

Response:

{
  "user": { ... },
  "session": {
    "access_token": "...",
    "refresh_token": "...",
    "user": { ... }
  }
}

Google OAuth Sign In

GET /gglSignIn

Initiate Google OAuth authentication flow.

Query Parameters:

  • redirectTo (optional): URL to redirect after authentication

Example:

GET /gglSignIn?redirectTo=https://your-app.com/callback

Response:

{
  "url": "https://accounts.google.com/oauth/authorize?..."
}

Password Reset Flow

Request Password Reset

POST /forgtPss

Request a password reset email. Validates user exists before sending email.

Request Body:

{
  "email": "user@example.com",
  "redirectTo": "https://your-app.com/reset-password"  // Optional
}

Response:

{
  "status": "ok",
  "data": { ... }
}

Error (if user doesn't exist):

{
  "error": "User does not exist"
}

Verify Reset Password OTP

POST /resetPssVerify

Verify the OTP token from reset email and set a new password.

Request Body:

{
  "email": "user@example.com",
  "token": "123456",  // OTP from email ({{ .Token }})
  "newPassword": "newSecurePassword123"
}

Response:

{
  "status": "ok",
  "message": "Password reset successfully",
  "session": {
    "access_token": "...",
    "refresh_token": "...",
    "user": { ... }
  },
  "user": { ... }
}

User Management Endpoints

Get User (by Access Token)

GET /getUsr

Get user information using an access token from sign-in or OTP verification.

Headers:

Authorization: Bearer <access_token>

Response:

{
  "user": {
    "id": "...",
    "email": "user@example.com",
    ...
  }
}

Get User (by Email) - Admin Only

GET /getUsr?email=user@example.com

Get user information by email. Requires SUPABASE_SERVICE_ROLE_KEY.

Query Parameters:

  • email: User's email address

Response:

[
  {
    "id": "...",
    "email": "user@example.com",
    ...
  }
]

Check User Exists

POST /usrExst

Check if a user exists by email. No admin key required - uses a clever signup-trick method.

Request Body:

{
  "email": "user@example.com"
}

Response:

{
  "exists": true,
  "data": null
}

How it works: Attempts a signup with a random password. If user_metadata and identities are empty/null β†’ user exists. If they have values β†’ new user (doesn't exist).


πŸ§ͺ Testing

Using the Built-in Web UI

  1. Start the server: npm run dev
  2. Open http://localhost:3024
  3. Fill in the form fields and click Run on any endpoint
  4. View responses in real-time below each card

Using curl

See curl_testing_guide.md for complete curl examples.

Quick examples:

# Health check
curl http://localhost:3024/health

# Sign up
curl -X POST http://localhost:3024/signUp \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"password123"}'

# Sign in
curl -X POST http://localhost:3024/signIn \
  -H "Content-Type: application/json" \
  -d '{"email":"test@example.com","password":"password123"}'

Windows PowerShell:

# Use curl.exe and single quotes for JSON
curl.exe -X POST http://localhost:3024/signUp `
  -H "Content-Type: application/json" `
  -d '{"email":"test@example.com","password":"password123"}'

Using Postman

Import supabase_server.postman_collection.json into Postman:

  1. Open Postman
  2. Click Import
  3. Select supabase_server.postman_collection.json
  4. All endpoints are ready to test!

πŸ“ Project Structure

Supabase/Auth/
β”œβ”€β”€ server.js                    # Main server file (all endpoints)
β”œβ”€β”€ package.json                 # Dependencies and scripts
β”œβ”€β”€ env.example                  # Environment variables template
β”œβ”€β”€ .env                         # Your actual env vars (gitignored)
β”œβ”€β”€ .gitignore                   # Git ignore rules
β”œβ”€β”€ README.md                    # This file
β”œβ”€β”€ curl_testing_guide.md        # curl examples
β”œβ”€β”€ supabase_server.postman_collection.json  # Postman collection
└── public/
    └── index.html               # Built-in API tester UI

πŸ”§ Configuration

Environment Variables

Variable Required Description
SUPABASE_URL βœ… Yes Your Supabase project URL
SUPABASE_ANON_KEY βœ… Yes Supabase anonymous/public key
SUPABASE_SERVICE_ROLE_KEY ❌ No Service role key (for admin endpoints)
PORT ❌ No Server port (default: 3024)

Scripts

npm run build    # Syntax check the server file
npm start        # Start server (production)
npm run dev      # Start server with auto-reload (development)

πŸ” Security Notes

  • Never commit .env - It's in .gitignore for a reason!
  • Service Role Key - Keep SUPABASE_SERVICE_ROLE_KEY secret. Only use for admin endpoints.
  • CORS - Currently enabled for all origins. Restrict in production if needed.
  • Password Reset - User existence is validated before sending reset emails to prevent email enumeration.

🎯 Use Cases

  • Backend API - Use as a middleware/auth layer for your frontend apps
  • Mobile Apps - REST API for React Native, Flutter, etc.
  • Microservices - Authentication service in a microservices architecture
  • Testing - Quick way to test Supabase auth flows
  • Prototyping - Fast setup for auth in new projects

🚧 Common Workflows

Complete Signup Flow

  1. POST /signUp β†’ User receives OTP email
  2. POST /signUpVerify β†’ User enters OTP, gets session
  3. GET /getUsr (with Bearer token) β†’ Get user info

Password Reset Flow

  1. POST /forgtPss β†’ User receives reset email with {{ .Token }}
  2. POST /resetPssVerify β†’ User enters token + new password
  3. User is authenticated with new session

Check Before Signup

  1. POST /usrExst β†’ Check if email already registered
  2. If exists: false β†’ Proceed with signup
  3. If exists: true β†’ Show "Email already in use"

πŸ› Troubleshooting

"SUPABASE_URL or SUPABASE_ANON_KEY missing"

  • Check your .env file exists and has correct values
  • Ensure no extra spaces or quotes around values

"User does not exist" on password reset

  • This is intentional! The endpoint validates user exists before sending email
  • Use /usrExst to check if user exists first

CORS errors

  • Server has CORS enabled by default
  • If issues persist, check browser console for specific error

Port already in use

  • Change PORT in .env to a different port (e.g., 3025)
  • Or stop the process using port 3024

Email rate limit exceeded

  • Supabase rate-limits email sending (OTP, password reset, etc.) to prevent abuse
  • Error message: "Email rate limit exceeded" or similar
  • Solution:
    • Wait a few minutes before retrying
    • Configure email via custom SMTP in Supabase Dashboard β†’ Settings β†’ Auth β†’ SMTP Settings to use your own email provider and bypass rate limits
  • Prevention: Avoid rapid successive requests to /signUp, /resendOtp, or /forgtPss with the same email
  • Note: Rate limits vary by Supabase plan (free tier has stricter limits)

πŸ“ License

ISC


🀝 Contributing

Feel free to submit issues, fork, and create pull requests!


πŸ“š Additional Resources


⭐ Features Highlights

  • βœ… Zero dependencies beyond core packages
  • βœ… Single file server - Easy to understand and modify
  • βœ… Built-in tester - No external tools needed
  • βœ… Production-ready - Error handling, validation, CORS
  • βœ… Well-documented - README, curl guide, Postman collection
  • βœ… Smart user check - No admin key needed for existence check

Made with ❀️ for the Supabase community

About

A complete Supabase authentication starter featuring JWT auth, OAuth (Google & GitHub), magic links, email/password login, session management, and secure user authentication for Next.js and React applications.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages