@edcalderon/auth 1.1.2 โ 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/README.md +139 -395
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -4,500 +4,244 @@
|
|
|
4
4
|
[](https://www.npmjs.com/package/@edcalderon/auth)
|
|
5
5
|
[](https://github.com/edcalderon/my-second-brain/tree/main/packages/auth)
|
|
6
6
|
|
|
7
|
-
A universal, **provider-agnostic** authentication orchestration package for
|
|
7
|
+
A universal, **provider-agnostic** authentication orchestration package designed for absolute runtime portability. One abstraction that works flawlessly across React Web `(18.x/19.x)`, Next.js `(14/15)`, and React Native/Expo `(SDK 50+)`.
|
|
8
|
+
|
|
9
|
+
Swap between Supabase, Firebase, Hybrid, or any custom provider without changing a single line of your UX component code.
|
|
8
10
|
|
|
9
11
|
---
|
|
10
12
|
|
|
11
|
-
## ๐ Latest Changes (v1.1.
|
|
13
|
+
## ๐ Latest Changes (v1.1.3)
|
|
12
14
|
|
|
13
|
-
###
|
|
15
|
+
### Docs
|
|
14
16
|
|
|
15
|
-
-
|
|
17
|
+
- ๐ Fully rewrote README to document the new `v1.1.0` Universal Compatibility (Web + Next.js + React Native/Expo) APIs and export paths.
|
|
16
18
|
|
|
17
19
|
For full version history, see [CHANGELOG.md](./CHANGELOG.md) and [GitHub releases](https://github.com/edcalderon/my-second-brain/releases)
|
|
18
20
|
|
|
19
21
|
---
|
|
20
22
|
|
|
21
|
-
##
|
|
22
|
-
|
|
23
|
-
The package follows a **Single Source of Truth** model with a **Federated OAuth Strategy**:
|
|
24
|
-
|
|
25
|
-
- **Principal Database (Source of Truth)**: Supabase anchors user identities, metadata, roles, and RLS policies in PostgreSQL (`auth.users`, `auth.identities`).
|
|
26
|
-
- **OAuth / Identity Providers**: External services (Firebase, Directus, native Google OAuth, Auth0, etc.) handle frontend login bridges or federated SSO flows.
|
|
27
|
-
- **The Orchestrator (`@edcalderon/auth`)**: A thin bridge layer that exposes generic interfaces (`User`, `AuthClient`). Applications consume a unified context without coupling to any specific vendor.
|
|
23
|
+
## ๐ Runtime Support Matrix
|
|
28
24
|
|
|
29
|
-
|
|
25
|
+
| Target Runtime | Engine / Framework | Notes | Supported Flow Semantics |
|
|
26
|
+
|----------------|--------------------|-------|-----------------|
|
|
27
|
+
| **Web** | React, Vite, SPA | Standard web APIs available (`window`) | `popup`, `redirect` |
|
|
28
|
+
| **Server** | Next.js Client | Compatible with App Router Contexts | `redirect`, `popup` |
|
|
29
|
+
| **Native** | Expo/React Native | Clean native bundles, strictly no web assumptions | `native` |
|
|
30
30
|
|
|
31
|
-
1. **Frontend Applications** `=>` consume **`@edcalderon/auth`** via `useAuth()`
|
|
32
|
-
2. **`@edcalderon/auth`** orchestrates the adapters:
|
|
33
|
-
- `=>` **Supabase Adapter** (Direct Session)
|
|
34
|
-
- `=>` **Hybrid Bridge** (Firebase OAuth + Supabase Session)
|
|
35
|
-
- `=>` **Custom Adapters** (e.g. Directus SSO, Auth0)
|
|
36
|
-
3. **Identity Providers** (Firebase/Directus) `=>` Sync Session to **Supabase**
|
|
37
|
-
4. **Supabase** `=>` Manages Roles & Scopes in the **PostgreSQL** Database
|
|
38
31
|
---
|
|
39
32
|
|
|
40
|
-
##
|
|
33
|
+
## ๐๏ธ Architecture
|
|
34
|
+
|
|
35
|
+
The package follows a **Single Source of Truth** model with a **Federated OAuth Strategy**:
|
|
36
|
+
- **Principal Database (Source of Truth)**: Supabase anchors user identities, metadata, roles, and RLS policies in PostgreSQL.
|
|
37
|
+
- **The Orchestrator (`@edcalderon/auth`)**: A thin bridge layer exposing a generic interface (`User`, `AuthClient`).
|
|
41
38
|
|
|
42
|
-
|
|
43
|
-
- โ๏ธ **React-First** โ `AuthProvider` + `useAuth` hook with full TypeScript types
|
|
44
|
-
- ๐ **Extensible Adapter System** โ implement `AuthClient` to add any provider
|
|
45
|
-
- ๐ **Hybrid Flows** โ Firebase popup โ Supabase session bridging out of the box
|
|
46
|
-
- ๐ก๏ธ **Unified User Model** โ `User` type normalizes identities across providers
|
|
47
|
-
- ๐ **Session Token Access** โ `getSessionToken()` for API calls regardless of provider
|
|
48
|
-
- ๐ฆ **Tree-Shakeable** โ import only the adapters you need
|
|
49
|
-
- ๐๏ธ **Zero Lock-In** โ swap providers by changing one line of dependency injection
|
|
39
|
+
The UI consumes a **unified context** disconnected entirely from provider implementations.
|
|
50
40
|
|
|
51
41
|
---
|
|
52
42
|
|
|
53
43
|
## Installation
|
|
54
44
|
|
|
55
|
-
### From NPM (public)
|
|
56
|
-
|
|
57
45
|
```bash
|
|
58
46
|
npm install @edcalderon/auth
|
|
59
47
|
# or
|
|
60
48
|
pnpm add @edcalderon/auth
|
|
61
|
-
# or
|
|
62
|
-
yarn add @edcalderon/auth
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### From Monorepo (internal workspace)
|
|
66
|
-
|
|
67
|
-
```bash
|
|
68
|
-
pnpm --filter <your-app> add @edcalderon/auth@workspace:*
|
|
69
49
|
```
|
|
70
50
|
|
|
71
51
|
### Peer Dependencies
|
|
72
52
|
|
|
73
|
-
Install
|
|
53
|
+
Install peers depending on what adapters you use. (The NPM module avoids forcing packages you won't ship to Native vs Web via strict subpath exports).
|
|
74
54
|
|
|
75
55
|
```bash
|
|
76
|
-
#
|
|
56
|
+
# Core requirements
|
|
57
|
+
pnpm add react react-dom
|
|
58
|
+
|
|
59
|
+
# Supabase (Adapter peers)
|
|
77
60
|
pnpm add @supabase/supabase-js
|
|
78
61
|
|
|
79
|
-
#
|
|
62
|
+
# Firebase (Hybrid/Pure peers)
|
|
80
63
|
pnpm add firebase
|
|
81
64
|
|
|
82
|
-
#
|
|
83
|
-
pnpm add
|
|
65
|
+
# Expo/Native Only
|
|
66
|
+
pnpm add react-native
|
|
84
67
|
```
|
|
85
68
|
|
|
86
|
-
> **Note:** `react` and `react-dom` (v18+ or v19+) are required peer dependencies.
|
|
87
|
-
|
|
88
69
|
---
|
|
89
70
|
|
|
90
|
-
##
|
|
91
|
-
|
|
92
|
-
### 1. Choose Your Provider
|
|
93
|
-
|
|
94
|
-
| Provider | Class | Peer Dependency | Use Case |
|
|
95
|
-
|----------|-------|-----------------|----------|
|
|
96
|
-
| Supabase | `SupabaseClient` | `@supabase/supabase-js` | Direct Supabase Auth |
|
|
97
|
-
| Firebase | `FirebaseClient` | `firebase` | Firebase-only applications |
|
|
98
|
-
| Hybrid | `HybridClient` | Both | Firebase popup โ Supabase session |
|
|
99
|
-
| Custom | Implement `AuthClient` | Your choice | Directus, Auth0, Keycloak, etc. |
|
|
71
|
+
## Subpath Exports (Crucial for RN/Next.js compatibility)
|
|
100
72
|
|
|
101
|
-
|
|
73
|
+
The package avoids bleeding `window` or `document` objects into Expo bundles or bleeding heavy native dependencies into web implementations via strict environment exports:
|
|
102
74
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
75
|
+
- `@edcalderon/auth` (Shared Core interfaces + Contexts)
|
|
76
|
+
- `@edcalderon/auth/supabase`
|
|
77
|
+
- `@edcalderon/auth/firebase-web`
|
|
78
|
+
- `@edcalderon/auth/firebase-native`
|
|
79
|
+
- `@edcalderon/auth/hybrid-web`
|
|
80
|
+
- `@edcalderon/auth/hybrid-native`
|
|
108
81
|
|
|
109
|
-
|
|
110
|
-
import { supabase } from "@/lib/supabase";
|
|
111
|
-
import { useMemo, type ReactNode } from "react";
|
|
82
|
+
---
|
|
112
83
|
|
|
113
|
-
|
|
114
|
-
const client = useMemo(() => new SupabaseClient(supabase), []);
|
|
115
|
-
return <UniversalAuthProvider client={client}>{children}</UniversalAuthProvider>;
|
|
116
|
-
}
|
|
84
|
+
## Quick Start (Web & Next.js)
|
|
117
85
|
|
|
118
|
-
|
|
119
|
-
```
|
|
86
|
+
### 1. Unified React Component UI (Usage)
|
|
120
87
|
|
|
121
|
-
|
|
88
|
+
Your component code is 100% blind to what provider or environment you are using. The `signIn` orchestration handles translating standard intent into provider actions seamlessly.
|
|
122
89
|
|
|
123
90
|
```tsx
|
|
124
91
|
"use client";
|
|
125
|
-
|
|
126
|
-
import { AuthProvider as UniversalAuthProvider, HybridClient, useAuth as useUniversalAuth } from "@edcalderon/auth";
|
|
127
|
-
import { supabase } from "@/lib/supabase";
|
|
128
|
-
import { auth, googleProvider, signInWithPopup, signOut, GoogleAuthProvider } from "@/lib/firebase";
|
|
129
|
-
import { useMemo, type ReactNode } from "react";
|
|
130
|
-
|
|
131
|
-
export function AuthProvider({ children }: { children: ReactNode }) {
|
|
132
|
-
const client = useMemo(() => new HybridClient({
|
|
133
|
-
supabase,
|
|
134
|
-
firebaseAuth: auth,
|
|
135
|
-
firebaseMethods: {
|
|
136
|
-
signInWithPopup,
|
|
137
|
-
signOut,
|
|
138
|
-
credentialFromResult: GoogleAuthProvider.credentialFromResult,
|
|
139
|
-
},
|
|
140
|
-
googleProvider,
|
|
141
|
-
}), []);
|
|
142
|
-
|
|
143
|
-
return <UniversalAuthProvider client={client}>{children}</UniversalAuthProvider>;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export const useAuth = useUniversalAuth;
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
### 3. Use in Components
|
|
150
|
-
|
|
151
|
-
Every component consumes **identical signatures** regardless of which provider is active:
|
|
152
|
-
|
|
153
|
-
```tsx
|
|
154
|
-
import { useAuth } from "@/components/auth/AuthProvider";
|
|
92
|
+
import { useAuth } from "@edcalderon/auth";
|
|
155
93
|
|
|
156
94
|
export default function Dashboard() {
|
|
157
|
-
const { user, loading, error,
|
|
95
|
+
const { user, loading, error, signIn, signOutUser } = useAuth();
|
|
158
96
|
|
|
159
97
|
if (loading) return <Spinner />;
|
|
160
98
|
if (error) return <p>Error: {error}</p>;
|
|
161
|
-
|
|
99
|
+
|
|
100
|
+
if (!user) {
|
|
101
|
+
return (
|
|
102
|
+
<button onClick={() => signIn({ provider: "google", flow: "popup" })}>
|
|
103
|
+
Sign In with Google
|
|
104
|
+
</button>
|
|
105
|
+
);
|
|
106
|
+
}
|
|
162
107
|
|
|
163
108
|
return (
|
|
164
109
|
<div>
|
|
165
|
-
<p>Welcome, {user.email}
|
|
110
|
+
<p>Welcome, {user.email}</p>
|
|
166
111
|
<button onClick={signOutUser}>Sign Out</button>
|
|
167
112
|
</div>
|
|
168
113
|
);
|
|
169
114
|
}
|
|
170
115
|
```
|
|
171
116
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
## ๐ Extensibility โ Custom Adapters
|
|
175
|
-
|
|
176
|
-
The core strength of `@edcalderon/auth` is that **any authentication provider** can be integrated by implementing the `AuthClient` interface. No changes to your React components are required.
|
|
117
|
+
### 2. Provider Top-Level App Injectors
|
|
177
118
|
|
|
178
|
-
|
|
119
|
+
Wire the environment appropriate class up at your app root.
|
|
179
120
|
|
|
180
|
-
|
|
181
|
-
export interface AuthClient {
|
|
182
|
-
getUser(): Promise<User | null>;
|
|
183
|
-
signInWithEmail(email: string, password: string): Promise<User>;
|
|
184
|
-
signInWithGoogle(redirectTo?: string): Promise<void>;
|
|
185
|
-
signOut(): Promise<void>;
|
|
186
|
-
onAuthStateChange(callback: (user: User | null) => void): () => void;
|
|
187
|
-
getSessionToken(): Promise<string | null>;
|
|
188
|
-
}
|
|
189
|
-
```
|
|
121
|
+
#### Supabase (Web/Native Universal)
|
|
190
122
|
|
|
191
|
-
|
|
123
|
+
```tsx
|
|
124
|
+
"use client";
|
|
125
|
+
import { AuthProvider } from "@edcalderon/auth";
|
|
126
|
+
import { SupabaseClient } from "@edcalderon/auth/supabase";
|
|
127
|
+
import { supabase } from "@/lib/supabase";
|
|
192
128
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
avatarUrl?: string;
|
|
198
|
-
provider?: string;
|
|
199
|
-
metadata?: Record<string, any>;
|
|
129
|
+
export function AppProviders({ children }) {
|
|
130
|
+
// Works perfectly in both web and Next.js out of the box
|
|
131
|
+
const client = new SupabaseClient({ supabase });
|
|
132
|
+
return <AuthProvider client={client}>{children}</AuthProvider>;
|
|
200
133
|
}
|
|
201
134
|
```
|
|
202
135
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
A custom Directus adapter that uses Directus SSO (e.g., Google OAuth through Directus) and optionally syncs sessions back to Supabase:
|
|
206
|
-
|
|
207
|
-
```typescript
|
|
208
|
-
import type { AuthClient, User } from "@edcalderon/auth";
|
|
209
|
-
|
|
210
|
-
interface DirectusClientOptions {
|
|
211
|
-
directusUrl: string;
|
|
212
|
-
supabase?: any; // Optional: sync to Supabase as source of truth
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export class DirectusClient implements AuthClient {
|
|
216
|
-
private directusUrl: string;
|
|
217
|
-
private supabase: any;
|
|
218
|
-
private currentUser: User | null = null;
|
|
219
|
-
private listeners: Set<(user: User | null) => void> = new Set();
|
|
220
|
-
|
|
221
|
-
constructor(options: DirectusClientOptions) {
|
|
222
|
-
this.directusUrl = options.directusUrl;
|
|
223
|
-
this.supabase = options.supabase;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
private mapUser(directusUser: any): User | null {
|
|
227
|
-
if (!directusUser) return null;
|
|
228
|
-
return {
|
|
229
|
-
id: directusUser.id,
|
|
230
|
-
email: directusUser.email,
|
|
231
|
-
avatarUrl: directusUser.avatar
|
|
232
|
-
? `${this.directusUrl}/assets/${directusUser.avatar}`
|
|
233
|
-
: undefined,
|
|
234
|
-
provider: "directus",
|
|
235
|
-
metadata: {
|
|
236
|
-
firstName: directusUser.first_name,
|
|
237
|
-
lastName: directusUser.last_name,
|
|
238
|
-
role: directusUser.role,
|
|
239
|
-
},
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
async getUser(): Promise<User | null> {
|
|
244
|
-
try {
|
|
245
|
-
const res = await fetch(`${this.directusUrl}/users/me`, {
|
|
246
|
-
credentials: "include",
|
|
247
|
-
});
|
|
248
|
-
if (!res.ok) return null;
|
|
249
|
-
const { data } = await res.json();
|
|
250
|
-
this.currentUser = this.mapUser(data);
|
|
251
|
-
return this.currentUser;
|
|
252
|
-
} catch {
|
|
253
|
-
return null;
|
|
254
|
-
}
|
|
255
|
-
}
|
|
136
|
+
#### Hybrid (Firebase UI โ Supabase Database Session Bridging for Web)
|
|
256
137
|
|
|
257
|
-
|
|
258
|
-
const res = await fetch(`${this.directusUrl}/auth/login`, {
|
|
259
|
-
method: "POST",
|
|
260
|
-
headers: { "Content-Type": "application/json" },
|
|
261
|
-
credentials: "include",
|
|
262
|
-
body: JSON.stringify({ email, password }),
|
|
263
|
-
});
|
|
264
|
-
if (!res.ok) throw new Error("Directus login failed");
|
|
265
|
-
const user = await this.getUser();
|
|
266
|
-
if (!user) throw new Error("No user after login");
|
|
267
|
-
this.notifyListeners(user);
|
|
268
|
-
|
|
269
|
-
// Optional: sync to Supabase
|
|
270
|
-
if (this.supabase) {
|
|
271
|
-
await this.syncToSupabase(user);
|
|
272
|
-
}
|
|
273
|
-
return user;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
async signInWithGoogle(redirectTo?: string): Promise<void> {
|
|
277
|
-
// Directus SSO โ redirect to Directus Google OAuth endpoint
|
|
278
|
-
const callback = redirectTo || window.location.origin + "/auth/callback";
|
|
279
|
-
window.location.href =
|
|
280
|
-
`${this.directusUrl}/auth/login/google?redirect=${encodeURIComponent(callback)}`;
|
|
281
|
-
}
|
|
138
|
+
Perfect if you want Firebase to handle the Google popup, but want to automatically consume the ID Token into Supabase to maintain your DB as the source of truth!
|
|
282
139
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
this.notifyListeners(null);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
onAuthStateChange(callback: (user: User | null) => void): () => void {
|
|
293
|
-
this.listeners.add(callback);
|
|
294
|
-
return () => { this.listeners.delete(callback); };
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
async getSessionToken(): Promise<string | null> {
|
|
298
|
-
try {
|
|
299
|
-
const res = await fetch(`${this.directusUrl}/auth/refresh`, {
|
|
300
|
-
method: "POST",
|
|
301
|
-
credentials: "include",
|
|
302
|
-
});
|
|
303
|
-
if (!res.ok) return null;
|
|
304
|
-
const { data } = await res.json();
|
|
305
|
-
return data?.access_token ?? null;
|
|
306
|
-
} catch {
|
|
307
|
-
return null;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
140
|
+
```tsx
|
|
141
|
+
"use client";
|
|
142
|
+
import { AuthProvider } from "@edcalderon/auth";
|
|
143
|
+
import { HybridWebClient } from "@edcalderon/auth/hybrid-web";
|
|
144
|
+
import { supabase } from "@/lib/supabase";
|
|
145
|
+
import { auth, signInWithPopup, signOut, GoogleAuthProvider } from "@/lib/firebase";
|
|
310
146
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
147
|
+
export function AppProviders({ children }) {
|
|
148
|
+
const client = new HybridWebClient({
|
|
149
|
+
supabase,
|
|
150
|
+
firebaseAuth: auth,
|
|
151
|
+
firebaseMethods: { signInWithPopup, signOut, credentialFromResult: GoogleAuthProvider.credentialFromResult },
|
|
152
|
+
googleProvider: new GoogleAuthProvider(),
|
|
153
|
+
});
|
|
314
154
|
|
|
315
|
-
|
|
316
|
-
// Sync user identity to Supabase as source of truth
|
|
317
|
-
// Implementation depends on your Supabase setup
|
|
318
|
-
}
|
|
155
|
+
return <AuthProvider client={client}>{children}</AuthProvider>;
|
|
319
156
|
}
|
|
320
157
|
```
|
|
321
158
|
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
```tsx
|
|
325
|
-
import { AuthProvider as UniversalAuthProvider } from "@edcalderon/auth";
|
|
326
|
-
import { DirectusClient } from "./adapters/DirectusClient";
|
|
159
|
+
---
|
|
327
160
|
|
|
328
|
-
|
|
329
|
-
directusUrl: "https://directus.example.com",
|
|
330
|
-
supabase: supabaseInstance, // optional sync
|
|
331
|
-
});
|
|
161
|
+
## Quick Start (Expo & React Native)
|
|
332
162
|
|
|
333
|
-
|
|
334
|
-
<App />
|
|
335
|
-
</UniversalAuthProvider>
|
|
336
|
-
```
|
|
163
|
+
React Native apps cannot safely utilize Web's window or popup assumptions. Because of the unified typings, your components never have to change, you just wire up the specific native adapters.
|
|
337
164
|
|
|
338
|
-
###
|
|
165
|
+
### Hybrid Strategy Native (`expo-auth-session`)
|
|
339
166
|
|
|
340
|
-
|
|
341
|
-
import type { AuthClient, User } from "@edcalderon/auth";
|
|
167
|
+
Instead of trying to pop up Firebase Web via polyfills, explicitly hand over native execution capabilities down to the adapter utilizing React Native Expo equivalents.
|
|
342
168
|
|
|
343
|
-
|
|
344
|
-
|
|
169
|
+
```tsx
|
|
170
|
+
import { AuthProvider } from "@edcalderon/auth";
|
|
171
|
+
import { HybridNativeClient } from "@edcalderon/auth/hybrid-native";
|
|
172
|
+
import { supabase } from "@/lib/supabase";
|
|
173
|
+
import { auth, signInWithCredential } from "firebase/auth";
|
|
174
|
+
import * as Google from 'expo-auth-session/providers/google'; // Or react-native-google-signin
|
|
175
|
+
|
|
176
|
+
export function ExpoProviders({ children }) {
|
|
177
|
+
// 1. You provide strictly native capability functions out of your Expo ecosystem
|
|
178
|
+
const nativeGoogleHandler = async (options) => {
|
|
179
|
+
// e.g promptAsync()
|
|
180
|
+
// Exchange credential response for Firebase Native Credentials
|
|
181
|
+
// Return { credential, idToken }
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const client = new HybridNativeClient({
|
|
185
|
+
supabase,
|
|
186
|
+
firebaseAuth: auth,
|
|
187
|
+
firebaseMethods: { signInWithCredential, signOut },
|
|
188
|
+
oauthHandlers: {
|
|
189
|
+
"google": nativeGoogleHandler
|
|
190
|
+
}
|
|
191
|
+
});
|
|
345
192
|
|
|
346
|
-
|
|
347
|
-
async signInWithEmail(email: string, password: string): Promise<User> { /* ... */ }
|
|
348
|
-
async signInWithGoogle(redirectTo?: string): Promise<void> { /* ... */ }
|
|
349
|
-
async signOut(): Promise<void> { /* ... */ }
|
|
350
|
-
onAuthStateChange(callback: (user: User | null) => void): () => void { /* ... */ }
|
|
351
|
-
async getSessionToken(): Promise<string | null> { /* ... */ }
|
|
193
|
+
return <AuthProvider client={client}>{children}</AuthProvider>;
|
|
352
194
|
}
|
|
353
195
|
```
|
|
354
196
|
|
|
355
|
-
|
|
197
|
+
Now, clicking `signIn({ provider: "google", flow: "native" })` from anywhere inside your Expo app safely triggers `nativeGoogleHandler` and orchestrates Firebase translation down to Supabase seamlessly behind the scenes!
|
|
356
198
|
|
|
357
199
|
---
|
|
358
200
|
|
|
359
|
-
##
|
|
360
|
-
|
|
361
|
-
### `SupabaseClient`
|
|
362
|
-
|
|
363
|
-
Direct Supabase Auth adapter. Uses `@supabase/supabase-js` for session management, OAuth, and email/password.
|
|
364
|
-
|
|
365
|
-
```typescript
|
|
366
|
-
import { SupabaseClient } from "@edcalderon/auth";
|
|
367
|
-
import { createClient } from "@supabase/supabase-js";
|
|
368
|
-
|
|
369
|
-
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
|
370
|
-
const client = new SupabaseClient(supabase);
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
**Features:**
|
|
374
|
-
- Email/password sign-in (`signInWithPassword`)
|
|
375
|
-
- Google OAuth (`signInWithOAuth`)
|
|
376
|
-
- Session token via `getSession().access_token`
|
|
377
|
-
- Real-time auth state changes via `onAuthStateChange`
|
|
201
|
+
## ๐ API Reference - Extensibility
|
|
378
202
|
|
|
379
|
-
### `
|
|
380
|
-
|
|
381
|
-
Firebase-only adapter. Uses Firebase Auth methods via dependency injection (tree-shaking friendly).
|
|
382
|
-
|
|
383
|
-
```typescript
|
|
384
|
-
import { FirebaseClient } from "@edcalderon/auth";
|
|
385
|
-
import { getAuth, GoogleAuthProvider, signInWithEmailAndPassword, signInWithPopup, signOut, onAuthStateChanged } from "firebase/auth";
|
|
386
|
-
|
|
387
|
-
const auth = getAuth(app);
|
|
388
|
-
const client = new FirebaseClient(auth, {
|
|
389
|
-
signInWithEmailAndPassword,
|
|
390
|
-
signInWithPopup,
|
|
391
|
-
signOut,
|
|
392
|
-
onAuthStateChanged,
|
|
393
|
-
}, new GoogleAuthProvider());
|
|
394
|
-
```
|
|
395
|
-
|
|
396
|
-
**Features:**
|
|
397
|
-
- Email/password sign-in
|
|
398
|
-
- Google popup sign-in
|
|
399
|
-
- Firebase ID token via `getIdToken()`
|
|
400
|
-
- Real-time auth state changes
|
|
401
|
-
|
|
402
|
-
### `HybridClient`
|
|
203
|
+
### The `AuthClient` Interface
|
|
403
204
|
|
|
404
|
-
|
|
205
|
+
The core strength of `@edcalderon/auth` is that **any authentication service** can be mapped directly onto the `AuthClient` type, exposing typed portability out-of-the-box.
|
|
405
206
|
|
|
406
207
|
```typescript
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const client = new HybridClient({
|
|
410
|
-
supabase,
|
|
411
|
-
firebaseAuth: auth,
|
|
412
|
-
firebaseMethods: { signInWithPopup, signOut, credentialFromResult: GoogleAuthProvider.credentialFromResult },
|
|
413
|
-
googleProvider: new GoogleAuthProvider(),
|
|
414
|
-
});
|
|
415
|
-
```
|
|
208
|
+
type AuthRuntime = "web" | "native" | "server";
|
|
209
|
+
type OAuthFlow = "popup" | "redirect" | "native";
|
|
416
210
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
---
|
|
424
|
-
|
|
425
|
-
## API Reference
|
|
426
|
-
|
|
427
|
-
### `<AuthProvider>`
|
|
428
|
-
|
|
429
|
-
React context provider that wraps your app with authentication state.
|
|
211
|
+
export interface SignInOptions {
|
|
212
|
+
provider?: "google" | "apple" | "github" | string;
|
|
213
|
+
flow?: OAuthFlow;
|
|
214
|
+
redirectUri?: string;
|
|
215
|
+
}
|
|
430
216
|
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
{
|
|
434
|
-
|
|
217
|
+
export interface AuthClient {
|
|
218
|
+
runtime: AuthRuntime;
|
|
219
|
+
capabilities(): { runtime: AuthRuntime; supportedFlows: OAuthFlow[] };
|
|
220
|
+
|
|
221
|
+
getUser(): Promise<User | null>;
|
|
222
|
+
signInWithEmail(email: string, password: string): Promise<User>;
|
|
223
|
+
signIn(options: SignInOptions): Promise<void>;
|
|
224
|
+
signOut(): Promise<void>;
|
|
225
|
+
|
|
226
|
+
onAuthStateChange(callback: (user: User | null) => void): () => void;
|
|
227
|
+
getSessionToken(): Promise<string | null>;
|
|
228
|
+
}
|
|
435
229
|
```
|
|
436
230
|
|
|
437
|
-
|
|
438
|
-
|------|------|-------------|
|
|
439
|
-
| `client` | `AuthClient` | The authentication adapter instance |
|
|
440
|
-
| `children` | `ReactNode` | Child components |
|
|
441
|
-
|
|
442
|
-
### `useAuth()`
|
|
443
|
-
|
|
444
|
-
React hook that returns the current authentication state and actions.
|
|
231
|
+
### The `User` Type
|
|
445
232
|
|
|
446
233
|
```typescript
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
}
|
|
234
|
+
export interface User {
|
|
235
|
+
id: string;
|
|
236
|
+
email?: string;
|
|
237
|
+
avatarUrl?: string;
|
|
238
|
+
provider?: string;
|
|
239
|
+
providerUserId?: string;
|
|
240
|
+
roles?: string[];
|
|
241
|
+
metadata?: Record<string, any>;
|
|
242
|
+
}
|
|
456
243
|
```
|
|
457
244
|
|
|
458
|
-
> **Note:** `useAuth()` must be called within an `<AuthProvider>`. It will throw if used outside the provider tree.
|
|
459
|
-
|
|
460
|
-
---
|
|
461
|
-
|
|
462
|
-
## Publishing & Releases
|
|
463
|
-
|
|
464
|
-
### Automated NPM Publishing
|
|
465
|
-
|
|
466
|
-
This package uses GitHub Actions for automated publishing to NPM when version tags are created.
|
|
467
|
-
|
|
468
|
-
#### Release Process
|
|
469
|
-
|
|
470
|
-
1. **Update Version**: Bump the version in `package.json`
|
|
471
|
-
```bash
|
|
472
|
-
cd packages/auth
|
|
473
|
-
npm version patch # or minor, major
|
|
474
|
-
```
|
|
475
|
-
|
|
476
|
-
2. **Create Git Tag**: Create and push an `auth-v*` tag
|
|
477
|
-
```bash
|
|
478
|
-
git add packages/auth/package.json
|
|
479
|
-
git commit -m "chore(auth): bump version to X.Y.Z"
|
|
480
|
-
git tag auth-vX.Y.Z
|
|
481
|
-
git push && git push --tags
|
|
482
|
-
```
|
|
483
|
-
|
|
484
|
-
3. **Automated Publishing**: GitHub Actions will automatically build and publish to NPM
|
|
485
|
-
|
|
486
|
-
#### NPM Token Setup
|
|
487
|
-
|
|
488
|
-
To enable automated publishing:
|
|
489
|
-
|
|
490
|
-
1. Go to [NPM](https://www.npmjs.com/) โ Access Tokens โ Generate New Token
|
|
491
|
-
2. Create a token with **Automation** scope
|
|
492
|
-
3. Add to GitHub repository secrets as `NPM_TOKEN`
|
|
493
|
-
|
|
494
|
-
---
|
|
495
|
-
|
|
496
|
-
## Documentation
|
|
497
|
-
|
|
498
|
-
- **[CHANGELOG](CHANGELOG.md)** โ Version history and changes
|
|
499
|
-
- **[GitHub Releases](https://github.com/edcalderon/my-second-brain/releases)** โ Tagged releases
|
|
500
|
-
|
|
501
245
|
---
|
|
502
246
|
|
|
503
247
|
## License
|