Skip to content

Commit 6c4c97d

Browse files
committed
runs intial formatter
1 parent 3b70c20 commit 6c4c97d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1020
-996
lines changed

.prettierrc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"plugins": ["prettier-plugin-tailwindcss"],
3-
"tailwindConfig": "./apps/web/tailwind.config.ts",
42
"semi": true,
53
"singleQuote": false,
64
"jsxSingleQuote": false,

apps/api/src/env.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
import { createEnv } from "@t3-oss/env-core";
22
import { z } from "zod";
33
export const env = createEnv({
4-
server: {
5-
CLOUDFLARE_ACCOUNT_ID: z.string({
6-
description:
7-
"Account ID for the Cloudflare account. Note that this ID should be the same one the bucket is hosted in.",
8-
}),
9-
FALLBACK_WEB_URL:z.string({
10-
description:"The URL of the frontend. DO NOT ADD A TRAILING SLASH"
11-
}).url(),
12-
R2_ACCESS_KEY_ID: z.string(),
13-
R2_SECRET_ACCESS_KEY: z.string(),
14-
BETTER_AUTH_SECRET: z.string(),
15-
// TODO: add these back once the oauth stuff is implemented.
16-
// GOOGLE_CLIENT_ID: z.string(),
17-
// GOOGLE_CLIENT_SECRET: z.string(),
18-
// DISCORD_CLIENT_ID: z.string(),
19-
// DISCORD_CLIENT_SECRET: z.string(),
20-
// GITHUB_CLIENT_ID: z.string(),
21-
// GITHUB_CLIENT_SECRET: z.string(),
22-
// LINEAR_CLIENT_ID: z.string(),
4+
server: {
5+
CLOUDFLARE_ACCOUNT_ID: z.string({
6+
description:
7+
"Account ID for the Cloudflare account. Note that this ID should be the same one the bucket is hosted in.",
8+
}),
9+
FALLBACK_WEB_URL: z
10+
.string({
11+
description:
12+
"The URL of the frontend. DO NOT ADD A TRAILING SLASH",
13+
})
14+
.url(),
15+
R2_ACCESS_KEY_ID: z.string(),
16+
R2_SECRET_ACCESS_KEY: z.string(),
17+
BETTER_AUTH_SECRET: z.string(),
18+
// TODO: add these back once the oauth stuff is implemented.
19+
// GOOGLE_CLIENT_ID: z.string(),
20+
// GOOGLE_CLIENT_SECRET: z.string(),
21+
// DISCORD_CLIENT_ID: z.string(),
22+
// DISCORD_CLIENT_SECRET: z.string(),
23+
// GITHUB_CLIENT_ID: z.string(),
24+
// GITHUB_CLIENT_SECRET: z.string(),
25+
// LINEAR_CLIENT_ID: z.string(),
2326
// LINEAR_CLIENT_SECRET: z.string(),
24-
},
25-
onValidationError: (issues) => {
26-
console.log("all process variables:", process.env);
27-
console.error("❌ Invalid environment variables:", issues);
28-
throw new Error("Invalid environment variables");
29-
},
30-
runtimeEnv: process.env,
31-
emptyStringAsUndefined: true,
32-
});
27+
},
28+
onValidationError: (issues) => {
29+
console.log("all process variables:", process.env);
30+
console.error("❌ Invalid environment variables:", issues);
31+
throw new Error("Invalid environment variables");
32+
},
33+
runtimeEnv: process.env,
34+
emptyStringAsUndefined: true,
35+
});

apps/api/src/index.ts

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,56 @@ import {
22
type ScheduledController,
33
type ExecutionContext,
44
} from "@cloudflare/workers-types";
5-
import { authHandler, backupHandler, userhandler, logHandler, healthHandler } from "./routes";
5+
import {
6+
authHandler,
7+
backupHandler,
8+
userhandler,
9+
logHandler,
10+
healthHandler,
11+
} from "./routes";
612
import { generalCorsPolicy, betterAuthCorsPolicy } from "./lib/functions/cors";
713
import { HonoBetterAuth } from "./lib/functions";
8-
import { setUserSessionContextMiddleware, authenticatedMiddleware } from "./lib/functions/middleware";
9-
14+
import {
15+
setUserSessionContextMiddleware,
16+
authenticatedMiddleware,
17+
} from "./lib/functions/middleware";
1018

1119
interface Env {}
1220

13-
// api stuff
21+
// api stuff
1422
export const api = HonoBetterAuth()
15-
.use(
16-
"*",
17-
generalCorsPolicy, // see if we can get rid of this one maybe later?
18-
betterAuthCorsPolicy,
19-
async (c, next) => setUserSessionContextMiddleware(c,next),
20-
async (c, next) => authenticatedMiddleware(c,next)
21-
)
22-
.route("/health", healthHandler)
23-
.route("/log", logHandler)
24-
.route("/backup", backupHandler)
25-
.route("/user", userhandler);
26-
23+
.use(
24+
"*",
25+
generalCorsPolicy, // see if we can get rid of this one maybe later?
26+
betterAuthCorsPolicy,
27+
async (c, next) => setUserSessionContextMiddleware(c, next),
28+
async (c, next) => authenticatedMiddleware(c, next),
29+
)
30+
.route("/health", healthHandler)
31+
.route("/log", logHandler)
32+
.route("/backup", backupHandler)
33+
.route("/user", userhandler)
34+
.route("/api/auth/*", authHandler); //TODO: Ensure that this is the correct route segment to start requests from.
35+
36+
///
2737

