Skip to content

Commit 6cf10b4

Browse files
Authentication (#164)
1 parent 7029aa7 commit 6cf10b4

File tree

25 files changed

+638
-136
lines changed

25 files changed

+638
-136
lines changed

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"recommendations": [
33
"dbaeumer.vscode-eslint",
4-
"bradlc.vscode-tailwindcss"
4+
"bradlc.vscode-tailwindcss",
5+
"prisma.prisma"
56
]
67
}

Dockerfile

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@ RUN go mod download
1010
COPY vendor/zoekt ./
1111
RUN CGO_ENABLED=0 GOOS=linux go build -o /cmd/ ./cmd/...
1212

13+
# ------ Build Database ------
14+
FROM node-alpine AS database-builder
15+
WORKDIR /app
16+
17+
COPY package.json yarn.lock* ./
18+
COPY ./packages/db ./packages/db
19+
RUN yarn workspace @sourcebot/db install --frozen-lockfile
20+
1321
# ------ Build Web ------
1422
FROM node-alpine AS web-builder
1523
RUN apk add --no-cache libc6-compat
1624
WORKDIR /app
1725

1826
COPY package.json yarn.lock* ./
1927
COPY ./packages/web ./packages/web
28+
COPY --from=database-builder /app/node_modules ./node_modules
29+
COPY --from=database-builder /app/packages/db ./packages/db
2030

2131
# Fixes arm64 timeouts
2232
RUN yarn config set registry https://registry.npmjs.org/
@@ -27,17 +37,15 @@ ENV NEXT_TELEMETRY_DISABLED=1
2737
ARG NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED=BAKED_NEXT_PUBLIC_SOURCEBOT_TELEMETRY_DISABLED
2838
ARG NEXT_PUBLIC_SOURCEBOT_VERSION=BAKED_NEXT_PUBLIC_SOURCEBOT_VERSION
2939
ENV NEXT_PUBLIC_POSTHOG_PAPIK=BAKED_NEXT_PUBLIC_POSTHOG_PAPIK
30-
# @note: leading "/" is required for the basePath property. @see: https://nextjs.org/docs/app/api-reference/next-config-js/basePath
31-
ARG NEXT_PUBLIC_DOMAIN_SUB_PATH=/BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH
32-
RUN yarn workspace @sourcebot/web build
3340

34-
# ------ Build Database ------
35-
FROM node-alpine AS database-builder
36-
WORKDIR /app
41+
# @nocheckin: This was interfering with the the `matcher` regex in middleware.ts,
42+
# causing regular expressions parsing errors when making a request. It's unclear
43+
# why exactly this was happening, but it's likely due to a bad replacement happening
44+
# in the `sed` command.
45+
# @note: leading "/" is required for the basePath property. @see: https://nextjs.org/docs/app/api-reference/next-config-js/basePath
46+
# ARG NEXT_PUBLIC_DOMAIN_SUB_PATH=/BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH
3747

38-
COPY package.json yarn.lock* ./
39-
COPY ./packages/db ./packages/db
40-
RUN yarn workspace @sourcebot/db install --frozen-lockfile
48+
RUN yarn workspace @sourcebot/web build
4149

4250

4351
# ------ Build Backend ------

entrypoint.sh

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -107,46 +107,50 @@ echo -e "\e[34m[Info] Using config file at: '$CONFIG_PATH'.\e[0m"
107107
done
108108
}
109109

