Explore skills. Ship every project faster.
One click to add expert know-how to any prompt.
Popular skill categories
Skills library
Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
by Sticklight
•
2K+ Uses
Kill list of visual cliches, layout tropes, and UI patterns that make frontends look AI-generated. ALWAYS trigger this skill alongside frontend-design whenever Claude builds any frontend — websites, landing pages, dashboards, React components, HTML pages, artifacts, or any visual UI. Also trigger when users say "make it look less AI," "this looks generic," "don't make it look like every other AI site," "avoid the AI look," "no AI slop," or when reviewing/auditing any frontend output. This is a companion checklist of what to NEVER do — the frontend-design skill handles what TO do.
by Sticklight
•
1K+ Uses
Build interactive 3D scenes for the web using Three.js r128. Use this skill ONLY when the user explicitly asks for 3D — product viewers, 3D hero backgrounds, interactive geometry, particle scenes, or scroll-driven 3D camera work. Do NOT trigger for standard 2D websites, CSS animations, or SVG graphics. Trigger words: 3D, three.js, WebGL, 3D scene, product viewer, 3D background, interactive 3D, rotating model, particle system, 3D experience.
by Sticklight
•
900+ Uses
Review a feature and enhance it with purposeful animations, micro-interactions, and motion effects that improve usability and delight. Use when the user mentions adding animation, transitions, micro-interactions, motion design, hover effects, or making the UI feel more alive.
by Sticklight
•
900+ Uses
Measurable UX rules, UI style catalog, industry-specific guidance, and pre-delivery checklist for any frontend project. ALWAYS trigger this skill alongside frontend-design when building websites, landing pages, dashboards, apps, React components, or any interactive UI. Also trigger when users mention "UX," "usability," "accessibility," "a11y," "WCAG," "responsive," "mobile-friendly," "user experience," "UI patterns," "checklist," or ask to audit, review, or improve the usability of any frontend. This skill provides the hard numbers — minimum sizes, contrast ratios, timing values, spacing rules — so Claude doesn't guess.
by Sticklight
•
700+ Uses
Make websites, apps, and dashboards work beautifully across screen sizes. Use when the user mentions responsive, mobile, tablet, breakpoints, adaptive layout, touch targets, or cross-device — or when building any UI that needs to work beyond desktop. Self-contained — no other skills required.
by Sticklight
•
500+ Uses
Authentication and authorization patterns for React + Vite + Tailwind apps using Cloud Backend (Supabase). Use when implementing login, signup, password reset, session management, protected routes, Row Level Security (RLS), role-based access control, or user data tables. Covers: profiles table with auto-creation trigger, user_roles table, AuthContext with getUser() (not getSession()), ProtectedRoute component, full auth pages (Login, Signup, Forgot Password, Reset Password with expired link handling), logout, user-owned data with RLS policies, and security best practices.
by Sticklight
•
400+ Uses
Add moments of joy, personality, and unexpected touches that make interfaces memorable and enjoyable to use. Elevates functional to delightful. Use when the user asks to add polish, personality, animations, micro-interactions, delight, or make an interface feel fun or memorable.
by Sticklight
•
400+ Uses
Performs a final quality pass fixing alignment, spacing, consistency, and micro-detail issues before shipping. Elevates good work to great work.
by Sticklight
•
400+ Uses
SEO best practices for React + Vite + Tailwind CSS apps built with sticklight.com. Use when building or reviewing web pages, writing content for the web, generating meta tags, improving search rankings, setting up sitemap or robots.txt, adding structured data (JSON-LD), or optimizing images and performance in a Vite-based SPA. Covers: static fallback meta tags, dynamic SEO with @unhead/react, useSEO hook, sitemaps (dynamic via Edge Function and static), semantic HTML, heading hierarchy, image SEO, link best practices, URL structure, Core Web Vitals, favicon and OG image specs, mobile-first design, and IndexNow integration for instant search engine notifications.
by Sticklight
•
400+ Uses
Mobile-first design system for building iOS and Android apps with React Native, Flutter, or native frameworks. Covers touch interaction, performance, platform conventions, and visual quality. Prevents generic AI mobile aesthetics (pill overload, glassmorphism shells, gradient hero sections, metric-card grids) and enforces human-designed, functional UI. Use whenever building any mobile app, mobile component, React Native screen, Flutter widget, or mobile UI — even if the user doesn't say "mobile design system." Also trigger when reviewing mobile code for UX or performance issues, or when the user describes an app that "looks too AI-generated" or "generic."
by Sticklight
•
300+ Uses
Core Web Vitals optimization for Sticklight React+Vite+Tailwind apps. Use when the user asks to improve load speed, Lighthouse scores, LCP/FCP/CLS/INP/TBT, or fix slow or broken pages after refresh. Covers images, fonts, scripts, CSS animation, listeners, and bundle size.
by Sticklight
•
200+ Uses
Guide for creating or improving Sticklight user skills (specialized instructions stored in the Skills editor). Use when the user wants to add a new skill, rewrite skill content, tune descriptions for better triggering, or migrate ideas from file-based skill docs into Sticklight's DB format.
by Sticklight
•
100+ Uses
Shadow and elevation system for creating depth hierarchy. Covers sharp, soft, glow, layered, and neumorphic shadow styles. Includes tinted shadows, dark mode patterns, performant animations, and elevation formulas. Use when adding shadows, depth, or elevation to any UI element.
by Sticklight
•
100+ Uses
Build a live admin dashboard for WooCommerce/WordPress: product, order, and category management pulling real store data. Trigger when the user wants an admin panel, dashboard, or back-office to manage a WordPress or WooCommerce store — not a public storefront.
by Sticklight
•
100+ Uses
auth
Authentication and authorization patterns for React + Vite + Tailwind apps using Cloud Backend (Supabase). Use when implementing login, signup, password reset, session management, protected routes, Row Level Security (RLS), role-based access control, or user data tables. Covers: profiles table with auto-creation trigger, user_roles table, AuthContext with getUser() (not getSession()), ProtectedRoute component, full auth pages (Login, Signup, Forgot Password, Reset Password with expired link handling), logout, user-owned data with RLS policies, and security best practices.
by Sticklight
•
400+ Uses
# Authentication Skill for Sticklight Apps
This skill helps you implement user authentication using Cloud Backend.
## Your Task
When this skill is active, follow these steps:
1. **Assess the current state** — Check if the project already has auth setup (AuthContext, ProtectedRoute, auth pages, database tables)
2. **Set up the database** — Create the `profiles` table with auto-creation trigger, and optionally the `user_roles` table
3. **Create AuthContext** — Build `src/contexts/AuthContext.tsx` with `getUser()`, `signUp`, `signIn`, `signOut`, and `resetPassword`
4. **Wrap the app** — Add `<AuthProvider>` to `src/main.tsx`
5. **Create ProtectedRoute** — Build `src/components/ProtectedRoute.tsx` to guard authenticated pages
6. **Build auth pages** — Create Login, Signup, Forgot Password, and Reset Password pages with proper error handling
7. **Add logout** — Add a sign-out button/flow that clears the session and redirects to login
8. **Secure data tables** — Enable RLS on every user-facing table, add `user_id` column, write proper policies
9. **Verify** — Run through the Auth Checklist (section 10) before shipping
## Overview
Cloud Backend provides built-in authentication with:
- Email/password authentication
- Session management
- Secure token handling
- Row Level Security (RLS) integration
---
## 1. Database Setup
### 1.1 User Profiles Table
Every auth setup should include a `profiles` table to store additional user data.
```sql
CREATE TABLE public.profiles (
id UUID NOT NULL PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
email TEXT,
full_name TEXT,
avatar_url TEXT,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);
ALTER TABLE public.profiles ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can read own profile"
ON public.profiles FOR SELECT
USING ((SELECT auth.uid()) = id);
CREATE POLICY "Users can update own profile"
ON public.profiles FOR UPDATE
USING ((SELECT auth.uid()) = id);
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER
LANGUAGE plpgsql
SECURITY DEFINER SET search_path = public
AS $$
BEGIN
INSERT INTO public.profiles (id, email, full_name)
VALUES (
NEW.id,
NEW.email,
COALESCE(NEW.raw_user_meta_data->>'full_name', '')
);
RETURN NEW;
END;
$$;
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
```
### 1.2 User Roles Table (Optional)
For admin/editor roles, create a separate table. Never store roles in `user_metadata`.
```sql
CREATE TABLE public.user_roles (
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
role TEXT NOT NULL DEFAULT 'user' CHECK (role IN ('user', 'editor', 'admin')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
UNIQUE(user_id)
);
ALTER TABLE public.user_roles ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can read own role"
ON public.user_roles FOR SELECT
USING ((SELECT auth.uid()) = user_id);
```
> **⚠️ Before writing any role-gated policy:** NEVER write a policy that subqueries the table it protects — e.g. a policy ON the roles table with `EXISTS (SELECT ... FROM user_roles ...)` in USING. The migration succeeds, then every query fails at runtime with `42P17 infinite recursion`. This applies to ANY authorization-table shape (roles, memberships, profiles-with-role), whatever it's named. Route the lookup through a `SECURITY DEFINER` boolean helper instead — the `supabaseMigration` tool documentation (Function Security → Exception 2) has the exact wrong-vs-right SQL, including the required `GRANT EXECUTE ... TO authenticated`.
>
> **⚠️ And the write side:** an INSERT/UPDATE policy on that same table with only `WITH CHECK (auth.uid() = user_id)` lets any signed-up user insert themselves as `role = 'owner'` in any tenant. Anchor the write to a row the caller already owns — wrong-vs-right SQL in `supabaseMigration` (Function Security → Exception 2b).
---
## 2. Auth Context
Create `src/contexts/AuthContext.tsx`:
> **⚠️ The `onAuthStateChange` callback MUST be synchronous.** NEVER `await` a Supabase call inside it — not `getUser()`, not `.from('profiles')`. The client fires this callback while it is still initializing (it refreshes a stored token and notifies subscribers before initialization resolves), and every Supabase call first waits for initialization to finish. So the callback waits on the client and the client waits on the callback: the promise never settles, the initial `getUser()` below never returns, and the app sits on its loading spinner forever — on every visit that arrives with a stored session. Read the user off the `session` argument (UI state only; the `getUser()` call below is what verifies identity against the server) and do any profile/role fetch in a separate effect keyed on the user id. Do **not** `setLoading(false)` in this callback — a stored token can still be revoked, and clearing the loader here would flash protected UI before `getUser()` returns.
```typescript
import { createContext, useContext, useEffect, useState, useCallback, ReactNode, useRef } from 'react';
import { supabase } from '@/integrations/supabase/client';
import type { User } from '@supabase/supabase-js';
interface AuthContextType {
user: User | null;
loading: boolean;
signUp: (email: string, password: string, fullName?: string) => Promise<{ success: boolean; error?: string; needsEmailConfirmation?: boolean }>;
signIn: (email: string, password: string) => Promise<{ success: boolean; error?: string }>;
signOut: () => Promise<void>;
resetPassword: (email: string) => Promise<{ success: boolean; error?: string }>;
}
const AuthContext = createContext<AuthContextType | null>(null);
export function AuthProvider({ children }: { children: ReactNode }) {
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState(true);
const initialized = useRef(false);
useEffect(() => {
if (!supabase || initialized.current) {
setLoading(false);
return;
}
initialized.current = true;
// Get initial user (verified from server)
supabase.auth.getUser().then(({ data: { user } }) => {
setUser(user);
setLoading(false);
});
// Listen for auth changes — synchronous only, never await in here.
// Do not clear loading here; getUser() above is what drops the spinner.
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
setUser(session?.user ?? null);
});
return () => subscription.unsubscribe();
}, []);
const signUp = useCallback(async (
email: string,
password: string,
fullName?: string
): Promise<{ success: boolean; error?: string; needsEmailConfirmation?: boolean }> => {
if (!supabase) return { success: false, error: 'Database not connected' };
const { data, error } = await supabase.auth.signUp({
email,
password,
options: {
data: { full_name: fullName }
}
});
if (error) return { success: false, error: error.message };
if (!data.user) return { success: false, error: 'Signup failed' };
// A session here means the user is already signed in and no confirmation
// email was sent. Only a missing session means one is on its way.
return { success: true, needsEmailConfirmation: !data.session };
}, []);
const signIn = useCallback(async (
email: string,
password: string
): Promise<{ success: boolean; error?: string }> => {
if (!supabase) return { success: false, error: 'Database not connected' };
const { data, error } = await supabase.auth.signInWithPassword({
email,
password
});
if (error) return { success: false, error: error.message };
if (!data.user) return { success: false, error: 'Login failed' };
setUser(data.user);
return { success: true };
}, []);
const signOut = useCallback(async () => {
if (!supabase) return;
await supabase.auth.signOut();
setUser(null);
}, []);
const resetPassword = useCallback(async (
email: string
): Promise<{ success: boolean; error?: string }> => {
if (!supabase) return { success: false, error: 'Database not connected' };
const { error } = await supabase.auth.resetPasswordForEmail(email, {
redirectTo: `${window.location.origin}/reset-password`
});
if (error) return { success: false, error: error.message };
return { success: true };
}, []);
return (
<AuthContext.Provider value={{ user, loading, signUp, signIn, signOut, resetPassword }}>
{children}
</AuthContext.Provider>
);
}
export function useAuth() {
const context = useContext(AuthContext);
if (!context) {
throw new Error('useAuth must be used within AuthProvider');
}
return context;
}
```
Wrap your app in `src/main.tsx`:
```typescript
import { AuthProvider } from '@/contexts/AuthContext';
<AuthProvider>
<App />
</AuthProvider>
```
---
## 3. Protected Routes
Create `src/components/ProtectedRoute.tsx`:
```typescript
import { useEffect } from 'react';
import { useNavigate } from 'react-router';
import { useAuth } from '@/contexts/AuthContext';
interface ProtectedRouteProps {
children: React.ReactNode;
fallbackPath?: string;
}
export function ProtectedRoute({ children, fallbackPath = '/login' }: ProtectedRouteProps) {
const { user, loading } = useAuth();
const navigate = useNavigate();
useEffect(() => {
if (!loading && !user) {
navigate(fallbackPath, { replace: true });
}
}, [loading, user, navigate, fallbackPath]);
if (loading) {
return (
<div className="flex min-h-screen items-center justify-center">
<div className="h-8 w-8 animate-spin rounded-full border-4 border-gray-200 border-t-blue-500" />
</div>
);
}
if (!user) {
return null;
}
return <>{children}</>;
}
```
Usage in routes:
```typescript
<Route path="/dashboard" element={
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
} />
```
---
## 4. Auth Pages
### 4.1 Login Page
```typescript
import { useState } from 'react';
import { useNavigate } from 'react-router';
import { useAuth } from '@/contexts/AuthContext';
export default function Login() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const navigate = useNavigate();
const { signIn } = useAuth();
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setError(null);
setLoading(true);
const result = await signIn(email, password);
if (result.success) {
navigate('/dashboard');
} else {
setError(result.error || 'Login failed');
}
setLoading(false);
}
return (
<form onSubmit={handleSubmit}>
{error && <div>{error}</div>}
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Email"
required
/>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
required
/>
<button type="submit" disabled={loading}>
{loading ? 'Signing in...' : 'Sign In'}
</button>
</form>
);
}
```
### 4.2 Signup Page
```typescript
import { useState } from 'react';
import { useNavigate } from 'react-router';
import { useAuth } from '@/contexts/AuthContext';
export default function Signup() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [fullName, setFullName] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
const { signUp } = useAuth();
const navigate = useNavigate();
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setError(null);
setLoading(true);
const result = await signUp(email, password, fullName);
if (!result.success) {
setError(result.error || 'Signup failed');
} else if (result.needsEmailConfirmation) {
setSuccess(true);
} else {
// Signed in already — never park a confirmed user on a waiting screen.
navigate('/');
}
setLoading(false);
}
if (success) {
return <div>Check your email for a confirmation link.</div>;
}
return (
<form onSubmit={handleSubmit}>
{error && <div>{error}</div>}
<input
type="text"
value={fullName}
onChange={(e) => setFullName(e.target.value)}
placeholder="Full Name"
/>
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Email"
required
/>
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
minLength={6}
required
/>
<button type="submit" disabled={loading}>
{loading ? 'Creating account...' : 'Sign Up'}
</button>
</form>
);
}
```
### 4.3 Forgot Password Page
```typescript
import { useState } from 'react';
import { useAuth } from '@/contexts/AuthContext';
export default function ForgotPassword() {
const [email, setEmail] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
const { resetPassword } = useAuth();
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setError(null);
setLoading(true);
const result = await resetPassword(email);
if (result.success) {
setSuccess(true);
} else {
setError(result.error || 'Failed to send reset email');
}
setLoading(false);
}
if (success) {
return <div>Check your email for a password reset link.</div>;
}
return (
<form onSubmit={handleSubmit}>
{error && <div>{error}</div>}
<input
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="Email"
required
/>
<button type="submit" disabled={loading}>
{loading ? 'Sending...' : 'Send Reset Link'}
</button>
</form>
);
}
```
### 4.4 Reset Password Page (with Error Handling)
This page handles expired links, invalid tokens, and shows appropriate error messages.
#### Default Email Service
Cloud Backend includes a built-in email service that works out of the box. No SMTP configuration needed.
| Aspect | Default Service |
|--------|-----------------|
| Sender | `[email protected]` |
| Rate limit | ~4 emails/hour per user |
| Link expiration | 1 hour |
For production, consider configuring a custom SMTP for branded emails and higher limits.
#### Error Handling
When a reset link is invalid or expired, Cloud Backend redirects with error parameters in the URL hash:
```
/reset-password#error=access_denied&error_code=otp_expired&error_description=Email+link+is+invalid+or+has+expired
```
**Common error codes:**
| Error Code | Meaning | User Action |
|------------|---------|-------------|
| `otp_expired` | Link expired (default: 1 hour) | Request new link |
| `access_denied` | Link already used or invalid | Request new link |
Always parse the URL hash on page load and show a helpful message with option to request a new link.
#### Component
```typescript
import { useState, useEffect, useRef } from 'react';
import { useNavigate, Link } from 'react-router';
import { supabase } from '@/integrations/supabase/client';
export default function ResetPassword() {
const [password, setPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
const [sessionReady, setSessionReady] = useState(false);
const [linkError, setLinkError] = useState<string | null>(null);
const sessionReadyRef = useRef(false);
const navigate = useNavigate();
useEffect(() => {
// Check for error in URL hash (e.g., expired link)
const hash = window.location.hash;
if (hash) {
const params = new URLSearchParams(hash.substring(1));
const errorCode = params.get('error_code');
const errorDescription = params.get('error_description');
if (errorCode || errorDescription) {
if (errorCode === 'otp_expired') {
setLinkError('This reset link has expired. Please request a new one.');
} else {
setLinkError(errorDescription?.replace(/\+/g, ' ') || 'Invalid reset link.');
}
return;
}
}
if (!supabase) return;
const markReady = () => {
sessionReadyRef.current = true;
setSessionReady(true);
};
// Primary signal: supabase-js fires PASSWORD_RECOVERY after parsing the
// recovery token from the URL hash. This is the only event we trust.
const { data: { subscription } } = supabase.auth.onAuthStateChange((event) => {
if (event === 'PASSWORD_RECOVERY' || event === 'SIGNED_IN') {
markReady();
}
});
// Fallback: if the recovery event already fired before this component mounted
// (e.g. user navigated client-side after landing), verify with the server.
// NEVER use getSession() here — it reads from local storage without server
// verification and is not safe for security gating (see Section 9).
supabase.auth.getUser().then(({ data: { user } }) => {
if (user) markReady();
});
// Show a clear error if neither path produced a verified session.
const timeout = setTimeout(() => {
if (!sessionReadyRef.current) {
setLinkError('Unable to verify reset link. It may have expired.');
}
}, 5000);
return () => {
subscription.unsubscribe();
clearTimeout(timeout);
};
}, []);
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setError(null);
if (password !== confirmPassword) {
setError('Passwords do not match');
return;
}
if (password.length < 6) {
setError('Password must be at least 6 characters');
return;
}
setLoading(true);
if (!supabase) {
setError('Database not connected');
setLoading(false);
return;
}
const { error: updateError } = await supabase.auth.updateUser({ password });
if (updateError) {
setError(updateError.message);
setLoading(false);
} else {
setSuccess(true);
await supabase.auth.signOut();
setTimeout(() => navigate('/login'), 2000);
}
}
// Show error if link is expired/invalid
if (linkError) {
return (
<div>
<p>{linkError}</p>
<Link to="/forgot-password">Request New Link</Link>
</div>
);
}
if (!sessionReady) {
return <div>Verifying reset link...</div>;
}
if (success) {
return <div>Password updated! Redirecting to login...</div>;
}
return (
<form onSubmit={handleSubmit}>
{error && <div>{error}</div>}
<input
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="New Password"
minLength={6}
required
/>
<input
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
placeholder="Confirm Password"
minLength={6}
required
/>
<button type="submit" disabled={loading}>
{loading ? 'Updating...' : 'Update Password'}
</button>
</form>
);
}
```
---
## 5. Logout
```typescript
import { useAuth } from '@/contexts/AuthContext';
import { useNavigate } from 'react-router';
function LogoutButton() {
const { signOut } = useAuth();
const navigate = useNavigate();
async function handleLogout() {
await signOut();
navigate('/login');
}
return <button onClick={handleLogout}>Sign Out</button>;
}
```
---
## 6. Creating the First User
1. Build the signup page and navigate to `/signup`
2. Create an account with email and password
3. The user is created automatically
Alternatively:
1. Go to **Cloud** (cloud icon in top menu)
2. Click **Auth users** in the Cloud Backend sidebar
3. Click **Add User**
4. Enter email and password
---
## 7. Promoting a User to Admin
1. Go to **Cloud** (cloud icon in top menu)
2. Click **Database** in the Cloud Backend sidebar
3. Select the `user_roles` table
4. Click **Add Row**
5. Set `user_id` (copy from **Studio → Cloud → Auth users** → click user → copy ID)
6. Set `role` to `admin`
7. Save
---
## 8. User Data with RLS
When creating tables that store user-specific data, always include `user_id` and proper RLS policies:
```sql
CREATE TABLE public.user_items (
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now()
);
ALTER TABLE public.user_items ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own items"
ON public.user_items FOR ALL
USING ((SELECT auth.uid()) = user_id)
WITH CHECK ((SELECT auth.uid()) = user_id);
```
Query with user filter:
```typescript
const { data } = await supabase
.from('user_items')
.select('id, title, created_at')
.eq('user_id', user.id);
```
---
## 9. Security Rules
1. **Always use `getUser()` for authorization** — it verifies with the server. Never use `getSession()` for security decisions. `session.user` in `onAuthStateChange` is UI state only (who to show in the header). Real access control is RLS in the database; if the client itself must gate something, call `getUser()`.
2. **Never store roles in user_metadata** — users can modify their own metadata. Use a database table.
3. **Always enable RLS** — every user table must have Row Level Security enabled with proper policies.
4. **Password requirements** — enforce minimum 6 characters (Supabase default). Add additional validation as needed.
5. **Protect routes** — wrap authenticated pages with `ProtectedRoute` component.
---
## Process
When adding auth to a project, follow this workflow:
1. Check if Cloud Backend is enabled — if not, enable it via `enableSticklightCloud`
2. Run the SQL to create `profiles` table, trigger, and RLS policies
3. If roles are needed, run the SQL to create `user_roles` table with RLS
4. Check if `AuthContext` exists — if not, create `src/contexts/AuthContext.tsx` with all auth functions
5. Verify the app is wrapped with `<AuthProvider>` in `src/main.tsx`
6. Check if `ProtectedRoute` exists — if not, create `src/components/ProtectedRoute.tsx`
7. Create auth pages that are missing: Login, Signup, Forgot Password, Reset Password
8. Ensure Reset Password handles expired/invalid links by parsing URL hash errors
9. Add a logout button or flow to the authenticated layout
10. For every new table with user-owned data, add `user_id` column, enable RLS, and write policies
11. Verify: use `getUser()` (not `getSession()`) for all authorization checks
12. Verify: roles are stored in `user_roles` table, never in `user_metadata`
13. Run through the Auth Checklist (section 10)
---
## Output Format
When generating auth setup for a project, include:
1. **SQL migrations** — `profiles` table, trigger, RLS policies, and optionally `user_roles` table
2. **`src/contexts/AuthContext.tsx`** — full context with `useAuth()` hook
3. **`src/components/ProtectedRoute.tsx`** — route guard component
4. **Auth pages** — Login, Signup, Forgot Password, Reset Password (each with loading state, error display, and success handling)
5. **Logout component** — button or menu item that calls `signOut()` and redirects
When generating a new user-owned data table, include:
1. **SQL** — table definition with `user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE`
2. **RLS** — `ALTER TABLE ... ENABLE ROW LEVEL SECURITY` and policies using `(SELECT auth.uid()) = user_id`
3. **Query example** — TypeScript code showing how to query with the user filter
When modifying existing auth code, preserve:
1. The `{ success: boolean; error?: string }` return pattern for all auth operations
2. The `useCallback` wrapping on auth functions
3. The `initialized` ref guard to prevent double-mount issues
4. Null-safe `if (!supabase)` checks
---
## 10. Auth Checklist
- [ ] Enable Cloud Backend
- [ ] Create `profiles` table with auto-creation trigger
- [ ] Create `user_roles` table (if roles needed)
- [ ] Create `AuthContext` with all auth functions
- [ ] Verify the `onAuthStateChange` callback contains no `await` — one awaited Supabase call in there hangs the app on its loader forever
- [ ] Verify signup navigates into the app when `signUp()` returns a session, instead of always showing "check your email"
- [ ] Wrap app with `AuthProvider`
- [ ] Create `ProtectedRoute` component
- [ ] Create Login page
- [ ] Create Signup page
- [ ] Create Forgot Password page
- [ ] Create Reset Password page (with expired link handling)
- [ ] Add Logout functionality
- [ ] Create first user via signup or **Studio → Cloud → Auth users**
- [ ] Promote user to admin via **Studio → Cloud → Database** (if roles needed)
- [ ] Add `user_id` to all user-owned tables
- [ ] Enable RLS on all user tables