/
lucia.ts
177 lines (170 loc) · 5.23 KB
/
lucia.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
175
176
177
// lucia.ts
import {
Adapter,
KeySchema,
LuciaErrorConstructor,
SessionSchema,
UserSchema,
lucia,
} from "lucia";
import { DatabaseReader, DatabaseWriter } from "./_generated/server";
type SessionId = string;
type UserId = string;
type KeyId = string;
export function getAuth(db: DatabaseWriter) {
return lucia({
// We cheat to allow queries to use `getAuth`
adapter: convexAdapter(db),
// TODO: Set the LUCIA_ENVIRONMENT variable to "PROD"
// on your prod deployment's dashboard
env: (process.env.LUCIA_ENVIRONMENT as "PROD" | undefined) ?? "DEV",
getUserAttributes(user: UserSchema) {
return {
_id: user._id,
_creationTime: user._creationTime,
email: user.email,
};
},
});
}
const convexAdapter = (db: DatabaseWriter) => {
return (LuciaError: LuciaErrorConstructor): Adapter => ({
async getSessionAndUser(
sessionId: string
): Promise<[SessionSchema, UserSchema] | [null, null]> {
const session = await getSession(db, sessionId);
if (session === null) {
return [null, null];
}
const user = await getUser(db, session.user_id);
if (user === null) {
return [null, null];
}
return [session, user];
},
async deleteSession(sessionId: SessionId): Promise<void> {
const session = await getSession(db, sessionId);
if (session === null) {
return;
}
await db.delete(session._id);
},
async deleteSessionsByUserId(userId: UserId): Promise<void> {
const sessions = await this.getSessionsByUserId(userId);
await Promise.all(sessions.map((session) => db.delete(session._id)));
},
async getSession(sessionId: SessionId): Promise<SessionSchema | null> {
return await getSession(db, sessionId);
},
async getSessionsByUserId(userId: UserId): Promise<SessionSchema[]> {
return await db
.query("sessions")
.withIndex("byUserId", (q) => q.eq("user_id", userId))
.collect();
},
async setSession(session: SessionSchema): Promise<void> {
const { _id, _creationTime, ...data } = session;
await db.insert("sessions", data);
},
async deleteKeysByUserId(userId: UserId): Promise<void> {
const keys = await db
.query("auth_keys")
.withIndex("byUserId", (q) => q.eq("user_id", userId))
.collect();
await Promise.all(keys.map((key) => db.delete(key._id)));
},
async deleteKey(keyId: KeyId): Promise<void> {
const key = await getKey(db, keyId);
if (key === null) {
return;
}
await db.delete(key._id);
},
async deleteUser(userId: UserId): Promise<void> {
const user = await getUser(db, userId);
if (user === null) {
return;
}
await db.delete(user._id);
},
async getKey(keyId: KeyId): Promise<KeySchema | null> {
return await getKey(db, keyId);
},
async getKeysByUserId(userId: UserId): Promise<KeySchema[]> {
return await db
.query("auth_keys")
.withIndex("byUserId", (q) => q.eq("user_id", userId))
.collect();
},
async getUser(userId: UserId): Promise<UserSchema | null> {
return await getUser(db, userId);
},
async setKey(key: KeySchema): Promise<void> {
const existingKey = await this.getKey(key.id);
if (existingKey !== null) {
throw new LuciaError("AUTH_DUPLICATE_KEY_ID");
}
const user = await this.getUser(key.user_id);
if (user === null) {
throw new LuciaError("AUTH_INVALID_USER_ID");
}
await db.insert("auth_keys", key);
},
async setUser(user: UserSchema, key: KeySchema | null): Promise<void> {
const { _id, _creationTime, ...data } = user;
await db.insert("users", data);
if (key !== null) {
await this.setKey(key);
}
},
async updateKey(
keyId: string,
partialKey: Partial<KeySchema>
): Promise<void> {
const key = await getKey(db, keyId);
if (key === null) {
throw new LuciaError("AUTH_INVALID_KEY_ID");
}
await db.patch(key._id, partialKey);
},
async updateUser(
userId: string,
partialUser: Partial<UserSchema>
): Promise<void> {
const user = await getUser(db, userId);
if (user === null) {
throw new LuciaError("AUTH_INVALID_USER_ID");
}
await db.patch(user._id, partialUser);
},
async updateSession(
sessionId: string,
partialSession: Partial<SessionSchema>
): Promise<void> {
const session = await getSession(db, sessionId);
if (session === null) {
throw new LuciaError("AUTH_INVALID_SESSION_ID");
}
await db.patch(session._id, partialSession);
},
});
};
async function getSession(db: DatabaseReader, sessionId: string) {
return await db
.query("sessions")
.withIndex("byId", (q) => q.eq("id", sessionId))
.first();
}
async function getUser(db: DatabaseReader, userId: string) {
return await db
.query("users")
.withIndex("byId", (q) => q.eq("id", userId))
.first();
}
async function getKey(db: DatabaseReader, keyId: string) {
return await db
.query("auth_keys")
.withIndex("byId", (q) => q.eq("id", keyId))
.first();
}
export type Auth = ReturnType<typeof getAuth>;