Skip to content

Commit 55ae2c8

Browse files
authored
Merge branch 'main' into gs/gdrive-create-discussion-relations
2 parents b868ef2 + 61a0012 commit 55ae2c8

File tree

9 files changed

+578
-13
lines changed

9 files changed

+578
-13
lines changed

core/prisma/create-admin-user.cts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* eslint-disable no-console */
2+
3+
import { createEnv } from "@t3-oss/env-nextjs";
4+
import { Kysely, PostgresDialect } from "kysely";
5+
import * as pg from "pg";
6+
import { z } from "zod";
7+
8+
import { Database } from "db/Database";
9+
10+
import { isUniqueConstraintError } from "../kysely/errors";
11+
import { createPasswordHash } from "../lib/authentication/password";
12+
13+
const env = createEnv({
14+
server: {
15+
ADMIN_EMAIL: z.string().email(),
16+
ADMIN_PASSWORD: z.string().min(8),
17+
ADMIN_FIRSTNAME: z.string(),
18+
ADMIN_LASTNAME: z.string(),
19+
DATABASE_URL: z.string(),
20+
},
21+
client: {},
22+
experimental__runtimeEnv: {},
23+
});
24+
25+
const dialect = new PostgresDialect({
26+
pool: new pg.Pool({
27+
connectionString: env.DATABASE_URL,
28+
}),
29+
});
30+
31+
const db = new Kysely<Database>({
32+
dialect,
33+
});
34+
35+
async function createAdminUser({
36+
email,
37+
password,
38+
firstName,
39+
lastName,
40+
}: {
41+
email: string;
42+
password: string;
43+
firstName: string;
44+
lastName: string;
45+
}) {
46+
const values = {
47+
slug: email.split("@")[0],
48+
email,
49+
firstName,
50+
lastName,
51+
passwordHash: await createPasswordHash(password),
52+
isSuperAdmin: true,
53+
};
54+
55+
return db.insertInto("users").values(values).returningAll().executeTakeFirstOrThrow();
56+
}
57+
58+
async function main() {
59+
const adminEmail = env.ADMIN_EMAIL;
60+
const adminPassword = env.ADMIN_PASSWORD;
61+
const adminFirstName = env.ADMIN_FIRSTNAME;
62+
const adminLastName = env.ADMIN_LASTNAME;
63+
64+
if (!adminEmail || !adminPassword) {
65+
throw new Error("ADMIN_EMAIL and ADMIN_PASSWORD must be set for admin initialization");
66+
}
67+
68+
try {
69+
await createAdminUser({
70+
email: adminEmail,
71+
password: adminPassword,
72+
firstName: adminFirstName,
73+
lastName: adminLastName,
74+
});
75+
console.log("✨ Admin user created successfully!");
76+
console.log(`You can now log in with:`);
77+
console.log(`${adminEmail}`);
78+
} catch (e) {
79+
if (isUniqueConstraintError(e)) {
80+
console.log("⚠️ Admin user already exists, skipping initialization");
81+
return;
82+
}
83+
throw e;
84+
}
85+
}
86+
87+
if (require.main === module) {
88+
main()
89+
.then(() => process.exit(0))
90+
.catch((e) => {
91+
console.error(e);
92+
process.exit(1);
93+
});
94+
}

self-host/.env.example

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# the default url of the platform
2+
PUBPUB_URL=http://localhost:3000 # the url of the platform
3+
# change this to eg
4+
# PUBPUB_URL=https://platform.example.com
5+
# for a production environment
6+
7+
8+
# configure these things with safe values
9+
# or the values of a remote postgres database
10+
POSTGRES_USER=my-postgres-user # change this!
11+
POSTGRES_PASSWORD=my-postgres-password # change this!
12+
POSTGRES_DB=my-postgres-db # change this! this is hard to change after the database has been created
13+
POSTGRES_HOST=db # change this to the name of the service in docker-compose.yml, or the domain of a remote postgres database if you're using that instead
14+
POSTGRES_PORT=5432 # don't forget to update the port in docker-compose.yml if you change this
15+
16+
# not needed if you're using a remote file server like AWS S3
17+
MINIO_ROOT_USER= # change this! this is the username for your file server!
18+
MINIO_ROOT_PASSWORD= # change this! this is the password for your file server!
19+
20+
ASSETS_BUCKET_NAME=assets
21+
ASSETS_UPLOAD_KEY= # change this! example: asset-user
22+
ASSETS_UPLOAD_SECRET_KEY= # change this!
23+
ASSETS_REGION=us-east-1 # leave this unchanged, unless you are hosting files on a different region on actual AWS
24+
25+
# this is the default value but you ideally should set this up more nicely using our caddy service
26+
ASSETS_STORAGE_ENDPOINT="http://localhost:9000"
27+
# you could also set this to the secured endpoint of your file server
28+
# ASSETS_STORAGE_ENDPOINT="https://example.com/assets"
29+
30+
MAILGUN_SMTP_HOST=localhost
31+
MAILGUN_SMTP_PORT=54325
32+
MAILGUN_SMTP_PASSWORD="xxx"
33+
MAILGUN_SMTP_USERNAME="xxx"
34+
35+
API_KEY="super_secret_key"
36+
37+
OTEL_SERVICE_NAME="pubpub-v7-dev" # should be shared across components but not environments
38+
HONEYCOMB_API_KEY="xxx"
39+
40+
# KYSELY_DEBUG="true"
41+
42+
GCLOUD_KEY_FILE='xxx'
43+
44+
SELF_HOST="true"

self-host/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
minio/.minio.sys

0 commit comments

Comments
 (0)