-
Notifications
You must be signed in to change notification settings - Fork 3
/
auth-setup.ts
98 lines (78 loc) · 2.42 KB
/
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
import { APP_NAME } from '@/utils/config'
import { createClient } from 'edgedb'
import crypto from 'node:crypto'
import process from 'node:process'
const client = createClient()
const CONFIG = {
tokenTTL: '336 hours',
magicLinkTokenTTL: 'PT10M',
signingKey: crypto.randomBytes(32).toString('hex'),
emailVerification: false,
providers: [
{
providerType: 'GoogleOAuthProvider',
clientId: process.env.GOOGLE_CLIENT_ID,
secret: process.env.GOOGLE_SECRET,
},
],
appName: APP_NAME,
/** Email sending */
SMTP: {
sender: '[email protected]',
username: process.env.SMTP_USERNAME,
password: process.env.SMTP_PASSWORD,
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
security: process.env.SMTP_SECURITY,
validate_certs: Boolean(process.env.SMTP_VALIDATE_CERTS),
},
} as const
let query = `
CONFIGURE CURRENT BRANCH
RESET ext::auth::ProviderConfig;
CONFIGURE CURRENT BRANCH
RESET ext::auth::AuthConfig;
CONFIGURE CURRENT BRANCH
RESET ext::auth::UIConfig;
CONFIGURE CURRENT BRANCH
RESET ext::auth::SMTPConfig;
CONFIGURE CURRENT BRANCH SET
ext::auth::AuthConfig::auth_signing_key := '${CONFIG.signingKey}';
CONFIGURE CURRENT BRANCH SET
ext::auth::AuthConfig::token_time_to_live := <duration>'${CONFIG.tokenTTL}';
CONFIGURE CURRENT BRANCH
INSERT ext::auth::MagicLinkProviderConfig {
token_time_to_live := <duration>'${CONFIG.magicLinkTokenTTL}',
};
CONFIGURE CURRENT BRANCH SET
ext::auth::SMTPConfig::sender := '${CONFIG.SMTP.sender}';
CONFIGURE CURRENT BRANCH SET
ext::auth::SMTPConfig::host := '${CONFIG.SMTP.host}';
CONFIGURE CURRENT BRANCH SET
ext::auth::SMTPConfig::port := <int32>${CONFIG.SMTP.port};
CONFIGURE CURRENT BRANCH SET
ext::auth::SMTPConfig::security := '${CONFIG.SMTP.security}';
CONFIGURE CURRENT BRANCH SET
ext::auth::SMTPConfig::validate_certs := ${CONFIG.SMTP.validate_certs};
`
if (CONFIG.SMTP.username && CONFIG.SMTP.password) {
query += `
CONFIGURE CURRENT BRANCH SET
ext::auth::SMTPConfig::username := '${CONFIG.SMTP.username}';
CONFIGURE CURRENT BRANCH SET
ext::auth::SMTPConfig::password := '${CONFIG.SMTP.password}';
`
}
for (const { providerType, clientId, secret } of CONFIG.providers) {
query += `
CONFIGURE CURRENT BRANCH
INSERT ext::auth::${providerType} {
secret := '${secret}',
client_id := '${clientId}'
};
`
}
await client
.execute(query)
.catch(console.error)
.then(() => console.log('Auth setup complete'))