-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
47 lines (44 loc) · 1.11 KB
/
auth.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
// sets up openauth server
// as per docs: https://openauth.js.org/docs
import { issuer } from "@openauthjs/openauth"
import { MemoryStorage } from "@openauthjs/openauth/storage/memory"
import { PasswordProvider } from "@openauthjs/openauth/provider/password"
import { PasswordUI } from "@openauthjs/openauth/ui/password"
import { object, string } from "valibot"
import { createSubjects } from "@openauthjs/openauth/subject"
const subjects = createSubjects({
user: object({
id: string(),
}),
})
async function getUser(email: string) {
// Get user from database
// Return user ID
return "123"
}
export default issuer({
subjects,
storage: MemoryStorage({
persist: "./persist.json",
}),
providers: {
password: PasswordProvider(
PasswordUI({
sendCode: async (email, code) => {
console.log(email, code)
},
})
),
},
async allow() {
return true
},
success: async (ctx, value) => {
if (value.provider === "password") {
return ctx.subject("user", {
id: await getUser(value.email),
})
}
throw new Error("Invalid provider")
},
})