clefbase 1.0.0
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/README.md +265 -0
- package/dist/app.d.ts +69 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +112 -0
- package/dist/app.js.map +1 -0
- package/dist/auth/index.d.ts +115 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +271 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/db/index.d.ts +165 -0
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +230 -0
- package/dist/db/index.js.map +1 -0
- package/dist/http.d.ts +25 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +132 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/storage/index.d.ts +105 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +201 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/types.d.ts +87 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
# clefbase
|
|
2
|
+
|
|
3
|
+
Firebase-style SDK for your Clefbase server. Drop-in for Node.js and browser apps.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install clefbase
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { initClefbase, getDatabase, getAuth, getStorage } from "clefbase";
|
|
17
|
+
|
|
18
|
+
const app = initClefbase({
|
|
19
|
+
serverUrl: "https://your-clefbase-server.com",
|
|
20
|
+
projectId: "my_project_1abc", // from your dashboard
|
|
21
|
+
apiKey: "cfx_abc123...", // generated API key
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const db = getDatabase(app);
|
|
25
|
+
const auth = getAuth(app);
|
|
26
|
+
const storage = getStorage(app);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Database
|
|
32
|
+
|
|
33
|
+
### Add a document
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const post = await db.collection("posts").add({
|
|
37
|
+
title: "Hello World",
|
|
38
|
+
published: true,
|
|
39
|
+
views: 0,
|
|
40
|
+
});
|
|
41
|
+
// post._id, post._createdAt, post._updatedAt are auto-set
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Get a document
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const user = await db.collection("users").doc("uid-123").get();
|
|
48
|
+
// returns null if not found
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Update a document
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// Merge (default) — only changes the fields you pass
|
|
55
|
+
await db.collection("users").doc("uid-123").update({ name: "Alice" });
|
|
56
|
+
|
|
57
|
+
// Full overwrite
|
|
58
|
+
await db.collection("users").doc("uid-123").set({ name: "Alice", age: 30 });
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Delete a document
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
await db.collection("users").doc("uid-123").delete();
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Query a collection
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
const results = await db.collection("posts")
|
|
71
|
+
.where({ published: true, views: { $gt: 100 } })
|
|
72
|
+
.orderBy("_createdAt", "desc")
|
|
73
|
+
.limit(10)
|
|
74
|
+
.get();
|
|
75
|
+
|
|
76
|
+
// results.data — array of documents
|
|
77
|
+
// results.total — total matching count
|
|
78
|
+
// results.limit, results.offset
|
|
79
|
+
|
|
80
|
+
// Or just get the array:
|
|
81
|
+
const posts = await db.collection("posts")
|
|
82
|
+
.where({ published: true })
|
|
83
|
+
.getDocs();
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Available filter operators
|
|
87
|
+
|
|
88
|
+
| Operator | Meaning |
|
|
89
|
+
|----------------|----------------------|
|
|
90
|
+
| `{ $gt: n }` | greater than |
|
|
91
|
+
| `{ $gte: n }` | greater than or equal|
|
|
92
|
+
| `{ $lt: n }` | less than |
|
|
93
|
+
| `{ $lte: n }` | less than or equal |
|
|
94
|
+
| `{ $ne: val }` | not equal |
|
|
95
|
+
| `{ $contains: "str" }` | string contains (case-insensitive) |
|
|
96
|
+
|
|
97
|
+
### Subcollections
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
// posts → [postId] → comments
|
|
101
|
+
const comments = db.collection("posts").doc("post-abc").collection("comments");
|
|
102
|
+
|
|
103
|
+
await comments.add({ text: "Great post!", author: "Bob" });
|
|
104
|
+
const all = await comments.getDocs();
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Convenience top-level helpers
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
await db.getDoc("users", "uid-123");
|
|
111
|
+
await db.addDoc("logs", { action: "login" });
|
|
112
|
+
await db.updateDoc("users", "uid-123", { lastSeen: new Date().toISOString() });
|
|
113
|
+
await db.deleteDoc("users", "uid-123");
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
## Auth
|
|
119
|
+
|
|
120
|
+
### Sign up
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
const { user, token } = await auth.signUp("alice@example.com", "password123", {
|
|
124
|
+
displayName: "Alice",
|
|
125
|
+
metadata: { role: "member" },
|
|
126
|
+
});
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Sign in / Sign out
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
const { user } = await auth.signIn("alice@example.com", "password123");
|
|
133
|
+
|
|
134
|
+
await auth.signOut();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Current user
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
const me = auth.currentUser; // AuthUser | null
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Listen for auth state changes
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
const unsubscribe = auth.onAuthStateChanged((user) => {
|
|
147
|
+
if (user) {
|
|
148
|
+
console.log("Signed in as", user.email);
|
|
149
|
+
} else {
|
|
150
|
+
console.log("Signed out");
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Stop listening
|
|
155
|
+
unsubscribe();
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
### Update profile
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
await auth.updateProfile({ displayName: "Bob", metadata: { theme: "dark" } });
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Password management
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
// Change password (must be signed in)
|
|
168
|
+
await auth.changePassword("oldPass", "newPass");
|
|
169
|
+
|
|
170
|
+
// Request reset email
|
|
171
|
+
await auth.sendPasswordResetEmail("alice@example.com");
|
|
172
|
+
|
|
173
|
+
// Confirm reset with token from email
|
|
174
|
+
await auth.confirmPasswordReset(resetToken, "newPassword");
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Email verification
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
await auth.sendEmailVerification();
|
|
181
|
+
await auth.verifyEmail("ABC123"); // code from email
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## Storage
|
|
187
|
+
|
|
188
|
+
### Upload a file
|
|
189
|
+
|
|
190
|
+
```ts
|
|
191
|
+
// Node.js
|
|
192
|
+
import fs from "fs";
|
|
193
|
+
const buffer = fs.readFileSync("./photo.jpg");
|
|
194
|
+
const meta = await storage.ref("avatars/user-123.jpg").upload(buffer, {
|
|
195
|
+
contentType: "image/jpeg",
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Browser
|
|
199
|
+
const file = fileInput.files[0];
|
|
200
|
+
const meta = await storage.ref(`uploads/${file.name}`).upload(file);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Get download URL
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
const url = await storage.ref("avatars/user-123.jpg").getDownloadURL();
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Get metadata
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
const meta = await storage.ref("avatars/user-123.jpg").getMetadata();
|
|
213
|
+
// meta.size, meta.mimeType, meta.url, meta.createdAt
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Delete a file
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
await storage.ref("avatars/user-123.jpg").delete();
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### List files
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
const files = await storage.ref("avatars/").list({ limit: 20 });
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Use a specific bucket
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const ref = storage.bucket("user-uploads").ref("doc.pdf");
|
|
232
|
+
await ref.upload(buffer, { contentType: "application/pdf" });
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## TypeScript
|
|
238
|
+
|
|
239
|
+
The SDK is fully typed. Pass your own document shape as a generic:
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
interface Post {
|
|
243
|
+
_id: string;
|
|
244
|
+
_createdAt: string;
|
|
245
|
+
_updatedAt: string;
|
|
246
|
+
title: string;
|
|
247
|
+
published: boolean;
|
|
248
|
+
views: number;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const posts = db.collection<Post>("posts");
|
|
252
|
+
const post = await posts.doc("abc").get(); // typed as Post | null
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## Multiple apps
|
|
258
|
+
|
|
259
|
+
```ts
|
|
260
|
+
const defaultApp = initClefbase({ ... });
|
|
261
|
+
const adminApp = initClefbase({ ..., apiKey: "admin-key" }, "admin");
|
|
262
|
+
|
|
263
|
+
const db = getDatabase(defaultApp);
|
|
264
|
+
const adminDb = getDatabase(adminApp);
|
|
265
|
+
```
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { HttpClient } from "./http";
|
|
2
|
+
import { Database } from "./db/index";
|
|
3
|
+
import { Auth } from "./auth/index";
|
|
4
|
+
import { ClefbaseStorage } from "./storage/index";
|
|
5
|
+
import type { ClefbaseConfig } from "./types";
|
|
6
|
+
/**
|
|
7
|
+
* A Clefbase application instance.
|
|
8
|
+
* Created by `initClefbase()` and passed to `getDatabase()`, `getAuth()`, `getStorage()`.
|
|
9
|
+
*/
|
|
10
|
+
export declare class ClefbaseApp {
|
|
11
|
+
readonly config: ClefbaseConfig;
|
|
12
|
+
/** @internal */
|
|
13
|
+
readonly _http: HttpClient;
|
|
14
|
+
private _db;
|
|
15
|
+
private _auth;
|
|
16
|
+
private _storage;
|
|
17
|
+
constructor(config: ClefbaseConfig);
|
|
18
|
+
/** @internal */
|
|
19
|
+
_getDb(): Database;
|
|
20
|
+
/** @internal */
|
|
21
|
+
_getAuth(): Auth;
|
|
22
|
+
/** @internal */
|
|
23
|
+
_getStorage(): ClefbaseStorage;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Initialise a Clefbase app. Call once at startup.
|
|
27
|
+
* Returns the same instance if called again with the same name.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* const app = initClefbase({
|
|
31
|
+
* serverUrl: "https://your-server.com",
|
|
32
|
+
* projectId: "my_project_1abc",
|
|
33
|
+
* apiKey: "cfx_abc123...",
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* // Named (multiple projects)
|
|
37
|
+
* const admin = initClefbase({ ... }, "admin");
|
|
38
|
+
*/
|
|
39
|
+
export declare function initClefbase(config: ClefbaseConfig, name?: string): ClefbaseApp;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieve an already-initialised app by name.
|
|
42
|
+
* Throws if `initClefbase()` has not been called for that name.
|
|
43
|
+
*/
|
|
44
|
+
export declare function getApp(name?: string): ClefbaseApp;
|
|
45
|
+
/**
|
|
46
|
+
* Get the Database service for an app.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* const db = getDatabase(app);
|
|
50
|
+
* const user = await db.collection("users").doc("123").get();
|
|
51
|
+
*/
|
|
52
|
+
export declare function getDatabase(app?: ClefbaseApp): Database;
|
|
53
|
+
/**
|
|
54
|
+
* Get the Auth service for an app.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* const auth = getAuth(app);
|
|
58
|
+
* const { user } = await auth.signIn("user@example.com", "pass");
|
|
59
|
+
*/
|
|
60
|
+
export declare function getAuth(app?: ClefbaseApp): Auth;
|
|
61
|
+
/**
|
|
62
|
+
* Get the Storage service for an app.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* const storage = getStorage(app);
|
|
66
|
+
* await storage.ref("files/doc.pdf").upload(buffer);
|
|
67
|
+
*/
|
|
68
|
+
export declare function getStorage(app?: ClefbaseApp): ClefbaseStorage;
|
|
69
|
+
//# sourceMappingURL=app.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACpC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAI9C;;;GAGG;AACH,qBAAa,WAAW;IACtB,QAAQ,CAAC,MAAM,EAAE,cAAc,CAAC;IAEhC,gBAAgB;IAChB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAE3B,OAAO,CAAC,GAAG,CAAuB;IAClC,OAAO,CAAC,KAAK,CAAmB;IAChC,OAAO,CAAC,QAAQ,CAA8B;gBAElC,MAAM,EAAE,cAAc;IAQlC,gBAAgB;IAChB,MAAM,IAAI,QAAQ;IAKlB,gBAAgB;IAChB,QAAQ,IAAI,IAAI;IAKhB,gBAAgB;IAChB,WAAW,IAAI,eAAe;CAK/B;AAOD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,cAAc,EACtB,IAAI,GAAE,MAAgB,GACrB,WAAW,CAKb;AAED;;;GAGG;AACH,wBAAgB,MAAM,CAAC,IAAI,GAAE,MAAgB,GAAG,WAAW,CAQ1D;AAID;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,GAAG,CAAC,EAAE,WAAW,GAAG,QAAQ,CAEvD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,GAAG,CAAC,EAAE,WAAW,GAAG,IAAI,CAE/C;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,GAAG,CAAC,EAAE,WAAW,GAAG,eAAe,CAE7D"}
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ClefbaseApp = void 0;
|
|
4
|
+
exports.initClefbase = initClefbase;
|
|
5
|
+
exports.getApp = getApp;
|
|
6
|
+
exports.getDatabase = getDatabase;
|
|
7
|
+
exports.getAuth = getAuth;
|
|
8
|
+
exports.getStorage = getStorage;
|
|
9
|
+
const http_1 = require("./http");
|
|
10
|
+
const index_1 = require("./db/index");
|
|
11
|
+
const index_2 = require("./auth/index");
|
|
12
|
+
const index_3 = require("./storage/index");
|
|
13
|
+
// ─── App ──────────────────────────────────────────────────────────────────────
|
|
14
|
+
/**
|
|
15
|
+
* A Clefbase application instance.
|
|
16
|
+
* Created by `initClefbase()` and passed to `getDatabase()`, `getAuth()`, `getStorage()`.
|
|
17
|
+
*/
|
|
18
|
+
class ClefbaseApp {
|
|
19
|
+
constructor(config) {
|
|
20
|
+
this.config = config;
|
|
21
|
+
const base = config.serverUrl.replace(/\/+$/, "");
|
|
22
|
+
this._http = new http_1.HttpClient(`${base}/ext/${config.projectId}`, {
|
|
23
|
+
"x-cfx-key": config.apiKey,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/** @internal */
|
|
27
|
+
_getDb() {
|
|
28
|
+
if (!this._db)
|
|
29
|
+
this._db = new index_1.Database(this._http);
|
|
30
|
+
return this._db;
|
|
31
|
+
}
|
|
32
|
+
/** @internal */
|
|
33
|
+
_getAuth() {
|
|
34
|
+
if (!this._auth)
|
|
35
|
+
this._auth = new index_2.Auth(this._http);
|
|
36
|
+
return this._auth;
|
|
37
|
+
}
|
|
38
|
+
/** @internal */
|
|
39
|
+
_getStorage() {
|
|
40
|
+
if (!this._storage)
|
|
41
|
+
this._storage = new index_3.ClefbaseStorage(this._http, this._getAuth());
|
|
42
|
+
return this._storage;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.ClefbaseApp = ClefbaseApp;
|
|
46
|
+
// ─── App registry ─────────────────────────────────────────────────────────────
|
|
47
|
+
const DEFAULT = "[DEFAULT]";
|
|
48
|
+
const registry = new Map();
|
|
49
|
+
/**
|
|
50
|
+
* Initialise a Clefbase app. Call once at startup.
|
|
51
|
+
* Returns the same instance if called again with the same name.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* const app = initClefbase({
|
|
55
|
+
* serverUrl: "https://your-server.com",
|
|
56
|
+
* projectId: "my_project_1abc",
|
|
57
|
+
* apiKey: "cfx_abc123...",
|
|
58
|
+
* });
|
|
59
|
+
*
|
|
60
|
+
* // Named (multiple projects)
|
|
61
|
+
* const admin = initClefbase({ ... }, "admin");
|
|
62
|
+
*/
|
|
63
|
+
function initClefbase(config, name = DEFAULT) {
|
|
64
|
+
if (registry.has(name))
|
|
65
|
+
return registry.get(name);
|
|
66
|
+
const app = new ClefbaseApp(config);
|
|
67
|
+
registry.set(name, app);
|
|
68
|
+
return app;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Retrieve an already-initialised app by name.
|
|
72
|
+
* Throws if `initClefbase()` has not been called for that name.
|
|
73
|
+
*/
|
|
74
|
+
function getApp(name = DEFAULT) {
|
|
75
|
+
const app = registry.get(name);
|
|
76
|
+
if (!app) {
|
|
77
|
+
throw new Error(`Clefbase app "${name}" has not been initialised. Call initClefbase() first.`);
|
|
78
|
+
}
|
|
79
|
+
return app;
|
|
80
|
+
}
|
|
81
|
+
// ─── Service accessors ────────────────────────────────────────────────────────
|
|
82
|
+
/**
|
|
83
|
+
* Get the Database service for an app.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* const db = getDatabase(app);
|
|
87
|
+
* const user = await db.collection("users").doc("123").get();
|
|
88
|
+
*/
|
|
89
|
+
function getDatabase(app) {
|
|
90
|
+
return (app ?? getApp())._getDb();
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Get the Auth service for an app.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const auth = getAuth(app);
|
|
97
|
+
* const { user } = await auth.signIn("user@example.com", "pass");
|
|
98
|
+
*/
|
|
99
|
+
function getAuth(app) {
|
|
100
|
+
return (app ?? getApp())._getAuth();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Get the Storage service for an app.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* const storage = getStorage(app);
|
|
107
|
+
* await storage.ref("files/doc.pdf").upload(buffer);
|
|
108
|
+
*/
|
|
109
|
+
function getStorage(app) {
|
|
110
|
+
return (app ?? getApp())._getStorage();
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=app.js.map
|
package/dist/app.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../src/app.ts"],"names":[],"mappings":";;;AAqEA,oCAQC;AAMD,wBAQC;AAWD,kCAEC;AASD,0BAEC;AASD,gCAEC;AA9HD,iCAAoC;AACpC,sCAAsC;AACtC,wCAAoC;AACpC,2CAAkD;AAGlD,iFAAiF;AAEjF;;;GAGG;AACH,MAAa,WAAW;IAUtB,YAAY,MAAsB;QAChC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAClD,IAAI,CAAC,KAAK,GAAG,IAAI,iBAAU,CAAC,GAAG,IAAI,QAAQ,MAAM,CAAC,SAAS,EAAE,EAAE;YAC7D,WAAW,EAAE,MAAM,CAAC,MAAM;SAC3B,CAAC,CAAC;IACL,CAAC;IAED,gBAAgB;IAChB,MAAM;QACJ,IAAI,CAAC,IAAI,CAAC,GAAG;YAAE,IAAI,CAAC,GAAG,GAAG,IAAI,gBAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnD,OAAO,IAAI,CAAC,GAAG,CAAC;IAClB,CAAC;IAED,gBAAgB;IAChB,QAAQ;QACN,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,IAAI,CAAC,KAAK,GAAG,IAAI,YAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACnD,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,gBAAgB;IAChB,WAAW;QACT,IAAI,CAAC,IAAI,CAAC,QAAQ;YAChB,IAAI,CAAC,QAAQ,GAAG,IAAI,uBAAe,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC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,0 +1,115 @@
|
|
|
1
|
+
import { HttpClient } from "../http";
|
|
2
|
+
import type { AuthUser, AuthResult } from "../types";
|
|
3
|
+
type AuthStateCallback = (user: AuthUser | null) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Authentication service. Obtain via `getAuth(app)`.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* import { initClefbase, getAuth } from "clefbase";
|
|
9
|
+
* const app = initClefbase({ serverUrl, projectId, apiKey });
|
|
10
|
+
* const auth = getAuth(app);
|
|
11
|
+
*
|
|
12
|
+
* const { user } = await auth.signUp("alice@example.com", "secret123");
|
|
13
|
+
* const { user } = await auth.signIn("alice@example.com", "secret123");
|
|
14
|
+
* console.log(auth.currentUser);
|
|
15
|
+
* await auth.signOut();
|
|
16
|
+
*
|
|
17
|
+
* const unsub = auth.onAuthStateChanged((u) => console.log(u));
|
|
18
|
+
* unsub(); // stop listening
|
|
19
|
+
*/
|
|
20
|
+
export declare class Auth {
|
|
21
|
+
private readonly http;
|
|
22
|
+
private readonly store;
|
|
23
|
+
private listeners;
|
|
24
|
+
constructor(http: HttpClient);
|
|
25
|
+
private notify;
|
|
26
|
+
private handleResult;
|
|
27
|
+
/** @internal Used by Storage to attach the auth token to upload requests. */
|
|
28
|
+
getAuthHeaders(): Record<string, string>;
|
|
29
|
+
/** The currently signed-in user, or null if no session exists. */
|
|
30
|
+
get currentUser(): AuthUser | null;
|
|
31
|
+
/** The raw bearer token for the active session, or null. */
|
|
32
|
+
get currentToken(): string | null;
|
|
33
|
+
/**
|
|
34
|
+
* Create a new account with email + password.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const { user } = await auth.signUp("alice@example.com", "pass123", {
|
|
38
|
+
* displayName: "Alice",
|
|
39
|
+
* metadata: { role: "admin" },
|
|
40
|
+
* });
|
|
41
|
+
*/
|
|
42
|
+
signUp(email: string, password: string, profile?: {
|
|
43
|
+
displayName?: string;
|
|
44
|
+
photoUrl?: string;
|
|
45
|
+
metadata?: Record<string, unknown>;
|
|
46
|
+
}): Promise<AuthResult>;
|
|
47
|
+
/**
|
|
48
|
+
* Sign in with email + password.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const { user, token } = await auth.signIn("alice@example.com", "pass123");
|
|
52
|
+
*/
|
|
53
|
+
signIn(email: string, password: string): Promise<AuthResult>;
|
|
54
|
+
/**
|
|
55
|
+
* Sign out the current user and clear the local session.
|
|
56
|
+
*/
|
|
57
|
+
signOut(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Re-fetch the current user's profile from the server and update the cache.
|
|
60
|
+
* Useful after an admin changes the user's data server-side.
|
|
61
|
+
*/
|
|
62
|
+
refreshCurrentUser(): Promise<AuthUser | null>;
|
|
63
|
+
/**
|
|
64
|
+
* Update the signed-in user's profile.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* await auth.updateProfile({ displayName: "Bob", metadata: { theme: "dark" } });
|
|
68
|
+
*/
|
|
69
|
+
updateProfile(updates: {
|
|
70
|
+
displayName?: string;
|
|
71
|
+
photoUrl?: string;
|
|
72
|
+
metadata?: Record<string, unknown>;
|
|
73
|
+
}): Promise<AuthUser>;
|
|
74
|
+
/**
|
|
75
|
+
* Change the signed-in user's password.
|
|
76
|
+
* All other active sessions are invalidated on the server.
|
|
77
|
+
*/
|
|
78
|
+
changePassword(currentPassword: string, newPassword: string): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Request a password-reset email. Safe to call even if the address
|
|
81
|
+
* is not registered (server returns the same response either way).
|
|
82
|
+
*/
|
|
83
|
+
sendPasswordResetEmail(email: string): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Complete a password reset with the token from the reset email.
|
|
86
|
+
*/
|
|
87
|
+
confirmPasswordReset(resetToken: string, newPassword: string): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Send an email verification code to the currently signed-in user.
|
|
90
|
+
*/
|
|
91
|
+
sendEmailVerification(): Promise<void>;
|
|
92
|
+
/**
|
|
93
|
+
* Verify the current user's email with the code they received.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const user = await auth.verifyEmail("ABC123");
|
|
97
|
+
*/
|
|
98
|
+
verifyEmail(code: string): Promise<AuthUser>;
|
|
99
|
+
/**
|
|
100
|
+
* Subscribe to auth state changes.
|
|
101
|
+
* The callback fires immediately with the current user, then on every change.
|
|
102
|
+
* Returns an unsubscribe function.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* const unsubscribe = auth.onAuthStateChanged((user) => {
|
|
106
|
+
* if (user) console.log("signed in:", user.email);
|
|
107
|
+
* else console.log("signed out");
|
|
108
|
+
* });
|
|
109
|
+
* // Later:
|
|
110
|
+
* unsubscribe();
|
|
111
|
+
*/
|
|
112
|
+
onAuthStateChanged(callback: AuthStateCallback): () => void;
|
|
113
|
+
}
|
|
114
|
+
export {};
|
|
115
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AA4DrD,KAAK,iBAAiB,GAAG,CAAC,IAAI,EAAE,QAAQ,GAAG,IAAI,KAAK,IAAI,CAAC;AAEzD;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,IAAI;IAIH,OAAO,CAAC,QAAQ,CAAC,IAAI;IAHjC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAsB;IAC5C,OAAO,CAAC,SAAS,CAA2B;gBAEf,IAAI,EAAE,UAAU;IAO7C,OAAO,CAAC,MAAM;IAMd,OAAO,CAAC,YAAY;IAUpB,6EAA6E;IAC7E,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAOxC,kEAAkE;IA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