2838
// cron stuff
2939
/**
3040
* The basic logic for running a cron job in Cloudflare Workers. Will be updated to be more specific later.
3141
*/
3242
const cron = async (
33-
controller: ScheduledController,
34-
_: Env,
35-
ctx: ExecutionContext
43+
controller: ScheduledController,
44+
_: Env,
45+
ctx: ExecutionContext,
3646
) => {
3747
// NOTE: controller.cron is what we will use to check what jobs need to be running
38-
// ctx.waitUntil(doBackup());
48+
// ctx.waitUntil(doBackup());
3949
};
4050

4151
export default {
4252
fetch: api.fetch,
4353
scheduled: cron,
4454
};
4555

46-
4756
// Special type only exported for the web client
48-
export type ApiType = typeof api;
57+
export type ApiType = typeof api;

apps/api/src/lib/auth.ts

Lines changed: 91 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,97 @@
11
import { betterAuth } from "better-auth";
22
import { drizzleAdapter } from "better-auth/adapters/drizzle";
33
import { db } from "db"; // your drizzle instance
4-
import { env } from "../env";
5-
import {APP_NAME, AUTH_CONFIG} from "shared/constants"
4+
import { APP_NAME, AUTH_CONFIG } from "shared/constants";
65

76
export const auth = betterAuth({
8-
database: drizzleAdapter(db, {
9-
provider: "sqlite",
10-
debugLogs: true,
11-
}),
12-
databaseHooks: {
13-
user: {
14-
create: {
15-
// used in order to break up the first and last name into separate fields
16-
before: async (user) => {
17-
// split the name into first and last name (name object is mapped to the first name by the config)
18-
const [firstName, ...rest] = user.name.split(" ");
19-
const lastName = rest.join(" ");
20-
return {
21-
data: { ...user, firstName, lastName },
22-
};
23-
},
24-
},
25-
},
26-
},
27-
user: {
28-
// this maps the default "name" field to the "firstName" field in the database
29-
fields: {
30-
name: "firstName",
31-
},
32-
// this declares the extra fields that are not in the default user schema that better auth creates, but are in the database
33-
additionalFields: {
34-
firstName: {
35-
type: "string",
36-
defaultValue: "",
37-
},
38-
lastName: {
39-
type: "string",
40-
defaultValue: "",
41-
},
42-
lastSeen: {
43-
type: "date",
44-
required: true,
45-
defaultValue: Date.now(),
46-
input: false,
47-
},
48-
// role: {
49-
// type: "string",
50-
// defaultValue: "user",
51-
// validator: {
52-
// input: z.enum(["user", "admin"]),
53-
// output: z.enum(["user", "admin"]),
54-
// },
55-
// },
56-
},
57-
},
58-
advanced: {
59-
cookiePrefix: APP_NAME,
60-
},
61-
emailAndPassword: {
62-
enabled: AUTH_CONFIG.emailAndPassword.enabled,
63-
minPasswordLength: AUTH_CONFIG.emailAndPassword.minPasswordLength,
64-
maxPasswordLength: AUTH_CONFIG.emailAndPassword.maxPasswordLength,
65-
},
66-
// TODO: Reference the following link to see if it is easier to have the social provider's returned values map to first and last name instead
67-
// https://www.better-auth.com/docs/concepts/database#extending-core-schema:~:text=Example%3A%20Mapping%20Profile%20to%20User%20For%20firstName%20and%20lastName
68-
// socialProviders: {
69-
// google: {
70-
// clientId: env.GOOGLE_CLIENT_ID,
71-
// clientSecret: env.GOOGLE_CLIENT_SECRET,
72-
// },
73-
// discord: {
74-
// clientId: env.DISCORD_CLIENT_ID,
75-
// clientSecret: env.DISCORD_CLIENT_SECRET,
76-
// },
77-
// github: {
78-
// clientId: env.GITHUB_CLIENT_ID,
79-
// clientSecret: env.GITHUB_CLIENT_SECRET,
80-
// },
81-
// linear: {
82-
// clientId: env.LINEAR_CLIENT_ID,
83-
// clientSecret: env.LINEAR_CLIENT_SECRET,
84-
// },
85-
// },
86-
rateLimit: {
87-
window: 10, // time window in seconds
88-
max: 100, // max requests in the window
89-
},
90-
session: {
91-
expiresIn: 60 * 60 * 24 * 7, // 7 days
92-
updateAge: 60 * 60 * 24, // 1 day (every 1 day the session expiration is updated)
93-
cookieCache: {
94-
enabled: true,
95-
maxAge: 5 * 60,
96-
},
97-
},
7+
database: drizzleAdapter(db, {
8+
provider: "sqlite",
9+
debugLogs: true,
10+
}),
11+
databaseHooks: {
12+
user: {
13+
create: {
14+
// used in order to break up the first and last name into separate fields
15+
before: async (user) => {
16+
// split the name into first and last name (name object is mapped to the first name by the config)
17+
const [firstName, ...rest] = user.name.split(" ");
18+
const lastName = rest.join(" ");
19+
return {
20+
data: { ...user, firstName, lastName },
21+
};
22+
},
23+
},
24+
},
25+
},
26+
user: {
27+
// this maps the default "name" field to the "firstName" field in the database
28+
fields: {
29+
name: "firstName",
30+
},
31+
// this declares the extra fields that are not in the default user schema that better auth creates, but are in the database
32+
additionalFields: {
33+
firstName: {
34+
type: "string",
35+
defaultValue: "",
36+
},
37+
lastName: {
38+
type: "string",
39+
defaultValue: "",
40+
},
41+
lastSeen: {
42+
type: "date",
43+
required: true,
44+
defaultValue: Date.now(),
45+
input: false,
46+
},
47+
// role: {
48+
// type: "string",
49+
// defaultValue: "user",
50+
// validator: {
51+
// input: z.enum(["user", "admin"]),
52+
// output: z.enum(["user", "admin"]),
53+
// },
54+
// },
55+
},
56+
},
57+
advanced: {
58+
cookiePrefix: APP_NAME,
59+
},
60+
emailAndPassword: {
61+
enabled: AUTH_CONFIG.emailAndPassword.enabled,
62+
minPasswordLength: AUTH_CONFIG.emailAndPassword.minPasswordLength,
63+
maxPasswordLength: AUTH_CONFIG.emailAndPassword.maxPasswordLength,
64+
},
65+
// TODO: Reference the following link to see if it is easier to have the social provider's returned values map to first and last name instead
66+
// https://www.better-auth.com/docs/concepts/database#extending-core-schema:~:text=Example%3A%20Mapping%20Profile%20to%20User%20For%20firstName%20and%20lastName
67+
// socialProviders: {
68+
// google: {
69+
// clientId: env.GOOGLE_CLIENT_ID,
70+
// clientSecret: env.GOOGLE_CLIENT_SECRET,
71+
// },
72+
// discord: {
73+
// clientId: env.DISCORD_CLIENT_ID,
74+
// clientSecret: env.DISCORD_CLIENT_SECRET,
75+
// },
76+
// github: {
77+
// clientId: env.GITHUB_CLIENT_ID,
78+
// clientSecret: env.GITHUB_CLIENT_SECRET,
79+
// },
80+
// linear: {
81+
// clientId: env.LINEAR_CLIENT_ID,
82+
// clientSecret: env.LINEAR_CLIENT_SECRET,
83+
// },
84+
// },
85+
rateLimit: {
86+
window: 10, // time window in seconds
87+
max: 100, // max requests in the window
88+
},
89+
session: {
90+
expiresIn: 60 * 60 * 24 * 7, // 7 days
91+
updateAge: 60 * 60 * 24, // 1 day (every 1 day the session expiration is updated)
92+
cookieCache: {
93+
enabled: true,
94+
maxAge: 5 * 60,
95+
},
96+
},
9897
});

