-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.ts
174 lines (161 loc) · 4.57 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import type {
Adapter,
DatabaseSession,
DatabaseUser,
Session,
User,
} from "npm:lucia@3";
/**
* @example
* ```ts
* import { Lucia } from "lucia";
* import { DenoKVAdapter } from "@cotyhamilton/lucia-adapter-denokv";
*
* const kv = await Deno.openKv();
*
* export const lucia = new Lucia(new DenoKVAdapter(kv), {
* getUserAttributes: (attributes) => {
* return {
* // attributes has the type of DatabaseUserAttributes
* name: attributes.name,
* }
* },
* });
*
* declare module "lucia" {
* interface Register {
* Lucia: typeof lucia;
* DatabaseUserAttributes: DatabaseUserAttributes;
* }
* }
*
* interface DatabaseUserAttributes {
* name: string;
* }
* ```
*
* @extends Adapter
*/
export class DenoKVAdapter implements Adapter {
private kv: Deno.Kv;
constructor(
kv: Deno.Kv,
) {
this.kv = kv;
}
public async getSessionAndUser(
sessionId: string,
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
const [databaseSession, databaseUser] = await Promise.all([
this.getSession(sessionId),
this.getUserFromSessionId(sessionId),
]);
return [databaseSession, databaseUser];
}
public async getUserSessions(userId: string): Promise<DatabaseSession[]> {
const iter = this.kv.list<Session>({
prefix: ["sessions_by_user", userId],
});
const sessions: Session[] = [];
for await (const { value } of iter) {
sessions.push(value);
}
return sessions.map(transformIntoDatabaseSession);
}
public async setSession(session: DatabaseSession): Promise<void> {
const primaryKey = ["sessions", session.id];
const byUserKey = ["sessions_by_user", session.userId, session.id];
const expireIn = session.expiresAt.getTime() - Date.now();
const { attributes, ...sessionData } = session;
await this.kv.atomic()
.check({ key: primaryKey, versionstamp: null })
.check({ key: byUserKey, versionstamp: null })
.set(primaryKey, {
...sessionData,
...attributes,
expiresAt: session.expiresAt.getTime(),
}, {
expireIn,
})
.set(byUserKey, {
...sessionData,
...attributes,
expiresAt: session.expiresAt.getTime(),
}, {
expireIn,
})
.commit();
}
public async updateSessionExpiration(
sessionId: string,
expiresAt: Date,
): Promise<void> {
const res = await this.kv.get<Session>(["sessions", sessionId]);
if (res.value) {
const session = { ...res.value, expiresAt: expiresAt.getTime() };
const expireIn = expiresAt.getTime() - Date.now();
await this.kv.atomic()
.set(["sessions", sessionId], session, { expireIn })
.set(["sessions_by_user", session.userId, sessionId], session, {
expireIn,
})
.commit();
}
}
public async deleteSession(sessionId: string): Promise<void> {
const res = await this.kv.get<Session>(["sessions", sessionId]);
if (res.value) {
await this.kv.atomic()
.delete(["sessions", sessionId])
.delete(["sessions_by_user", res.value.userId, sessionId])
.commit();
}
}
public async deleteUserSessions(userId: string): Promise<void> {
const iter = this.kv.list<Session>({
prefix: ["sessions_by_user", userId],
});
for await (const res of iter) {
await this.kv.atomic()
.delete(["sessions", res.value.id])
.delete(["sessions_by_user", res.value.userId, res.value.id])
.commit();
}
}
// handled by key expiration
public async deleteExpiredSessions(): Promise<void> {
}
private async getSession(sessionId: string): Promise<DatabaseSession | null> {
const result = (await this.kv.get<Session>(["sessions", sessionId])).value;
if (!result) return null;
return transformIntoDatabaseSession(result);
}
private async getUserFromSessionId(
sessionId: string,
): Promise<DatabaseUser | null> {
const session =
(await this.kv.get<DatabaseSession>(["sessions", sessionId])).value;
if (!session) {
return null;
}
const result = (await this.kv.get<User>(["users", session.userId])).value;
if (!result) return null;
return transformIntoDatabaseUser(result);
}
}
function transformIntoDatabaseSession(raw: Session): DatabaseSession {
const { id, userId, expiresAt, ...attributes } = raw;
return {
userId,
id,
expiresAt: new Date(expiresAt),
attributes,
};
}
function transformIntoDatabaseUser(raw: User): DatabaseUser {
const { id, ...attributes } = raw;
return {
id,
attributes,
};
}