forked from learn-anything/learn-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword-auth-setup.ts
222 lines (197 loc) · 5.23 KB
/
password-auth-setup.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import process from "node:process"
import crypto from "node:crypto"
import { createClient } from "edgedb"
const client = createClient()
type AuthConfig = {
token_time_to_live: string
providers: {
name: string
url: string | null
secret: string | null
client_id: string | null
}[]
ui: {
redirect_to: string
redirect_to_on_signup: string
app_name: string
logo_url: string
dark_logo_url: string
brand_color: string
} | null
}
async function main() {
const inquirer = (await import("inquirer")).default
const existingConfig = await client.queryRequiredSingle<AuthConfig>(`
SELECT cfg::Config.extensions[is ext::auth::AuthConfig] {
*,
providers: {
name,
[is ext::auth::OAuthProviderConfig].*,
},
ui: { * },
} limit 1
`)
if (existingConfig.providers.length > 0) {
console.warn(
`Auth is already configured with the following values:
${JSON.stringify(existingConfig, null, 2)}
`,
)
}
const questions = [
{
type: "input",
name: "appName",
message: "Enter the app name:",
},
{
type: "input",
name: "authSigningKey",
message: "Enter the signing key:",
default: crypto.randomBytes(32).toString("hex"),
validate: (val: string) =>
val.length >= 32 || "The key must be at least 32 bytes long",
},
{
type: "input",
name: "tokenTTL",
message: "Enter the token time to live:",
default: existingConfig.token_time_to_live.toString() ?? "336 hours",
},
]
const answers = await inquirer.prompt(questions)
let query = `
CONFIGURE CURRENT DATABASE
RESET ext::auth::ProviderConfig;
CONFIGURE CURRENT DATABASE
RESET ext::auth::AuthConfig;
CONFIGURE CURRENT DATABASE
RESET ext::auth::UIConfig;
CONFIGURE CURRENT DATABASE
RESET ext::auth::SMTPConfig;
CONFIGURE CURRENT DATABASE SET
ext::auth::AuthConfig::auth_signing_key := '${answers.authSigningKey}';
`
if (answers.tokenTTL) {
query += `
CONFIGURE CURRENT DATABASE SET
ext::auth::AuthConfig::token_time_to_live := <duration>'${answers.tokenTTL}';
`
}
query += `
CONFIGURE CURRENT DATABASE
INSERT ext::auth::EmailPasswordProviderConfig {
require_verification := false,
};
`
const hostedUi = await inquirer.prompt([
{
type: "input",
name: "redirectTo",
message: "Enter the redirect URL:",
default:
existingConfig.ui?.redirect_to ??
"http://localhost:3001/auth/builtin/callback",
required: true,
},
{
type: "input",
name: "redirectToOnSignup",
message: "Enter the redirect URL on signup:",
default:
existingConfig.ui?.redirect_to_on_signup ??
"http://localhost:3001/auth/builtin/callback?isSignUp=true",
required: false,
},
{
type: "input",
name: "brandColor",
message: "Enter the brand color:",
default: existingConfig.ui?.brand_color ?? "#000000",
required: false,
},
{
type: "input",
name: "logo_url",
message: "Enter the brand logo",
default:
existingConfig.ui?.logo_url ??
"https://avatars.githubusercontent.com/u/14262913",
},
])
query += `
CONFIGURE CURRENT DATABASE
INSERT ext::auth::UIConfig {
redirect_to := '${hostedUi.redirectTo}',
redirect_to_on_signup := '${hostedUi.redirectToOnSignup ?? hostedUi.redirectTo}',
app_name := '${answers.appName}',
brand_color := '${hostedUi.brandColor}',
logo_url := '${hostedUi.logo_url}',
};
`
const smtpConfig = await inquirer.prompt([
{
type: "input",
name: "sender",
message: "Sender email:",
required: true,
},
{
type: "input",
name: "host",
message: "SMTP Host:",
default: "localhost",
required: true,
},
{
type: "number",
name: "port",
message: "SMTP Port:",
default: 1025,
required: true,
},
{
type: "input",
name: "username",
message: "SMTP Username:",
},
{
type: "input",
name: "password",
message: "SMTP Password:",
},
])
query += `
CONFIGURE CURRENT DATABASE SET
ext::auth::SMTPConfig::sender := '${smtpConfig.sender}';
CONFIGURE CURRENT DATABASE SET
ext::auth::SMTPConfig::host := '${smtpConfig.host}';
CONFIGURE CURRENT DATABASE SET
ext::auth::SMTPConfig::port := <int32>${smtpConfig.port};
CONFIGURE CURRENT DATABASE SET
ext::auth::SMTPConfig::username := '${smtpConfig.username}';
CONFIGURE CURRENT DATABASE SET
ext::auth::SMTPConfig::password := '${smtpConfig.password}';
CONFIGURE CURRENT DATABASE SET
ext::auth::SMTPConfig::security := 'TLS';
CONFIGURE CURRENT DATABASE SET
ext::auth::SMTPConfig::validate_certs := false;
`
console.log("The following query will be executed:\n", query)
const confirm = await inquirer.prompt({
type: "confirm",
name: "execute",
message: "Do you want to execute this query?",
})
if (confirm.execute) {
await client.execute(query)
} else {
return
}
}
main()
.then(() => process.exit(0))
.catch((err: Error) => {
console.error(err)
process.exit(1)
})