@auth/firebase-adapter 2.5.2 → 2.6.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/index.d.ts CHANGED
@@ -29,17 +29,30 @@ export interface FirebaseAdapterConfig extends AppOptions {
29
29
  *
30
30
  *
31
31
  * @example
32
- * ```ts title="pages/api/auth/[...nextauth].ts"
33
- * import NextAuth from "next-auth"
34
- * import { FirestoreAdapter } from "@auth/firebase-adapter"
35
- *
36
- * export default NextAuth({
32
+ * ```ts
33
+ * // This will convert all field and collection names to snake_case
37
34
  * adapter: FirestoreAdapter({ namingStrategy: "snake_case" })
38
35
  * // ...
39
36
  * })
40
37
  * ```
41
38
  */
42
39
  namingStrategy?: "snake_case" | "default";
40
+ /**
41
+ * Use this option if you already have one of the default collections in your Firestore database.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * // This will use the collection name "authjs_users" instead of the default "users"
46
+ * adapter: FirestoreAdapter({ collections: { users: "authjs_users" } })
47
+ * // ...
48
+ * ```
49
+ */
50
+ collections?: {
51
+ users?: string;
52
+ sessions?: string;
53
+ accounts?: string;
54
+ verificationTokens?: string;
55
+ };
43
56
  }
44
57
  export declare function FirestoreAdapter(config?: FirebaseAdapterConfig | Firestore): Adapter;
