@groton/authjs-firestore-adapter 0.0.0-stage → 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/CHANGELOG.md +5 -0
- package/dist/AdapterAccounts.d.ts +13 -0
- package/dist/AdapterAccounts.js +42 -0
- package/dist/AdapterAuthenticators.d.ts +8 -0
- package/dist/AdapterAuthenticators.js +30 -0
- package/dist/AdapterSessions.d.ts +17 -0
- package/dist/AdapterSessions.js +45 -0
- package/dist/AdapterUsers.d.ts +16 -0
- package/dist/AdapterUsers.js +54 -0
- package/dist/VerificationTokens.d.ts +12 -0
- package/dist/VerificationTokens.js +20 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +37 -0
- package/package.json +41 -3
- package/README.md +0 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AdapterAccount, AdapterUser } from '@auth/core/adapters';
|
|
2
|
+
import { Collection, CollectionOptions } from '@battis/lazy-firestore';
|
|
3
|
+
export declare class AdapterAccountsCollection extends Collection<AdapterAccount> {
|
|
4
|
+
static readonly DEFAULT: {
|
|
5
|
+
path: string;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export declare function AdapterAccounts({ path, ...options }?: CollectionOptions): {
|
|
9
|
+
linkAccount: (data: AdapterAccount) => Promise<AdapterAccount | null>;
|
|
10
|
+
getAccount: (providerAccountId: AdapterAccount['providerAccountId'], provider: AdapterAccount['provider']) => Promise<AdapterAccount | null>;
|
|
11
|
+
getUserByAccount: ({ provider, providerAccountId }: Pick<AdapterAccount, 'provider' | 'providerAccountId'>) => Promise<AdapterUser | null>;
|
|
12
|
+
unlinkAccount: (account: Pick<AdapterAccount, 'provider' | 'providerAccountId'>) => Promise<AdapterAccount | undefined>;
|
|
13
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Collection } from '@battis/lazy-firestore';
|
|
2
|
+
import { requireUnique } from '@battis/lazy-firestore/directives/requireUnique';
|
|
3
|
+
import { users } from './AdapterUsers.js';
|
|
4
|
+
export class AdapterAccountsCollection extends Collection {
|
|
5
|
+
static DEFAULT = { path: 'accounts' };
|
|
6
|
+
}
|
|
7
|
+
export function AdapterAccounts({ path = AdapterAccountsCollection.DEFAULT.path, ...options } = AdapterAccountsCollection.DEFAULT) {
|
|
8
|
+
const accounts = new AdapterAccountsCollection({ path, ...options });
|
|
9
|
+
const getAccount = async (providerAccountId, provider) => requireUnique({
|
|
10
|
+
list: await accounts.list({
|
|
11
|
+
...identify({ provider, providerAccountId }),
|
|
12
|
+
strict: false
|
|
13
|
+
}),
|
|
14
|
+
strict: false
|
|
15
|
+
}).data;
|
|
16
|
+
return {
|
|
17
|
+
linkAccount: async (data) => (await accounts.create({
|
|
18
|
+
...identify(data),
|
|
19
|
+
data,
|
|
20
|
+
strict: false
|
|
21
|
+
})).data,
|
|
22
|
+
getAccount,
|
|
23
|
+
getUserByAccount: async ({ provider, providerAccountId }) => {
|
|
24
|
+
const account = await getAccount(provider, providerAccountId);
|
|
25
|
+
if (account) {
|
|
26
|
+
return (await users().read({ id: account.userId, strict: false })).data;
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
},
|
|
30
|
+
unlinkAccount: async (account) => (await accounts.delete({
|
|
31
|
+
...identify(account),
|
|
32
|
+
strict: false
|
|
33
|
+
})).data ?? undefined
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function identify({ provider, providerAccountId }) {
|
|
37
|
+
return {
|
|
38
|
+
query: (ref) => ref
|
|
39
|
+
.where('provider', '==', provider)
|
|
40
|
+
.where('providerAccountId', '==', providerAccountId)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AdapterAuthenticator } from '@auth/core/adapters';
|
|
2
|
+
import { CollectionOptions } from '@battis/lazy-firestore';
|
|
3
|
+
export declare function AdapterAuthenticators({ path, ...options }?: CollectionOptions): {
|
|
4
|
+
getAuthenticator: (id: AdapterAuthenticator['credentialID']) => Promise<AdapterAuthenticator | null>;
|
|
5
|
+
createAuthenticator: (data: AdapterAuthenticator) => Promise<AdapterAuthenticator>;
|
|
6
|
+
listAuthenticatorsByUserId: (userId: AdapterAuthenticator['userId']) => Promise<AdapterAuthenticator[]>;
|
|
7
|
+
updateAuthenticatorCounter: (credentialID: AdapterAuthenticator['credentialID'], newCounter: AdapterAuthenticator['counter']) => Promise<AdapterAuthenticator>;
|
|
8
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Collection } from '@battis/lazy-firestore';
|
|
2
|
+
import { DateConverter } from '@battis/lazy-firestore/converters/Date';
|
|
3
|
+
class AdapterAuthenticatorsCollection extends Collection {
|
|
4
|
+
static DEFAULT = { path: 'authenticators' };
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super({ ...options, converter: { expires: DateConverter } });
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function AdapterAuthenticators({ path = AdapterAuthenticatorsCollection.DEFAULT.path, ...options } = AdapterAuthenticatorsCollection.DEFAULT) {
|
|
10
|
+
const authenticators = new AdapterAuthenticatorsCollection({
|
|
11
|
+
path,
|
|
12
|
+
...options
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
getAuthenticator: async (id) => (await authenticators.read({ id, strict: false })).data,
|
|
16
|
+
createAuthenticator: async (data) => (await authenticators.create({
|
|
17
|
+
data,
|
|
18
|
+
id: data.credentialID,
|
|
19
|
+
strict: true
|
|
20
|
+
})).data,
|
|
21
|
+
listAuthenticatorsByUserId: async (userId) => (await authenticators.list({
|
|
22
|
+
query: (ref) => ref.where('userId', '==', userId)
|
|
23
|
+
})).map((doc) => doc.data),
|
|
24
|
+
updateAuthenticatorCounter: async (credentialID, newCounter) => (await authenticators.update({
|
|
25
|
+
id: credentialID,
|
|
26
|
+
patch: { counter: newCounter },
|
|
27
|
+
strict: true
|
|
28
|
+
})).data
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AdapterSession } from '@auth/core/adapters';
|
|
2
|
+
import { Collection, CollectionOptions } from '@battis/lazy-firestore';
|
|
3
|
+
export declare class AdapterSessionsCollection extends Collection<AdapterSession> {
|
|
4
|
+
static readonly DEFAULT: {
|
|
5
|
+
path: string;
|
|
6
|
+
};
|
|
7
|
+
constructor(options: CollectionOptions);
|
|
8
|
+
}
|
|
9
|
+
export declare function AdapterSessions({ path, ...options }?: CollectionOptions): {
|
|
10
|
+
createSession: (data: AdapterSession) => Promise<AdapterSession>;
|
|
11
|
+
getSessionAndUser: (id: string) => Promise<{
|
|
12
|
+
session: AdapterSession;
|
|
13
|
+
user: import("@auth/core/adapters").AdapterUser;
|
|
14
|
+
} | null>;
|
|
15
|
+
updateSession: (patch: Partial<AdapterSession> & Pick<AdapterSession, 'sessionToken'>) => Promise<AdapterSession | null>;
|
|
16
|
+
deleteSession: (id: string) => Promise<AdapterSession | null>;
|
|
17
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Collection } from '@battis/lazy-firestore';
|
|
2
|
+
import { DateConverter } from '@battis/lazy-firestore/converters/Date';
|
|
3
|
+
import { users } from './AdapterUsers.js';
|
|
4
|
+
export class AdapterSessionsCollection extends Collection {
|
|
5
|
+
static DEFAULT = { path: 'sessions' };
|
|
6
|
+
constructor(options) {
|
|
7
|
+
super({
|
|
8
|
+
...options,
|
|
9
|
+
converter: { expires: DateConverter }
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export function AdapterSessions({ path = AdapterSessionsCollection.DEFAULT.path, ...options } = AdapterSessionsCollection.DEFAULT) {
|
|
14
|
+
const sessions = new AdapterSessionsCollection({ path, ...options });
|
|
15
|
+
return {
|
|
16
|
+
createSession: async (data) => (await sessions.create({
|
|
17
|
+
id: data.sessionToken,
|
|
18
|
+
data,
|
|
19
|
+
strict: true
|
|
20
|
+
})).data,
|
|
21
|
+
getSessionAndUser: async (id) => {
|
|
22
|
+
const session = (await sessions.read({
|
|
23
|
+
id,
|
|
24
|
+
strict: false
|
|
25
|
+
})).data;
|
|
26
|
+
if (session) {
|
|
27
|
+
const user = (await users().read({ id: session.userId, strict: false }))
|
|
28
|
+
.data;
|
|
29
|
+
if (user) {
|
|
30
|
+
return { session, user };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
},
|
|
35
|
+
updateSession: async (patch) => (await sessions.update({
|
|
36
|
+
id: patch.sessionToken,
|
|
37
|
+
patch,
|
|
38
|
+
strict: false
|
|
39
|
+
})).data,
|
|
40
|
+
deleteSession: async (id) => (await sessions.delete({
|
|
41
|
+
id,
|
|
42
|
+
strict: false
|
|
43
|
+
})).data
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AdapterUser } from '@auth/core/adapters';
|
|
2
|
+
import { Collection, CollectionOptions } from '@battis/lazy-firestore';
|
|
3
|
+
export declare class AdapterUsersCollection extends Collection<AdapterUser> {
|
|
4
|
+
static readonly DEFAULT: {
|
|
5
|
+
path: string;
|
|
6
|
+
};
|
|
7
|
+
constructor(options: CollectionOptions);
|
|
8
|
+
}
|
|
9
|
+
export declare function users(): AdapterUsersCollection;
|
|
10
|
+
export declare function AdapterUsers({ path, ...options }?: CollectionOptions): {
|
|
11
|
+
createUser: (data: AdapterUser) => Promise<AdapterUser>;
|
|
12
|
+
getUser: (id: string) => Promise<AdapterUser | null>;
|
|
13
|
+
getUserByEmail: (email: string) => Promise<AdapterUser | null>;
|
|
14
|
+
updateUser: (patch: Partial<AdapterUser> & Pick<AdapterUser, 'id'>) => Promise<AdapterUser>;
|
|
15
|
+
deleteUser: (id: string) => Promise<AdapterUser | null>;
|
|
16
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Collection } from '@battis/lazy-firestore';
|
|
2
|
+
import { requireUnique } from '@battis/lazy-firestore/directives/requireUnique';
|
|
3
|
+
export class AdapterUsersCollection extends Collection {
|
|
4
|
+
static DEFAULT = { path: 'users' };
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super({
|
|
7
|
+
...options,
|
|
8
|
+
converter: {
|
|
9
|
+
emailVerified: {
|
|
10
|
+
deflate: (value) => (value ? value.toISOString() : null),
|
|
11
|
+
inflate: (value) => (value ? new Date(value) : null)
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
let _users = undefined;
|
|
18
|
+
export function users() {
|
|
19
|
+
if (_users) {
|
|
20
|
+
return _users;
|
|
21
|
+
}
|
|
22
|
+
throw new Error('Users collection not initialized');
|
|
23
|
+
}
|
|
24
|
+
export function AdapterUsers({ path = AdapterUsersCollection.DEFAULT.path, ...options } = AdapterUsersCollection.DEFAULT) {
|
|
25
|
+
_users = new AdapterUsersCollection({ path, ...options });
|
|
26
|
+
return {
|
|
27
|
+
createUser: async (data) => (await users().create({
|
|
28
|
+
data,
|
|
29
|
+
id: data.id,
|
|
30
|
+
query: (ref) => ref.where('email', '==', data.email),
|
|
31
|
+
strict: true
|
|
32
|
+
})).data,
|
|
33
|
+
getUser: async (id) => (await users().read({
|
|
34
|
+
id,
|
|
35
|
+
strict: false
|
|
36
|
+
})).data,
|
|
37
|
+
getUserByEmail: async (email) => requireUnique({
|
|
38
|
+
list: await users().list({
|
|
39
|
+
query: (ref) => ref.where('email', '==', email),
|
|
40
|
+
strict: false
|
|
41
|
+
}),
|
|
42
|
+
strict: false
|
|
43
|
+
}).data,
|
|
44
|
+
updateUser: async (patch) => (await users().update({
|
|
45
|
+
id: patch.id,
|
|
46
|
+
patch,
|
|
47
|
+
strict: true
|
|
48
|
+
})).data,
|
|
49
|
+
deleteUser: async (id) => (await users().delete({
|
|
50
|
+
id,
|
|
51
|
+
strict: false
|
|
52
|
+
})).data
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { VerificationToken } from '@auth/core/adapters';
|
|
2
|
+
import { Collection, CollectionOptions } from '@battis/lazy-firestore';
|
|
3
|
+
export declare class VerificationTokensCollection extends Collection<VerificationToken> {
|
|
4
|
+
static readonly DEFAULT: {
|
|
5
|
+
path: string;
|
|
6
|
+
};
|
|
7
|
+
constructor(options: CollectionOptions);
|
|
8
|
+
}
|
|
9
|
+
export declare function VerificationTokens({ path, ...options }?: CollectionOptions): {
|
|
10
|
+
createVerificationToken: (data: VerificationToken) => Promise<VerificationToken | null>;
|
|
11
|
+
useVerificationToken: (token: Pick<VerificationToken, 'identifier' | 'token'>) => Promise<VerificationToken | null>;
|
|
12
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Collection } from '@battis/lazy-firestore';
|
|
2
|
+
import { DateConverter } from '@battis/lazy-firestore/converters/Date';
|
|
3
|
+
export class VerificationTokensCollection extends Collection {
|
|
4
|
+
static DEFAULT = { path: 'verificationTokens' };
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super({ ...options, converter: { expires: DateConverter } });
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export function VerificationTokens({ path = VerificationTokensCollection.DEFAULT.path, ...options } = VerificationTokensCollection.DEFAULT) {
|
|
10
|
+
const tokens = new VerificationTokensCollection({ path, ...options });
|
|
11
|
+
return {
|
|
12
|
+
createVerificationToken: async (data) => (await tokens.create({ data, ...identify(data), strict: false })).data,
|
|
13
|
+
useVerificationToken: async (token) => (await tokens.delete({ ...identify(token), strict: false })).data
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function identify({ identifier, token }) {
|
|
17
|
+
return {
|
|
18
|
+
query: (ref) => ref.where('identifier', '==', identifier).where('token', '==', token)
|
|
19
|
+
};
|
|
20
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Adapter } from '@auth/core/adapters';
|
|
2
|
+
import { CollectionOptions } from '@battis/lazy-firestore';
|
|
3
|
+
interface Options extends Omit<CollectionOptions, 'path'> {
|
|
4
|
+
basePath?: string;
|
|
5
|
+
usersPath?: string;
|
|
6
|
+
accountsPath?: string;
|
|
7
|
+
sessionsPath?: string;
|
|
8
|
+
verificationTokensPath?: string;
|
|
9
|
+
authenticatorsPath?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function FirestoreAdapter({ db, databaseId, basePath, usersPath, accountsPath, sessionsPath, verificationTokensPath, authenticatorsPath, firestore }?: Options): Adapter;
|
|
12
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { Firestore } from '@google-cloud/firestore';
|
|
3
|
+
import { AdapterAccounts, AdapterAccountsCollection } from './AdapterAccounts.js';
|
|
4
|
+
import { AdapterAuthenticators } from './AdapterAuthenticators.js';
|
|
5
|
+
import { AdapterSessions, AdapterSessionsCollection } from './AdapterSessions.js';
|
|
6
|
+
import { AdapterUsers, AdapterUsersCollection } from './AdapterUsers.js';
|
|
7
|
+
import { VerificationTokens, VerificationTokensCollection } from './VerificationTokens.js';
|
|
8
|
+
export function FirestoreAdapter({ db, databaseId, basePath = '.', usersPath, accountsPath, sessionsPath, verificationTokensPath, authenticatorsPath, firestore } = {}) {
|
|
9
|
+
db = db || new Firestore({ databaseId, ...firestore });
|
|
10
|
+
return {
|
|
11
|
+
...AdapterUsers({
|
|
12
|
+
db,
|
|
13
|
+
path: path.join(
|
|
14
|
+
/*turbopackIgnore: true*/ basePath, usersPath || AdapterUsersCollection.DEFAULT.path)
|
|
15
|
+
}),
|
|
16
|
+
...AdapterAccounts({
|
|
17
|
+
db,
|
|
18
|
+
path: path.join(
|
|
19
|
+
/*turbopackIgnore: true*/ basePath, accountsPath || AdapterAccountsCollection.DEFAULT.path)
|
|
20
|
+
}),
|
|
21
|
+
...AdapterSessions({
|
|
22
|
+
db,
|
|
23
|
+
path: path.join(
|
|
24
|
+
/*turbopackIgnore: true*/ basePath, sessionsPath || AdapterSessionsCollection.DEFAULT.path)
|
|
25
|
+
}),
|
|
26
|
+
...VerificationTokens({
|
|
27
|
+
db,
|
|
28
|
+
path: path.join(
|
|
29
|
+
/*turbopackIgnore: true*/ basePath, verificationTokensPath || VerificationTokensCollection.DEFAULT.path)
|
|
30
|
+
}),
|
|
31
|
+
...AdapterAuthenticators({
|
|
32
|
+
db,
|
|
33
|
+
path: path.join(
|
|
34
|
+
/*turbopackIgnore: true*/ basePath, authenticatorsPath || AdapterAccountsCollection.DEFAULT.path)
|
|
35
|
+
})
|
|
36
|
+
};
|
|
37
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@groton/authjs-firestore-adapter",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"homepage": "https://github.com/groton-school/authjs/tree/main/packages/firestore-adapter#readme",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "github",
|
|
7
|
+
"url": "https://github.com/groton-school/authjs.git",
|
|
8
|
+
"directory": "packages/firestore-adapter"
|
|
9
|
+
},
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Seth Battis",
|
|
12
|
+
"email": "sbattis@groton.org"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./dist/index.js",
|
|
17
|
+
"./collections/accounts": "./dist/AdapterAccounts.js",
|
|
18
|
+
"./collections/users": "./dist/AdapterUsers.js",
|
|
19
|
+
"./collections/sessions": "./dist/AdapterSessions.js",
|
|
20
|
+
"./collections/authenticators": "./dist/AdapterAuthenticators.js"
|
|
21
|
+
},
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@battis/lazy-firestore": "^1.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@battis/workspace": "^0.0.12",
|
|
28
|
+
"@tsconfig/node-lts": "^24.0.1",
|
|
29
|
+
"@types/node": "^24.19.2",
|
|
30
|
+
"del-cli": "^8.0.1",
|
|
31
|
+
"npm-run-all": "^4.1.5",
|
|
32
|
+
"typescript": "^7.0.2"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@auth/core": "^0.41.3",
|
|
36
|
+
"@google-cloud/firestore": "*"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "run-s build:*",
|
|
40
|
+
"build:clean": "del ./dist",
|
|
41
|
+
"build:compile": "tsc",
|
|
42
|
+
"release": "commit-and-tag-version --noBumpWhenEmptyChanges"
|
|
43
|
+
}
|
|
6
44
|
}
|
package/README.md
DELETED