Skip to main content

Overview

EpiNeko uses Supabase Auth for secure, scalable authentication. The system supports email/password authentication with a unique username-based login feature.
All authentication operations are handled through Next.js Server Actions, providing a seamless developer experience without the need for separate API routes.

Authentication Architecture

EpiNeko implements a multi-layered authentication system:

Supabase Client Setup

EpiNeko uses two different Supabase clients depending on the execution context:

Browser Client

Used in client components for client-side operations:

Server Client

Used in server components and server actions for server-side operations:
Always use the appropriate client for your context. Using the browser client in server components or vice versa will cause errors.

Authentication Flows

User Signup

The signup flow creates a new user account and automatically generates a profile:
User metadata (username and full_name) is passed during signup and automatically used by the database trigger to create the profile.

User Login

EpiNeko supports login with both email and username:
1

User enters identifier

The user provides either an email or username along with their password
2

Identifier validation

The system checks if the identifier contains ’@’ to determine if it’s an email
3

Username lookup

If it’s a username, the system queries the profiles table to find the associated email
4

Authentication

Supabase Auth validates the email and password combination
5

Session creation

On success, a session is created and cookies are set

User Logout

Simple logout functionality that clears the session:

Session Management

Middleware

EpiNeko uses Next.js middleware to automatically refresh user sessions on every request:
The middleware automatically refreshes the authentication token on every request, ensuring users stay logged in without manual intervention.

Profile Management

Automatic Profile Creation

Profiles are automatically created when a user signs up through a database trigger:
supabase/migrations/20260218_initial_schema.sql

Row Level Security (RLS)

Profiles are protected with Row Level Security policies:
RLS policies ensure that users can only modify their own profiles while still allowing public viewing of all profiles.

Getting the Current User

Fetch the authenticated user in server components:

Protecting Routes

Create protected routes by checking authentication status:
app/protected/page.tsx
Always validate authentication on the server side, never rely solely on client-side checks for security.

Customization

Adding Social Authentication

To add OAuth providers (Google, GitHub, etc.):
  1. Enable the provider in Supabase Dashboard (Authentication > Providers)
  2. Add the OAuth redirect URL to your provider’s app settings
  3. Update your login page with social buttons:

Email Verification

Enable email verification in Supabase Dashboard (Authentication > Email Templates):

Password Recovery

Implement password reset:

Troubleshooting

Ensure middleware is properly configured and running on all routes. Check that cookies are being set correctly.
Verify that the username is stored in the profiles table and is properly indexed. Check that the email column is added to profiles.
Check that the database trigger is installed correctly. Verify that user metadata is being passed during signup.