Skip to content

Commit d82cca5

Browse files
committed
docs(auth-security): add documentation for authentication flows, best practices, and token strategy
1 parent 2026b9e commit d82cca5

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

docs/auth-security/auth-flows.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Authentication Flows
2+
3+
This application uses [Supabase Auth](https://supabase.com/docs/guides/auth) as the authentication layer, integrated via the Supabase JavaScript SDK.
4+
5+
We support:
6+
7+
- OAuth with GitHub and Google
8+
- Session management via Supabase's client session
9+
10+
## OAuth Providers
11+
12+
Currently enabled providers:
13+
14+
- **GitHub**
15+
- **Google**
16+
17+
These providers are configured directly in the Supabase project settings, under **Authentication > Providers**.
18+
19+
## Frontend Flow (OAuth)
20+
21+
1. User clicks "Login with Google" or "Login with GitHub".
22+
2. Supabase SDK triggers OAuth redirect:
23+
24+
```javascript
25+
const { data, error } = await supabase.auth.signInWithOAuth({
26+
provider: "google", // or 'github'
27+
});
28+
```
29+
30+
3. After the OAuth handshake, the user is redirected back to the app with a valid session.
31+
4. The session is automatically persisted in local storage and available via:
32+
33+
```javascript
34+
const {
35+
data: { session },
36+
} = await supabase.auth.getSession();
37+
```
38+
39+
## Session Handling
40+
41+
- Sessions are auto-refreshed via the SDK.
42+
- On each load, use `supabase.auth.getSession()` to verify and hydrate the current session state.
43+
- Auth state changes can be subscribed to:
44+
45+
```javascript
46+
supabase.auth.onAuthStateChange((event, session) => {
47+
// handle login, logout, token refresh
48+
});
49+
```
50+
51+
## Server-Side Session
52+
53+
For SSR or protected API routes, the JWT in the Supabase session must be verified. This is optional if the app is frontend-only.

docs/auth-security/best-practices.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Auth & Security Best Practices
2+
3+
- ✅ Always use `NEXT_PUBLIC_SUPABASE_` for keys meant to be exposed in frontend.
4+
- ✅ Use environment variables to avoid leaking secrets in the repo.
5+
- ✅ Handle auth errors explicitly and show user-friendly messages.
6+
- ✅ Validate sessions on page load and redirect unauthorized users to login.
7+
- ✅ Use Supabase's RLS (Row Level Security) to protect data access on the backend.
8+
- ✅ Secure protected API routes by validating the JWT if applicable.
9+
- ✅ Avoid relying only on `localStorage` for critical auth checks (can be manipulated).
10+
11+
## Recommended Setup
12+
13+
```typescript
14+
// lib/supabase/client.ts
15+
export const supabase = createClient(
16+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
17+
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
18+
);
19+
```

docs/auth-security/github.md

Whitespace-only changes.

docs/auth-security/google.md

Whitespace-only changes.

docs/auth-security/token-strategy.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Token Strategy
2+
3+
## Storage
4+
5+
- Supabase handles tokens internally.
6+
- Access token and refresh token are stored in `localStorage` by default.
7+
- You can configure session persistence as `"session"` or `"none"`:
8+
9+
```ts
10+
createClient(SUPABASE_URL, SUPABASE_KEY, {
11+
auth: {
12+
storage: localStorage,
13+
autoRefreshToken: true,
14+
persistSession: true,
15+
},
16+
});
17+
```
18+
19+
```
20+
21+
```
22+
23+
## Environment Variables
24+
25+
All environment-specific secrets are stored in `.env` or `.env.local`:
26+
27+
```env
28+
NEXT_PUBLIC_SUPABASE_URL=https://xyzcompany.supabase.co
29+
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1...
30+
```
31+
32+
⚠️ Never expose the `service_role` key on the frontend — only use `anon` keys client-side.
33+
34+
---
35+
36+
## 📁 `docs/auth-security/best-practices.md`
37+
38+
```md
39+
40+
```

0 commit comments

Comments
 (0)