A modern, real-time chat application built with Next.js 14, Tailwind CSS, and Supabase. Features instant messaging, user authentication, and a beautiful, responsive UI.
- π Secure Authentication - Email & password authentication using Supabase Auth
- π‘οΈ Protected Routes - Middleware-based route protection
- π¬ Real-time Messaging - Instant message delivery using Supabase Realtime
- π¨ Modern UI - Clean, responsive design with Tailwind CSS
- π± Message Bubbles - Self messages on right, others on left
- β° Timestamps - Message timestamps displayed
- π€ User Profiles - Username display in chat
- π Fast & Scalable - Built with Next.js 14 App Router
- Framework: Next.js 14 (App Router)
- UI Library: React 18
- Styling: Tailwind CSS
- Backend: Supabase
- Authentication
- PostgreSQL Database
- Real-time Subscriptions
- Deployment: Ready for Vercel, Netlify, or any Node.js hosting
- Node.js 18+ installed
- npm or yarn package manager
- A Supabase account (free tier works perfectly!)
git clone https://github.com/yourusername/livechat.git
cd livechatnpm install- Go to Supabase and create a new project
- Wait for your project to be fully provisioned
- Go to SQL Editor in your Supabase dashboard
- Copy and paste the contents of
supabase/schema.sqlinto the SQL Editor - Run the SQL script to create the necessary tables and policies
- Important: If you get RLS (Row Level Security) errors, also run
supabase/fix_rls.sqlto create the auto-user-profile trigger - Go to Settings β API to get your project URL and anon key
-
Copy
.env.exampleto.env.local:cp .env.example .env.local
-
Fill in your Supabase credentials in
.env.local:NEXT_PUBLIC_SUPABASE_URL=your_supabase_project_url NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key NEXT_PUBLIC_SITE_URL=http://localhost:3000
Note: Get your Supabase credentials from Settings β API in your Supabase dashboard
Note: Supabase Realtime is available on the Free Plan with 2 million real-time messages per month and 200 peak concurrent connections - perfect for this chat application!
- Go to Database β Replication in your Supabase dashboard
- Find the
messagestable - Toggle the replication switch to enable real-time updates
npm run devOpen http://localhost:3000 in your browser.
livechat/
βββ app/
β βββ auth/
β β βββ signout/
β β βββ route.js # Sign out API route
β βββ chat/
β β βββ page.jsx # Chat page (protected)
β βββ login/
β β βββ page.jsx # Login page
β βββ register/
β β βββ page.jsx # Register page
β βββ globals.css # Global styles
β βββ layout.jsx # Root layout
β βββ page.jsx # Home page (redirects to login)
βββ components/
β βββ AuthForm.jsx # Reusable auth form
β βββ ChatBox.jsx # Main chat component
β βββ MessageBubble.jsx # Individual message component
β βββ SignOutButton.jsx # Sign out button component
βββ lib/
β βββ supabaseClient.js # Client-side Supabase client
β βββ supabaseServer.js # Server-side Supabase client
βββ supabase/
β βββ schema.sql # Database schema
β βββ fix_rls.sql # RLS policy fixes
β βββ security_fix.sql # Security improvements
βββ middleware.js # Route protection middleware
βββ package.json
βββ tailwind.config.js
βββ next.config.js
βββ README.md
- Users register/login through Supabase Auth
- On registration, a user profile is automatically created in the
userstable via database trigger - Middleware protects
/chatroute and redirects unauthenticated users - Authenticated users are redirected away from login/register pages
- Messages are stored in the
messagestable - Supabase Realtime subscription listens for new message inserts
- When a new message is inserted, all connected clients receive it instantly
- Messages auto-scroll to the latest
- users: Stores user profiles (id, username, created_at)
- messages: Stores chat messages (id, user_id, content, created_at)
Row Level Security (RLS) policies ensure:
- Users can read all profiles and messages
- Users can only insert/update their own data
- Push your code to GitHub
- Import your repository on Vercel
- Add your environment variables in Vercel dashboard
- Deploy!
- Push your code to GitHub
- Import your repository on Netlify
- Add your environment variables in Netlify dashboard
- Set build command:
npm run build - Set publish directory:
.next - Deploy!
Make sure to set these in your hosting platform:
NEXT_PUBLIC_SUPABASE_URLNEXT_PUBLIC_SUPABASE_ANON_KEYNEXT_PUBLIC_SITE_URL(your production URL)
Modify tailwind.config.js to customize the design system, or edit component styles directly.
- Add online status indicators
- Implement message reactions
- Add file/image uploads
- Create private/direct messages
- Add message editing/deletion
- Typing indicators
- Message search functionality
- Ensure Realtime is enabled for the
messagestable in Supabase - Check that the Realtime publication includes the
messagestable - Verify your Supabase project has Realtime enabled
- Verify your Supabase URL and anon key in
.env.local - Important: Disable email confirmation in Supabase Dashboard β Authentication β Settings β Email Auth β Disable "Enable email confirmations" (for development)
- Ensure the
userstable has proper RLS policies - If you see "new row violates row-level security policy" error, run
supabase/fix_rls.sqlin SQL Editor - If login doesn't work even with correct credentials, check browser console (F12) for errors
- Clear browser cookies and localStorage, then try again
- Function search_path mutable: Run
supabase/security_fix.sqlto fix this warning - Leaked password protection: Enable this in Supabase Dashboard β Authentication β Password β Enable "Leaked Password Protection"
- Make sure all environment variables are set
- Run
npm installto ensure all dependencies are installed - Check Node.js version (should be 18+)
For more detailed troubleshooting, see TROUBLESHOOTING.md
npm run dev- Start development servernpm run build- Build for productionnpm run start- Start production servernpm run lint- Run ESLint
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Next.js for the amazing framework
- Supabase for the backend infrastructure
- Tailwind CSS for the utility-first CSS framework
If you have any questions or suggestions, please open an issue on GitHub.
Made with β€οΈ using Next.js and Supabase