A production-ready authentication system built with Next.js and Supabase featuring Google OAuth integration.
- π Secure OAuth authentication with Google
- π Server-side and client-side authentication handling
- π‘οΈ Protected routes with Next.js middleware
- π SSR-compatible authentication flow
- π¦ Custom React hooks for authentication state
- π οΈ Row Level Security (RLS) with Supabase
Before you begin, ensure you have:
- Node.js 18.x or later
- A Supabase account
- A Google Cloud Platform account
git clone https://github.com/shsfwork/supabase-auth-nextjs-google-boilerplate.git
cd supabase-auth-nextjs-google-boilerplate
npm install
Create a .env.local
file in the root directory:
NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
- Create a new Supabase project
- Run the SQL commands from the
schema.sql
file in the Supabase SQL editor - Configure Google authentication provider in Authentication > Providers
- Add
http://localhost:3000/**
to the redirect URLs in Authentication > URL Configuration
- Create a project in Google Cloud Console
- Set up OAuth consent screen
- Create OAuth 2.0 client ID
- Configure authorized JavaScript origins and redirect URIs
npm run dev
Visit http://localhost:3000 to see your application.
βββ app/
β βββ (playground)/ # Example pages
β β βββ client/ # Client component example
β β βββ profile/ # Protected route example
β β βββ server/ # Server component example
β βββ auth/ # Authentication routes
β β βββ callback/ # OAuth callback handler
β β βββ login/ # Login page
β βββ layout.tsx # Root layout
β βββ page.tsx # Home page
βββ components/ # Reusable UI components
βββ constants/ # Application constants
βββ hooks/ # Custom React hooks
βββ lib/
β βββ supabase/ # Supabase clients and middleware
βββ middleware.ts # Next.js middleware for auth
βββ .env.local.example # Environment variables example
- User clicks the "Login with Google" button
- User is redirected to Google's OAuth consent screen
- After authorization, Google redirects back to the callback URL
- The callback handler exchanges the code for a Supabase session
- The user is redirected to the original destination or home page
- Profile routes are guarded by middleware
The middleware handles authentication checks before rendering protected routes:
- Redirects unauthenticated users to the login page
- Prevents authenticated users from accessing the login page
- Preserves the intended destination with the
next
parameter
A custom hook that provides the current user state in client components:
const { loading, error, user, session } = useUser();
if (loading) return <LoadingSpinner />;
if (error) return <ErrorMessage error={error} />;
if (!user) return <LoginPrompt />;
return <UserProfile user={user} />;
The project uses two different Supabase clients:
- Client-side: For use in client components and hooks
- Server-side: For use in server components and API routes
The project uses the following database structure:
CREATE TABLE public.users (
id UUID PRIMARY KEY REFERENCES auth.users(id) NOT NULL,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
-- Automatically insert user data when a new user is created
CREATE OR REPLACE FUNCTION public.create_new_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.users (id, name, email)
VALUES (
NEW.id,
NEW.raw_user_meta_data->>'full_name',
NEW.email
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.create_new_user();
-- Enable Row Level Security
ALTER TABLE public.users ENABLE ROW LEVEL SECURITY;
For production, update:
- Redirect URLs in Supabase to your production domain
- Authorized origins and redirect URIs in Google Cloud Console
- Environment variables for your production deployment
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you find this project helpful, please give it a βοΈ on GitHub!
For issues, questions, or contributions, please open an issue or PR on GitHub.