110-
111-
# Update specifically NEXT_PUBLIC_DOMAIN_SUB_PATH w/o requiring a rebuild.
112-
# Ultimately, the DOMAIN_SUB_PATH sets the `basePath` param in the next.config.mjs.
113-
# Similar to above, we pass in a `BAKED_` sentinal value into next.config.mjs at build
114-
# time. Unlike above, the `basePath` configuration is set in files other than just javascript
115-
# code (e.g., manifest files, css files, etc.), so this section has subtle differences.
110+
# @nocheckin: This was interfering with the the `matcher` regex in middleware.ts,
111+
# causing regular expressions parsing errors when making a request. It's unclear
112+
# why exactly this was happening, but it's likely due to a bad replacement happening
113+
# in the `sed` command.
116114
#
117-
# @see: https://nextjs.org/docs/app/api-reference/next-config-js/basePath
118-
# @see: https://phase.dev/blog/nextjs-public-runtime-variables/
119-
{
120-
if [ ! -z "$DOMAIN_SUB_PATH" ]; then
121-
# If the sub-path is "/", this creates problems with certain replacements. For example:
122-
# /BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH/_next/image -> //_next/image (notice the double slash...)
123-
# To get around this, we default to an empty sub-path, which is the default when no sub-path is defined.
124-
if [ "$DOMAIN_SUB_PATH" = "/" ]; then
125-
DOMAIN_SUB_PATH=""
126-
127-
# Otherwise, we need to ensure that the sub-path starts with a slash, since this is a requirement
128-
# for the basePath property. For example, assume DOMAIN_SUB_PATH=/bot, then:
129-
# /BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH/_next/image -> /bot/_next/image
130-
elif [[ ! "$DOMAIN_SUB_PATH" =~ ^/ ]]; then
131-
DOMAIN_SUB_PATH="/$DOMAIN_SUB_PATH"
132-
fi
133-
fi
134-
135-
if [ ! -z "$DOMAIN_SUB_PATH" ]; then
136-
echo -e "\e[34m[Info] DOMAIN_SUB_PATH was set to "$DOMAIN_SUB_PATH". Overriding default path.\e[0m"
137-
fi
138-
139-
# Always set NEXT_PUBLIC_DOMAIN_SUB_PATH to DOMAIN_SUB_PATH (even if it is empty!!)
140-
export NEXT_PUBLIC_DOMAIN_SUB_PATH="$DOMAIN_SUB_PATH"
141-
142-
# Iterate over _all_ files in the web directory, making substitutions for the `BAKED_` sentinal values
143-
# with their actual desired runtime value.
144-
find /app/packages/web -type f |
145-
while read file; do
146-
# @note: the leading "/" is required here as it is included at build time. See Dockerfile.
147-
sed -i "s|/BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH|${NEXT_PUBLIC_DOMAIN_SUB_PATH}|g" "$file"
148-
done
149-
}
115+
# # Update specifically NEXT_PUBLIC_DOMAIN_SUB_PATH w/o requiring a rebuild.
116+
# # Ultimately, the DOMAIN_SUB_PATH sets the `basePath` param in the next.config.mjs.
117+
# # Similar to above, we pass in a `BAKED_` sentinal value into next.config.mjs at build
118+
# # time. Unlike above, the `basePath` configuration is set in files other than just javascript
119+
# # code (e.g., manifest files, css files, etc.), so this section has subtle differences.
120+
# #
121+
# # @see: https://nextjs.org/docs/app/api-reference/next-config-js/basePath
122+
# # @see: https://phase.dev/blog/nextjs-public-runtime-variables/
123+
# {
124+
# if [ ! -z "$DOMAIN_SUB_PATH" ]; then
125+
# # If the sub-path is "/", this creates problems with certain replacements. For example:
126+
# # /BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH/_next/image -> //_next/image (notice the double slash...)
127+
# # To get around this, we default to an empty sub-path, which is the default when no sub-path is defined.
128+
# if [ "$DOMAIN_SUB_PATH" = "/" ]; then
129+
# DOMAIN_SUB_PATH=""
130+
131+
# # Otherwise, we need to ensure that the sub-path starts with a slash, since this is a requirement
132+
# # for the basePath property. For example, assume DOMAIN_SUB_PATH=/bot, then:
133+
# # /BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH/_next/image -> /bot/_next/image
134+
# elif [[ ! "$DOMAIN_SUB_PATH" =~ ^/ ]]; then
135+
# DOMAIN_SUB_PATH="/$DOMAIN_SUB_PATH"
136+
# fi
137+
# fi
138+
139+
# if [ ! -z "$DOMAIN_SUB_PATH" ]; then
140+
# echo -e "\e[34m[Info] DOMAIN_SUB_PATH was set to "$DOMAIN_SUB_PATH". Overriding default path.\e[0m"
141+
# fi
142+
143+
# # Always set NEXT_PUBLIC_DOMAIN_SUB_PATH to DOMAIN_SUB_PATH (even if it is empty!!)
144+
# export NEXT_PUBLIC_DOMAIN_SUB_PATH="$DOMAIN_SUB_PATH"
145+
146+
# # Iterate over _all_ files in the web directory, making substitutions for the `BAKED_` sentinal values
147+
# # with their actual desired runtime value.
148+
# find /app/packages/web -type f |
149+
# while read file; do
150+
# # @note: the leading "/" is required here as it is included at build time. See Dockerfile.
151+
# sed -i "s|/BAKED_NEXT_PUBLIC_DOMAIN_SUB_PATH|${NEXT_PUBLIC_DOMAIN_SUB_PATH}|g" "$file"
152+
# done
153+
# }
150154