45
58
  /**
package/index.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,UAAU,EAA0B,MAAM,oBAAoB,CAAA;AAE5E,OAAO,EACL,SAAS,EAIV,MAAM,0BAA0B,CAAA;AAEjC,OAAO,KAAK,EACV,OAAO,EAKR,MAAM,qBAAqB,CAAA;AAE5B,sCAAsC;AACtC,MAAM,WAAW,qBAAsB,SAAQ,UAAU;IACvD;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB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O,aAQpE"}
package/index.js CHANGED
@@ -14,11 +14,19 @@
14
14
  import { getApps, initializeApp } from "firebase-admin/app";
15
15
  import { Firestore, getFirestore, initializeFirestore, Timestamp, } from "firebase-admin/firestore";
16
16
  export function FirestoreAdapter(config) {
17
- const { db, namingStrategy = "default" } = config instanceof Firestore
17
+ const { db, namingStrategy = "default", collections = {}, } = config instanceof Firestore
18
18
  ? { db: config }
19
19
  : { ...config, db: config?.firestore ?? initFirestore(config) };
20
20
  const preferSnakeCase = namingStrategy === "snake_case";
21
- const C = collectionsFactory(db, preferSnakeCase);
21
+ const C = collectionsFactory(db, preferSnakeCase, {
22
+ users: "users",
23
+ sessions: "sessions",
24
+ accounts: "accounts",
25
+ verificationTokens: preferSnakeCase
26
+ ? "verification_tokens"
27
+ : "verificationTokens",
28
+ ...collections,
29
+ });
22
30
  const mapper = mapFieldsFactory(preferSnakeCase);
23
31
  return {
24
32
  async createUser(userInit) {
@@ -154,9 +162,8 @@ export function mapFieldsFactory(preferSnakeCase) {
154
162
  }
155
163
  return { toDb: identity, fromDb: identity };
156
164
  }
157
- /** @internal */
158
165
  function getConverter(options) {
159
- const mapper = mapFieldsFactory(options?.preferSnakeCase ?? false);
166
+ const mapper = mapFieldsFactory(options?.preferSnakeCase);
160
167
  return {
161
168
  toFirestore(object) {
162
169
  const document = {};
@@ -194,7 +201,6 @@ export async function getOneDoc(querySnapshot) {
194
201
  const querySnap = await querySnapshot.limit(1).get();
195
202
  return querySnap.docs[0]?.data() ?? null;
196
203
  }
197
- /** @internal */
198
204
  async function deleteDocs(querySnapshot) {
199
205
  const querySnap = await querySnapshot.get();
200
206
  for (const doc of querySnap.docs) {
@@ -207,19 +213,19 @@ export async function getDoc(docRef) {
207
213
  return docSnap.data() ?? null;
208
214
  }
209
215
  /** @internal */
210
- export function collectionsFactory(db, preferSnakeCase = false) {
216
+ export function collectionsFactory(db, preferSnakeCase = false, collections) {
211
217
  return {
212
218
  users: db
213
- .collection("users")
219
+ .collection(collections.users)
214
220
  .withConverter(getConverter({ preferSnakeCase })),
215
221
  sessions: db
216
- .collection("sessions")
222
+ .collection(collections.sessions)
217
223
  .withConverter(getConverter({ preferSnakeCase })),
218
224
  accounts: db
219
- .collection("accounts")
225
+ .collection(collections.accounts)
220
226
  .withConverter(getConverter({ preferSnakeCase })),
221
227
  verification_tokens: db
222
- .collection(preferSnakeCase ? "verification_tokens" : "verificationTokens")
228
+ .collection(collections.verificationTokens)
223
229
  .withConverter(getConverter({ preferSnakeCase, excludeId: true })),
224
230
  };
225
231
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@auth/firebase-adapter",
3
- "version": "2.5.2",
3
+ "version": "2.6.0",
4
4
  "description": "Firebase adapter for Auth.js",
5
5
  "homepage": "https://authjs.dev",
6
6
  "repository": "https://github.com/nextauthjs/next-auth",
@@ -37,7 +37,7 @@
37
37
  "access": "public"
38
38
  },
39
39
  "dependencies": {
40
- "@auth/core": "0.35.2"
40
+ "@auth/core": "0.36.0"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "firebase-admin": "^12.0.0"
package/src/index.ts CHANGED
@@ -44,29 +44,53 @@ export interface FirebaseAdapterConfig extends AppOptions {
44
44
  *
45
45
  *
46
46
  * @example
47
- * ```ts title="pages/api/auth/[...nextauth].ts"
48
- * import NextAuth from "next-auth"
49
- * import { FirestoreAdapter } from "@auth/firebase-adapter"
50
- *
51
- * export default NextAuth({
47
+ * ```ts
48
+ * // This will convert all field and collection names to snake_case
52
49
  * adapter: FirestoreAdapter({ namingStrategy: "snake_case" })
53
50
  * // ...
54
51
  * })
55
52
  * ```
56
53
  */
57
54
  namingStrategy?: "snake_case" | "default"
55
+ /**
56
+ * Use this option if you already have one of the default collections in your Firestore database.
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * // This will use the collection name "authjs_users" instead of the default "users"
61
+ * adapter: FirestoreAdapter({ collections: { users: "authjs_users" } })
62
+ * // ...
63
+ * ```
64
+ */
65
+ collections?: {
66
+ users?: string
67
+ sessions?: string
68
+ accounts?: string
69
+ verificationTokens?: string
70
+ }
58
71
  }
59
72
 
60
73
  export function FirestoreAdapter(
61
74
  config?: FirebaseAdapterConfig | Firestore
62
75
  ): Adapter {
63
- const { db, namingStrategy = "default" } =
64
- config instanceof Firestore
65
- ? { db: config }
66
- : { ...config, db: config?.firestore ?? initFirestore(config) }
76
+ const {
77
+ db,
78
+ namingStrategy = "default",
79
+ collections = {},
80
+ } = config instanceof Firestore
81
+ ? { db: config }
82
+ : { ...config, db: config?.firestore ?? initFirestore(config) }
67
83
 
68
84
  const preferSnakeCase = namingStrategy === "snake_case"
69
- const C = collectionsFactory(db, preferSnakeCase)
85
+ const C = collectionsFactory(db, preferSnakeCase, {
86
+ users: "users",
87
+ sessions: "sessions",
88
+ accounts: "accounts",
89
+ verificationTokens: preferSnakeCase
90
+ ? "verification_tokens"
91
+ : "verificationTokens",
92
+ ...collections,
93
+ })
70
94
  const mapper = mapFieldsFactory(preferSnakeCase)
71
95
 
72
96
  return {
@@ -247,12 +271,11 @@ export function mapFieldsFactory(preferSnakeCase?: boolean) {
247
271
  return { toDb: identity, fromDb: identity }
248
272
  }
249
273
 
250
- /** @internal */
251
274
  function getConverter<Document extends Record<string, any>>(options: {
252
275
  excludeId?: boolean
253
276
  preferSnakeCase?: boolean
254
277
  }): FirebaseFirestore.FirestoreDataConverter<Document> {
255
- const mapper = mapFieldsFactory(options?.preferSnakeCase ?? false)
278
+ const mapper = mapFieldsFactory(options?.preferSnakeCase)
256
279
 
257
280
  return {
258
281
  toFirestore(object) {
@@ -302,7 +325,6 @@ export async function getOneDoc<T>(
302
325
  return querySnap.docs[0]?.data() ?? null
303
326
  }
304
327
 
305
- /** @internal */
306
328
  async function deleteDocs<T>(
307
329
  querySnapshot: FirebaseFirestore.Query<T>
308
330
  ): Promise<void> {
@@ -323,22 +345,21 @@ export async function getDoc<T>(
323
345
  /** @internal */
324
346
  export function collectionsFactory(
325
347
  db: FirebaseFirestore.Firestore,
326
- preferSnakeCase = false
348
+ preferSnakeCase = false,
349
+ collections: Required<NonNullable<FirebaseAdapterConfig["collections"]>>
327
350
  ) {
328
351
  return {
329
352
  users: db
330
- .collection("users")
353
+ .collection(collections.users)
331
354
  .withConverter(getConverter<AdapterUser>({ preferSnakeCase })),
332
355
  sessions: db
333
- .collection("sessions")
356
+ .collection(collections.sessions)
334
357
  .withConverter(getConverter<AdapterSession>({ preferSnakeCase })),
335
358
  accounts: db
336
- .collection("accounts")
359
+ .collection(collections.accounts)
337
360
  .withConverter(getConverter<AdapterAccount>({ preferSnakeCase })),
338
361
  verification_tokens: db
339
- .collection(
340
- preferSnakeCase ? "verification_tokens" : "verificationTokens"
341
- )
362
+ .collection(collections.verificationTokens)
342
363
  .withConverter(
343
364
  getConverter<VerificationToken>({ preferSnakeCase, excludeId: true })
344
365
  ),