Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: sessionToken definition

Richie McIlroy87e476d2eecc
diff --git a/apps/web/app/api/desktop/session/authenticate/route.ts b/apps/web/app/api/desktop/session/authenticate/route.ts
index 9edbd763d..36b9b2b7d 100644
--- a/apps/web/app/api/desktop/session/authenticate/route.ts
+++ b/apps/web/app/api/desktop/session/authenticate/route.ts
@@ -24,7 +24,9 @@ export async function GET(req: NextRequest) {
const response = NextResponse.next();
response.cookies.set({
- name: "next-auth.session-token",
+ name: `${
+ process.env.NEXT_PUBLIC_ENVIRONMENT === "development" ? "__Secure-" : ""
+ }next-auth.session-token`,
value: token,
path: "/",
});
diff --git a/apps/web/app/api/desktop/session/request/route.ts b/apps/web/app/api/desktop/session/request/route.ts
index 3cc7c3599..fa03e24d3 100644
--- a/apps/web/app/api/desktop/session/request/route.ts
+++ b/apps/web/app/api/desktop/session/request/route.ts
@@ -15,7 +15,7 @@ export async function GET(req: NextRequest) {
if (!token) {
return Response.redirect(
- `${process.env.NEXT_PUBLIC_URL}/login?next=api/desktop/session/request?port=${port}`
+ `${process.env.NEXT_PUBLIC_URL}/login?next=${process.env.NEXT_PUBLIC_URL}/api/desktop/session/request?port=${port}`
);
}
diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts
index bbac8932e..04cafc299 100644
--- a/apps/web/middleware.ts
+++ b/apps/web/middleware.ts
@@ -1,10 +1,16 @@
import { NextRequest, NextResponse } from "next/server";
+const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;
+
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.includes("/api/desktop/")) {
const token = request.headers.get("authorization")?.split(" ")[1];
- const cookieIsSet = request.cookies.get("next-auth.session-token");
+ const cookieIsSet = request.cookies.get(
+ `${
+ process.env.NEXT_PUBLIC_ENVIRONMENT === "development" ? "__Secure-" : ""
+ }next-auth.session-token`
+ );
if (cookieIsSet) {
console.log("Cookie is set");
@@ -14,9 +20,13 @@ export function middleware(request: NextRequest) {
if (token && !cookieIsSet) {
const response = NextResponse.next();
response.cookies.set({
- name: "next-auth.session-token",
+ name: `${VERCEL_DEPLOYMENT ? "__Secure-" : ""}next-auth.session-token`,
value: token,
path: "/",
+ sameSite: "lax",
+ secure: VERCEL_DEPLOYMENT,
+ httpOnly: true,
+ domain: VERCEL_DEPLOYMENT ? ".cap.so" : undefined,
});
return response;
}
diff --git a/packages/database/auth/auth-options.ts b/packages/database/auth/auth-options.ts
index 1071b06c1..97fcd36b0 100644
--- a/packages/database/auth/auth-options.ts
+++ b/packages/database/auth/auth-options.ts
@@ -7,6 +7,12 @@ import type { NextAuthOptions } from "next-auth";
import { sendEmail } from "../emails/config";
import { LoginLink } from "../emails/login-link";
+const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;
+
+export const config = {
+ maxDuration: 120,
+};
+
export const authOptions: NextAuthOptions = {
adapter: DrizzleAdapter(db),
debug: true,
@@ -28,6 +34,18 @@ export const authOptions: NextAuthOptions = {
},
}),
],
+ cookies: {
+ sessionToken: {
+ name: `${VERCEL_DEPLOYMENT ? "__Secure-" : ""}next-auth.session-token`,
+ options: {
+ httpOnly: true,
+ sameSite: "lax",
+ path: "/",
+ domain: VERCEL_DEPLOYMENT ? ".cap.so" : undefined,
+ secure: VERCEL_DEPLOYMENT,
+ },
+ },
+ },
callbacks: {
async session({ token, session }) {
if (token) {