151155

152156
# Run supervisord

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"build": "yarn workspaces run build",
88
"test": "yarn workspaces run test",
9-
"dev": "npm-run-all --print-label --parallel dev:zoekt dev:backend dev:web dev:redis",
10-
"dev:mt": "npm-run-all --print-label --parallel dev:zoekt:mt dev:backend dev:web dev:redis",
9+
"dev": "yarn workspace @sourcebot/db prisma:migrate:dev && npm-run-all --print-label --parallel dev:zoekt dev:backend dev:web dev:redis",
10+
"dev:mt": "yarn workspace @sourcebot/db prisma:migrate:dev && npm-run-all --print-label --parallel dev:zoekt:mt dev:backend dev:web dev:redis",
1111
"dev:zoekt": "export PATH=\"$PWD/bin:$PATH\" && export SRC_TENANT_ENFORCEMENT_MODE=none && zoekt-webserver -index .sourcebot/index -rpc",
1212
"dev:zoekt:mt": "export PATH=\"$PWD/bin:$PATH\" && export SRC_TENANT_ENFORCEMENT_MODE=strict && zoekt-webserver -index .sourcebot/index -rpc",
1313
"dev:backend": "yarn workspace @sourcebot/backend dev:watch",

packages/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"lowdb": "^7.0.1",
3333
"micromatch": "^4.0.8",
3434
"posthog-node": "^4.2.1",
35+
"@sourcebot/db": "^0.1.0",
3536
"simple-git": "^3.27.0",
3637
"strip-json-comments": "^5.0.1",
3738
"winston": "^3.15.0",

packages/backend/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export const syncConfig = async (configPath: string, db: PrismaClient, signal: A
105105
name: repoName,
106106
tenantId: 0, // TODO: add support for tenantId in GitLab config
107107
isFork,
108-
isArchived: project.archived,
108+
isArchived: !!project.archived,
109109
metadata: {
110110
'zoekt.web-url-type': 'gitlab',
111111
'zoekt.web-url': project.web_url,

packages/backend/src/gitlab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import micromatch from "micromatch";
33
import { createLogger } from "./logger.js";
44
import { GitLabConfig } from "./schemas/v2.js";
55
import { AppContext } from "./types.js";
6-
import { getTokenFromConfig, marshalBool, measure } from "./utils.js";
6+
import { getTokenFromConfig, measure } from "./utils.js";
77

88
const logger = createLogger("GitLab");
99
export const GITLAB_CLOUD_HOSTNAME = "gitlab.com";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
-- CreateTable
2+
CREATE TABLE "User" (
3+
"id" TEXT NOT NULL PRIMARY KEY,
4+
"name" TEXT,
5+
"email" TEXT,
6+
"emailVerified" DATETIME,
7+
"image" TEXT,
8+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
9+
"updatedAt" DATETIME NOT NULL
10+
);
11+
12+
-- CreateTable
13+
CREATE TABLE "Account" (
14+
"id" TEXT NOT NULL PRIMARY KEY,
15+
"userId" TEXT NOT NULL,
16+
"type" TEXT NOT NULL,
17+
"provider" TEXT NOT NULL,
18+
"providerAccountId" TEXT NOT NULL,
19+
"refresh_token" TEXT,
20+
"access_token" TEXT,
21+
"expires_at" INTEGER,
22+
"token_type" TEXT,
23+
"scope" TEXT,
24+
"id_token" TEXT,
25+
"session_state" TEXT,
26+
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
27+
"updatedAt" DATETIME NOT NULL,
28+
CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE
29+
);
30+
31+
-- CreateTable
32+
CREATE TABLE "VerificationToken" (
33+
"identifier" TEXT NOT NULL,
34+
"token" TEXT NOT NULL,
35+
"expires" DATETIME NOT NULL
36+
);
37+
38+
-- CreateIndex
39+
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
40+
41+
-- CreateIndex
42+
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");
43+
44+
-- CreateIndex
45+
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");

packages/db/prisma/schema.prisma

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,48 @@ model Repo {
4141
4242
@@unique([external_id, external_codeHostUrl])
4343
}
44+
45+
// @see : https://authjs.dev/concepts/database-models#user
46+
model User {
47+
id String @id @default(cuid())
48+
name String?
49+
email String? @unique
50+
emailVerified DateTime?
51+
image String?
52+
accounts Account[]
53+
54+
createdAt DateTime @default(now())
55+
updatedAt DateTime @updatedAt
56+
}
57+
58+
// @see : https://authjs.dev/concepts/database-models#account
59+
model Account {
60+
id String @id @default(cuid())
61+
userId String
62+
type String
63+
provider String
64+
providerAccountId String
65+
refresh_token String?
66+
access_token String?
67+
expires_at Int?
68+
token_type String?
69+
scope String?
70+
id_token String?
71+
session_state String?
72+
73+
createdAt DateTime @default(now())
74+
updatedAt DateTime @updatedAt
75+
76+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
77+
78+
@@unique([provider, providerAccountId])
79+
}
80+
81+
// @see : https://authjs.dev/concepts/database-models#verificationtoken
82+
model VerificationToken {
83+
identifier String
84+
token String
85+
expires DateTime
86+
87+
@@unique([identifier, token])
88+
}

packages/web/next.config.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ const nextConfig = {
2222
// This is required to support PostHog trailing slash API requests
2323
skipTrailingSlashRedirect: true,
2424

25+
// @nocheckin: This was interfering with the the `matcher` regex in middleware.ts,
26+
// causing regular expressions parsing errors when making a request. It's unclear
27+
// why exactly this was happening, but it's likely due to a bad replacement happening
28+
// in the `sed` command.
2529
// @note: this is evaluated at build time.
26-
...(process.env.NEXT_PUBLIC_DOMAIN_SUB_PATH ? {
27-
basePath: process.env.NEXT_PUBLIC_DOMAIN_SUB_PATH,
28-
} : {})
30+
// ...(process.env.NEXT_PUBLIC_DOMAIN_SUB_PATH ? {
31+
// basePath: process.env.NEXT_PUBLIC_DOMAIN_SUB_PATH,
32+
// } : {})
2933
};
3034

3135
export default nextConfig;

packages/web/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"test": "vitest"
1111
},
1212
"dependencies": {
13+
"@auth/prisma-adapter": "^2.7.4",
1314
"@codemirror/commands": "^6.6.0",
1415
"@codemirror/lang-cpp": "^6.0.2",
1516
"@codemirror/lang-css": "^6.3.0",
@@ -39,6 +40,7 @@
3940
"@hookform/resolvers": "^3.9.0",
4041
"@iconify/react": "^5.1.0",
4142
"@iizukak/codemirror-lang-wgsl": "^0.3.0",
43+
"@radix-ui/react-avatar": "^1.1.2",
4244
"@radix-ui/react-dropdown-menu": "^2.1.1",
4345
"@radix-ui/react-icons": "^1.3.0",
4446
"@radix-ui/react-label": "^2.1.0",
@@ -89,6 +91,7 @@
8991
"http-status-codes": "^2.3.0",
9092
"lucide-react": "^0.435.0",
9193
"next": "14.2.21",
94+
"next-auth": "^5.0.0-beta.25",
9295
"next-themes": "^0.3.0",
9396
"posthog-js": "^1.161.5",
9497
"pretty-bytes": "^6.1.1",
@@ -119,9 +122,10 @@
119122
"jsdom": "^25.0.1",
120123
"npm-run-all": "^4.1.5",
121124
"postcss": "^8",
125+
"@sourcebot/db": "^0.1.0",
122126
"tailwindcss": "^3.4.1",
123127
"typescript": "^5",
124128
"vite-tsconfig-paths": "^5.1.3",
125129
"vitest": "^2.1.5"
126130
}
127-
}
131+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { handlers } from "@/auth";
2+
export const { GET, POST } = handlers;

packages/web/src/app/api/(server)/search/route.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import { NextRequest } from "next/server";
88

99
export const POST = async (request: NextRequest) => {
1010
const body = await request.json();
11-
const tenantId = await request.headers.get("X-Tenant-ID");
11+
const tenantId = request.headers.get("X-Tenant-ID");
1212

1313
console.log(`Search request received. Tenant ID: ${tenantId}`);
1414

1515
const parsed = await searchRequestSchema.safeParseAsync({
1616
...body,
17-
...(tenantId && { tenantId: parseInt(tenantId) }),
17+
...(tenantId ? {
18+
tenantId: parseInt(tenantId)
19+
} : {}),
1820
});
1921
if (!parsed.success) {
2022
return serviceErrorResponse(

0 commit comments

Comments
 (0)