apps/api/src/lib/functions/cors.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
import { cors } from "hono/cors"
2-
import {env} from "../../env"
1+
import { cors } from "hono/cors";
2+
import { env } from "../../env";
33

44
/**
55
* General CORS policy for the API. Will run on every request, but others can be specified for individual routes.
66
*/
7-
export const generalCorsPolicy = cors({
8-
origin: env.FALLBACK_WEB_URL,
9-
allowHeaders: [
10-
"Content-Type",
11-
"Authorization",
12-
"Access-Control-Allow-Origin",
13-
],
14-
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
15-
exposeHeaders: ["Content-Length"],
16-
maxAge: 600,
17-
credentials: true,
18-
});
19-
7+
export const generalCorsPolicy = cors({
8+
origin: env.FALLBACK_WEB_URL,
9+
allowHeaders: [
10+
"Content-Type",
11+
"Authorization",
12+
"Access-Control-Allow-Origin",
13+
],
14+
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
15+
exposeHeaders: ["Content-Length"],
16+
maxAge: 600,
17+
credentials: true,
18+
});
19+
2020
/**
2121
* CORS policy specifically for the Better Auth routes.
2222
*/
2323
export const betterAuthCorsPolicy = cors({
24-
origin: env.FALLBACK_WEB_URL,
25-
allowHeaders: ["Content-Type", "Authorization"],
26-
allowMethods: ["POST", "GET", "OPTIONS"],
27-
exposeHeaders: ["Content-Length"],
28-
maxAge: 600,
29-
credentials: true,
30-
});
24+
origin: env.FALLBACK_WEB_URL,
25+
allowHeaders: ["Content-Type", "Authorization"],
26+
allowMethods: ["POST", "GET", "OPTIONS"],
27+
exposeHeaders: ["Content-Length"],
28+
maxAge: 600,
29+
credentials: true,
30+
});

0 commit comments

Comments
 (0)