Skip to content

Mohamed-4rarh/next-supabase-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

22 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Next.js 15 Starter with Supabase, React Query

A modern Next.js 15 starter template with Supabase authentication, React Query for data fetching, and built-in wrappers for queries and authentication. This starter is designed to accelerate development by providing preconfigured hooks, utilities, and best practices.

๐Ÿš€ Features

  • Next.js 15 โ€“ The latest Next.js version for optimized performance.
  • Supabase Authentication โ€“ Built-in auth system with user session handling.
  • React Query โ€“ Efficient data fetching and caching.
  • ShadCN Components โ€“ Prebuilt UI components for faster development.
  • Tailwind CSS โ€“ Utility-first styling for rapid UI building.
  • Zod Validation โ€“ Schema-based form validation for better data handling.
  • Prebuilt Hooks โ€“ Hooks for fetching data and mutations in client components.
  • Query & Auth Wrappers โ€“ Easily manage authentication state and query handling.

๐Ÿ“ฆ Installation

Create a new project using the CLI (if available):

npx create-next-supabase-starter@latest my-project

Or manually clone the repository:

git clone https://github.com/your-username/your-repo.git my-project
cd my-project
pnpm install

๐Ÿ›  Setup

1. Environment Variables

Create a .env.local file and add:

NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key

2. Run Development Server

pnpm dev

Your app should be running at http://localhost:3000.

๐Ÿ“Œ Usage

๐Ÿ— Authentication

The AuthContext ensures user authentication is managed across the app.

"use client";

const AuthContext = createContext<AuthContextType | undefined>(undefined);

export function AuthProvider({ children }: { children: React.ReactNode }) {
  const { data: user, isLoading } = useQuery({
    queryKey: ["user"],
    queryFn: async () => {
      const { data } = await supabase.auth.getUser();
      return data?.user ?? null; // โœ… Ensures it's never undefined
    },
    staleTime: 0,
  });

  return (
    <AuthContext.Provider value={{ user: user ?? null, loading: isLoading }}>
      {children}
    </AuthContext.Provider>
  );
}

export function useAuth() {
  const context = useContext(AuthContext);
  if (!context) {
    throw new Error("useAuth must be used within an AuthProvider");
  }
  return context;
}

๐Ÿ”„ Fetching Data

Use the useClientFetch hook for fetching data efficiently on client components:

import { useClientFetch } from "@/hooks/useClientFetch";

const Posts = () => {
  const { data, isLoading } = useClientFetch("posts", "posts");

  if (isLoading) return <p>Loading...</p>;

  return (
    <ul>
      {data?.map((post) => (
        <li key={post.id}>{post.name}</li>
      ))}
    </ul>
  );
};

Advanced Filtering Example

const FilteredUsers = () => {
  const { data, isLoading } = useClientFetch(
    "filtered-users", // key
    "users", // table name
    5000, // cache time
    (query) => query.eq("role", "admin") // Supabase query filter
  );

  if (isLoading) return <p>Loading...</p>;

  return (
    <ul>
      {data?.map((user) => (
        <li key={user.id}>
          {user.name} ({user.role})
        </li>
      ))}
    </ul>
  );
};

๐Ÿ“ฎ Mutations

Use the useClientMutate hook for inserting, updating, and deleting data on client components:

import { useClientMutate } from "@/hooks/useClientMutate";

const AddPost = () => {
  const mutation = useClientMutate("posts", "insert");

  const handleSubmit = async () => {
    mutation.mutate({ id: Date.now(), name: "New Post" });
  };

  return <button onClick={handleSubmit}>Add Post</button>;
};

Updating Data Example

const UpdatePost = () => {
  const mutation = useClientMutate("posts", "update");

  const handleUpdate = () => {
    mutation.mutate({ id: 1, name: "Updated Post" });
  };

  return <button onClick={handleUpdate}>Update User</button>;
};

Deleting Data Example

const DeleteUser = () => {
  const mutation = useClientMutate("users", "delete");

  const handleDelete = () => {
    mutation.mutate({ id: 1 });
  };

  return <button onClick={handleDelete}>Delete User</button>;
};

๐Ÿ— Folder Structure

๐Ÿ“ฆ my-project
โ”œโ”€โ”€ ๐Ÿ“‚ app                 # Next.js app directory
โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ (auth)           # Authentication pages
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ auth         # Authentication utilities
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ confirm  # Confirmation route
โ”‚   โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ route.ts
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ error        # Error handling
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ login        # Login page
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ register     # Register page
โ”‚   โ”‚   โ”œโ”€โ”€ actions.ts      # Auth actions
โ”‚   โ”‚   โ””โ”€โ”€ layout.tsx      # Auth layout
โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ (dashboard)      # Dashboard pages
โ”‚   โ”œโ”€โ”€ favicon.ico         # Favicon
โ”‚   โ”œโ”€โ”€ globals.css         # Global styles
โ”‚   โ”œโ”€โ”€ layout.tsx          # Main layout
โ”‚   โ”œโ”€โ”€ not-found.tsx       # 404 Page
โ”‚   โ””โ”€โ”€ page.tsx            # Home page
โ”œโ”€โ”€ ๐Ÿ“‚ components          # Shared UI components
โ”œโ”€โ”€ ๐Ÿ“‚ hooks               # Custom React Query hooks
โ”œ    โ””โ”€โ”€ use-client-fetch.ts
โ”œ    โ””โ”€โ”€ use-client-mutation.ts
โ”œโ”€โ”€ ๐Ÿ“‚ lib                 # Utilities & helpers
โ”œโ”€โ”€ ๐Ÿ“‚ public              # Static assets
โ”œโ”€โ”€ ๐Ÿ“‚ supabase            # Supabase integrations clients
โ”‚   โ”œโ”€โ”€ client.ts          # Supabase client
โ”‚   โ”œโ”€โ”€ middleware.ts      # Middleware configuration
โ”‚   โ””โ”€โ”€ server.ts          # Server-side Supabase utilities
โ”œโ”€โ”€ ๐Ÿ“‚ node_modules        # Dependencies
โ”œโ”€โ”€ .env                   # Environment configuration
โ”œโ”€โ”€ .env.example           # Example environment variables
โ”œโ”€โ”€ .gitignore             # Git ignore file
โ”œโ”€โ”€ components.json        # UI component configurations
โ”œโ”€โ”€ eslint.config.mjs      # ESLint configuration
โ”œโ”€โ”€ middleware.ts          # Global middleware
โ”œโ”€โ”€ next-env.d.ts          # Next.js environment types
โ”œโ”€โ”€ next.config.ts         # Next.js configuration
โ”œโ”€โ”€ package.json           # Project dependencies
โ”œโ”€โ”€ pnpm-lock.yaml         # Lock file
โ”œโ”€โ”€ postcss.config.mjs     # PostCSS configuration
โ”œโ”€โ”€ README.md              # Project documentation
โ”œโ”€โ”€ tailwind.config.ts     # Tailwind CSS configuration
โ””โ”€โ”€ tsconfig.json          # TypeScript configuration

๐Ÿ›  Technologies I Used

  • Next.js 15
  • Supabase
  • React Query
  • ShadCN Components
  • Tailwind CSS
  • Zod Validation
  • TypeScript

About

๐Ÿš€ A Next.js 15 + Supabase + React Query starter template with Tailwind CSS, shadcn/ui, and built-in authentication & database integration.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages