diff --git a/bruno/Auth/verify-user.bru b/bruno/Auth/verify-user.bru new file mode 100644 index 00000000..80bb8457 --- /dev/null +++ b/bruno/Auth/verify-user.bru @@ -0,0 +1,15 @@ +meta { + name: verify-user + type: http + seq: 4 +} + +get { + url: http://localhost:3000/badger/verify-user?sessionId=asdf + body: none + auth: none +} + +params:query { + sessionId: asdf +} diff --git a/bruno/Traefik/traefik-config.bru b/bruno/Traefik/traefik-config.bru new file mode 100644 index 00000000..e0d584b8 --- /dev/null +++ b/bruno/Traefik/traefik-config.bru @@ -0,0 +1,11 @@ +meta { + name: traefik-config + type: http + seq: 1 +} + +get { + url: http://localhost:3000/api/v1/traefik-config + body: none + auth: none +} diff --git a/server/auth/index.ts b/server/auth/index.ts index de648e89..244ebeeb 100644 --- a/server/auth/index.ts +++ b/server/auth/index.ts @@ -27,9 +27,10 @@ export const lucia = new Lucia(adapter, { name: "session", expires: false, attributes: { - secure: environment.ENVIRONMENT === "prod", - sameSite: "strict", - // domain: "example.com" + // secure: environment.ENVIRONMENT === "prod", + // sameSite: "strict", + secure: false, + domain: ".testing123.io" }, }, sessionExpiresIn: new TimeSpan(2, "w"), diff --git a/server/index.ts b/server/index.ts index 5b008f51..71292f26 100644 --- a/server/index.ts +++ b/server/index.ts @@ -23,12 +23,11 @@ const handle = app.getRequestHandler(); const externalPort = environment.EXTERNAL_PORT; const internalPort = environment.INTERNAL_PORT; -app.prepare().then(() => { - +app.prepare().then(() => { - // External server const externalServer = express(); + externalServer.set("trust proxy", 1); // externalServer.use(helmet()); // Disabled because causes issues with Next.js externalServer.use(cors()); diff --git a/server/routers/auth/login.ts b/server/routers/auth/login.ts index 60f70b55..e8500288 100644 --- a/server/routers/auth/login.ts +++ b/server/routers/auth/login.ts @@ -10,6 +10,7 @@ import createHttpError from "http-errors"; import { z } from "zod"; import { fromError } from "zod-validation-error"; import { verifyTotpCode } from "@server/auth/2fa"; +import logger from "@server/logger"; export const loginBodySchema = z.object({ email: z.string().email(), @@ -116,9 +117,11 @@ export async function login( } const session = await lucia.createSession(existingUser.id, {}); + const cookie = lucia.createSessionCookie(session.id).serialize(); + logger.debug("Session cookie", JSON.stringify(cookie, null, 2)); res.appendHeader( "Set-Cookie", - lucia.createSessionCookie(session.id).serialize(), + cookie ); if (!existingUser.emailVerified) { diff --git a/server/routers/badger/verifyUser.ts b/server/routers/badger/verifyUser.ts index 15d0c9ad..b4fd686b 100644 --- a/server/routers/badger/verifyUser.ts +++ b/server/routers/badger/verifyUser.ts @@ -5,6 +5,7 @@ import createHttpError from "http-errors"; import { z } from "zod"; import { fromError } from "zod-validation-error"; import { response } from "@server/utils/response"; +import logger from "@server/logger"; export const verifyUserBody = z.object({ sessionId: z.string(), @@ -23,6 +24,8 @@ export async function verifyUser( ): Promise { const parsedBody = verifyUserBody.safeParse(req.query); + logger.debug("Parsed body", parsedBody); + if (!parsedBody.success) { return next( createHttpError( @@ -37,6 +40,9 @@ export async function verifyUser( try { const { session, user } = await lucia.validateSession(sessionId); + logger.debug("Session", session); + logger.debug("User", user); + if (!session || !user) { return next( createHttpError(HttpCode.UNAUTHORIZED, "Invalid session"), diff --git a/server/routers/internal.ts b/server/routers/internal.ts index dfcce4eb..08bd2a21 100644 --- a/server/routers/internal.ts +++ b/server/routers/internal.ts @@ -24,6 +24,6 @@ gerbilRouter.post("/receive-bandwidth", gerbil.receiveBandwidth); const badgerRouter = Router(); internalRouter.use("/badger", badgerRouter); -internalRouter.get("/verify-user", badger.verifyUser) +badgerRouter.get("/verify-user", badger.verifyUser) export default internalRouter; diff --git a/server/routers/traefik/getTraefikConfig.ts b/server/routers/traefik/getTraefikConfig.ts index bae3122e..5d1b2171 100644 --- a/server/routers/traefik/getTraefikConfig.ts +++ b/server/routers/traefik/getTraefikConfig.ts @@ -6,6 +6,7 @@ import { and, like, eq } from "drizzle-orm"; import logger from "@server/logger"; import HttpCode from "@server/types/HttpCode"; import env from "@server/environment"; +import environment from "@server/environment"; export async function traefikConfigProvider(_: Request, res: Response) { try { @@ -31,14 +32,35 @@ export function buildTraefikConfig( } const http: DynamicTraefikConfig["http"] = { - routers: {}, - services: {}, + routers: { + "themainwebpage": { + "entryPoints": [ + "http" + ], + "middlewares": [ + ], + "service": "service-themainwebpage", + "rule": "Host(`testing123.io`)" + }, + }, + services: { + "service-themainwebpage": { + "loadBalancer": { + "servers": [ + { + "url": `http://${environment.APP_NAME.toLowerCase()}:3000` + } + ] + } + }, + }, middlewares: { [middlewareName]: { plugin: { [middlewareName]: { - apiBaseUrl: "http://localhost:3001/api/v1", - appBaseUrl: env.BASE_URL, + apiBaseUrl: `http://${environment.APP_NAME.toLowerCase()}:3001/api/v1`, + // appBaseUrl: env.BASE_URL, + appBaseUrl: "http://testing123.io:8081", }, }, }, diff --git a/src/api/index.ts b/src/api/index.ts index 42ece4ef..aa7fa88e 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -2,8 +2,17 @@ import axios from "axios"; // const baseURL = `${window.location.protocol}//${window.location.host}/api/v1`; + export const api = axios.create({ - baseURL: "http://localhost:3000/api/v1", + baseURL: "http://testing123.io:8081/api/v1", + timeout: 10000, + headers: { + "Content-Type": "application/json", + }, +}); + +export const internal = axios.create({ + baseURL: "http://pangolin:3000/api/v1", timeout: 10000, headers: { "Content-Type": "application/json", diff --git a/src/lib/verifySession.ts b/src/lib/verifySession.ts index 5cf86491..f6914a14 100644 --- a/src/lib/verifySession.ts +++ b/src/lib/verifySession.ts @@ -1,11 +1,11 @@ -import api from "@app/api"; +import { internal } from "@app/api"; import { cookies } from "next/headers"; export async function verifySession() { const sessionId = cookies().get("session")?.value ?? null; try { - await api.get("/user", { + await internal.get("/user", { headers: { Cookie: `session=${sessionId}` }