Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: Desktop app auth flow

Richie McIlroy50cd3215c2e6
diff --git a/apps/desktop/src/components/windows/Camera.tsx b/apps/desktop/src/components/windows/Camera.tsx
index 0a278d85e..65aaf4bf7 100644
--- a/apps/desktop/src/components/windows/Camera.tsx
+++ b/apps/desktop/src/components/windows/Camera.tsx
@@ -199,40 +199,42 @@ export const Camera = () => {
</svg>
</div>
)}
- <div className="opacity-0 group-hover:opacity-100 absolute bottom-3 left-1/2 transform -translate-x-1/2 bg-gray-800 rounded-xl z-20 grid grid-cols-4 overflow-hidden">
- <div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
- <button
- onClick={() => {
- closeWindow();
- }}
- >
+ <div className="opacity-0 group-hover:opacity-100 absolute top-3 left-1/2 transform -translate-x-1/2 bg-gray-800 rounded-xl z-20 grid grid-cols-4 overflow-hidden">
+ <div
+ onClick={() => {
+ closeWindow();
+ }}
+ className="h-full flex items-center justify-center p-2 hover:bg-gray-900"
+ >
+ <div>
<CloseX className="w-5 h-5 stroke-gray-200" />
- </button>
+ </div>
</div>
- <div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
- <button
- onClick={async () => {
- await setWindowSize("sm");
- }}
- className="w-2 h-2 m-0 p-0 bg-gray-200 rounded-full"
- ></button>
+ <div
+ onClick={async () => {
+ await setWindowSize("sm");
+ }}
+ className="h-full flex items-center justify-center p-2 hover:bg-gray-900"
+ >
+ <span className="w-1 h-1 m-0 p-0 bg-gray-200 rounded-full"></span>
</div>
- <div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
- <button
- onClick={async () => {
- await setWindowSize("lg");
- }}
- className="w-4 h-4 bg-gray-200 rounded-full"
- ></button>
+ <div
+ onClick={async () => {
+ await setWindowSize("lg");
+ }}
+ className="h-full flex items-center justify-center p-2 hover:bg-gray-900"
+ >
+ <span className="w-3 h-3 bg-gray-200 rounded-full"></span>
</div>
- <div className="h-full flex items-center justify-center p-2 hover:bg-gray-900">
- <button
- onClick={() => {
- toggleBackgroundBlur();
- }}
- >
+ <div
+ onClick={() => {
+ toggleBackgroundBlur();
+ }}
+ className="h-full flex items-center justify-center p-2 hover:bg-gray-900"
+ >
+ <div>
<Focus className="w-5 h-5 stroke-gray-200" />
- </button>
+ </div>
</div>
</div>
<canvas
diff --git a/apps/desktop/src/components/windows/inner/Recorder.tsx b/apps/desktop/src/components/windows/inner/Recorder.tsx
index b66f10819..d5eaff086 100644
--- a/apps/desktop/src/components/windows/inner/Recorder.tsx
+++ b/apps/desktop/src/components/windows/inner/Recorder.tsx
@@ -16,6 +16,7 @@ import { getSelectedVideoProperties } from "@/utils/recording/utils";
import { getLatestVideoId, saveLatestVideoId } from "@/utils/database/utils";
import { openLinkInBrowser } from "@/utils/helpers";
import toast, { Toaster } from "react-hot-toast";
+import { authFetch } from "@/utils/auth/helpers";
export const Recorder = () => {
const {
@@ -77,11 +78,16 @@ export const Recorder = () => {
// };
const prepareVideoData = async () => {
- const res = await fetch(
+ const session = JSON.parse(localStorage.getItem("session"));
+ const token = session?.token;
+ const res = await authFetch(
`${process.env.NEXT_PUBLIC_URL}/api/desktop/video/create`,
{
credentials: "include",
cache: "no-store",
+ headers: {
+ Authorization: `Bearer ${token}`,
+ },
}
);
diff --git a/apps/desktop/src/utils/auth/callback.template.ts b/apps/desktop/src/utils/auth/callback.template.ts
index 9de85f695..d9d884d2c 100644
--- a/apps/desktop/src/utils/auth/callback.template.ts
+++ b/apps/desktop/src/utils/auth/callback.template.ts
@@ -11,15 +11,23 @@ export default `
<title>My Notes</title>
<style>
body {
+ width: 100%;
+ height: 100%;
+ display: flex;
+ align-items: center;
+ justify-content: center;
font-family: 'Open Sans', sans-serif;
- margin: auto;
- max-width: 640px;
+ margin: 0;
+ padding: 0;
text-align: center;
}
</style>
</head>
<body>
- <h1>You are now signed in. Please re-open the Cap desktop app to continue.</h1>
+ <div style="padding: 30px 0; width: 100%; max-width: 400px; margin: 0 auto; ">
+ <svg xmlns="http://www.w3.org/2000/svg" style="width: 100px; height: auto; margin-bottom: 10px;" viewBox="0 0 719 269"><path fill="#0A486B" d="M474.954 156.466c-3.122 16.278-10.217 29.23-21.286 38.856-10.927 9.484-25.118 14.226-42.572 14.226-14.333 0-26.892-3.397-37.677-10.192-10.643-6.936-18.873-16.349-24.692-28.24-5.818-11.89-8.727-25.267-8.727-40.13 0-14.863 2.838-28.31 8.514-40.342 5.819-12.032 14.12-21.445 24.905-28.24C384.204 55.468 396.763 52 411.096 52c16.603 0 30.368 4.53 41.295 13.589 10.927 9.059 18.164 21.304 21.712 36.733l-27.885 1.486c-2.129-8.776-6.244-15.641-12.346-20.596-6.102-5.096-13.765-7.644-22.989-7.644-9.65 0-17.739 2.407-24.266 7.22-6.386 4.812-11.14 11.465-14.262 19.959-3.122 8.351-4.683 17.764-4.683 28.239 0 10.475 1.561 19.818 4.683 28.028 3.122 8.21 7.876 14.792 14.262 19.746 6.527 4.813 14.616 7.219 24.266 7.219 9.65 0 17.526-2.76 23.628-8.28 6.243-5.662 10.359-13.236 12.346-22.72l28.097 1.487zM486.826 127.801c2.271-11.607 7.734-20.596 16.391-26.965 8.656-6.512 19.796-9.768 33.419-9.768 16.177 0 28.452 4.106 36.825 12.316 8.372 8.21 12.559 20.171 12.559 35.883v39.281c0 2.973.567 5.096 1.703 6.37 1.277 1.132 3.051 1.698 5.321 1.698h4.257v19.535l-6.598.212h-.852c-3.831.142-7.734-.142-11.707-.849-3.832-.708-7.237-2.336-10.217-4.884-2.981-2.548-4.683-6.441-5.109-11.678-2.838 5.804-7.45 10.546-13.836 14.226-6.386 3.68-14.191 5.521-23.415 5.521-11.636 0-21.357-2.902-29.162-8.706-7.663-5.945-11.494-13.73-11.494-23.356 0-7.078 1.631-12.811 4.895-17.199 3.406-4.388 8.089-7.856 14.049-10.404 6.102-2.548 14.049-4.742 23.841-6.582l31.716-6.37c-.142-8.635-2.058-15.004-5.747-19.109-3.69-4.247-9.366-6.37-17.029-6.37-5.96 0-10.927 1.628-14.9 4.883-3.832 3.114-6.457 7.715-7.876 13.802l-27.034-1.487zm25.331 47.987c0 4.105 1.703 7.502 5.108 10.191 3.548 2.548 8.586 3.822 15.114 3.822 5.25 0 9.933-1.274 14.048-3.822 4.258-2.547 7.522-6.369 9.792-11.465 2.413-5.238 3.619-11.537 3.619-18.898v-1.061l-21.712 3.822-3.832.637c-5.25.991-9.366 1.981-12.346 2.972-2.838.991-5.179 2.619-7.024 4.884-1.845 2.123-2.767 5.096-2.767 8.918zM611.079 93.616h24.905l.851 23.569-2.128-2.76c2.838-7.503 7.308-13.236 13.41-17.199 6.244-4.105 13.765-6.158 22.563-6.158 10.502 0 19.371 2.62 26.608 7.857 7.237 5.237 12.63 12.315 16.178 21.233 3.689 8.776 5.534 18.684 5.534 29.726 0 11.041-1.845 21.02-5.534 29.938-3.548 8.776-8.941 15.783-16.178 21.02-7.237 5.238-16.106 7.857-26.608 7.857-5.818 0-11.139-.92-15.964-2.761-4.683-1.84-8.728-4.529-12.133-8.068-3.264-3.539-5.818-7.927-7.663-13.165l2.554-2.123V238h-26.395V93.616zm24.266 56.268c0 6.794.994 13.022 2.98 18.684 2.129 5.521 5.322 9.98 9.579 13.377 4.257 3.397 9.437 5.096 15.539 5.096 9.224 0 16.178-3.539 20.861-10.616 4.824-7.078 7.237-15.925 7.237-26.541 0-10.475-2.413-19.322-7.237-26.542-4.683-7.219-11.637-10.828-20.861-10.828-6.102 0-11.282 1.769-15.539 5.308-4.257 3.397-7.45 7.927-9.579 13.589-1.986 5.521-2.98 11.678-2.98 18.473z"></path><path fill="url(#paint0_linear_0_1)" d="M218.623 0H50C22.386 0 0 22.386 0 50v168.623c0 27.614 22.386 50 50 50h168.623c27.614 0 50-22.386 50-50V50c0-27.614-22.386-50-50-50z"></path><path fill="url(#paint1_radial_0_1)" d="M134.646 240.418c58.415 0 105.77-47.355 105.77-105.77s-47.355-105.77-105.77-105.77-105.77 47.355-105.77 105.77 47.355 105.77 105.77 105.77z"></path><path fill="url(#paint2_linear_0_1)" d="M134.983 224.972c49.699 0 89.988-40.289 89.988-89.988 0-49.7-40.289-89.989-89.988-89.989-49.7 0-89.989 40.29-89.989 89.989 0 49.699 40.29 89.988 89.989 89.988z"></path><path fill="url(#paint3_radial_0_1)" d="M134.983 202.81c37.46 0 67.828-30.368 67.828-67.828 0-37.46-30.368-67.827-67.828-67.827-37.46 0-67.827 30.367-67.827 67.827 0 37.46 30.367 67.828 67.827 67.828z"></path><defs><linearGradient id="paint0_linear_0_1" x1="134.311" x2="134.311" y1="0" y2="268.623" gradientUnits="userSpaceOnUse"><stop stop-color="#A8DBF8"></stop><stop offset="1" stop-color="#BCCBFF"></stop></linearGradient><radialGradient id="paint1_radial_0_1" cx="0" cy="0" r="1" gradientTransform="rotate(107.38 31.314 98.475) scale(149.225)" gradientUnits="userSpaceOnUse"><stop offset="0.063" stop-color="#84D3FF"></stop><stop offset="0.411" stop-color="#17ABFE"></stop><stop offset="0.906" stop-color="#0770D1"></stop></radialGradient><linearGradient id="paint2_linear_0_1" x1="134.983" x2="134.983" y1="44.995" y2="224.972" gradientUnits="userSpaceOnUse"><stop stop-color="#BBE8FF"></stop><stop offset="1" stop-color="#018FEF"></stop></linearGradient><radialGradient id="paint3_radial_0_1" cx="0" cy="0" r="1" gradientTransform="rotate(98.584 14.543 129.964) scale(68.5955)" gradientUnits="userSpaceOnUse"><stop stop-color="#7EC3FF"></stop><stop offset="1" stop-color="#EAF5FF"></stop></radialGradient></defs></svg>
+ <h1 style="font-size: 21px; line-height: 26px;">You are now signed in. Please re-open the Cap desktop app to continue.</h1>
+ </div>
</body>
</html>
`;
diff --git a/apps/desktop/src/utils/auth/helpers.ts b/apps/desktop/src/utils/auth/helpers.ts
new file mode 100644
index 000000000..7390f3a3a
--- /dev/null
+++ b/apps/desktop/src/utils/auth/helpers.ts
@@ -0,0 +1,12 @@
+export async function authFetch(
+ url: string,
+ options: RequestInit,
+ retries: number = 3
+): Promise<Response> {
+ const response = await fetch(url, options);
+ if (response.status === 401 && retries > 0) {
+ console.warn("Unauthorized - trying again...");
+ return authFetch(url, options, retries - 1);
+ }
+ return response;
+}
diff --git a/apps/desktop/src/utils/auth/index.ts b/apps/desktop/src/utils/auth/index.ts
index 4754645e9..ceecfbc7f 100644
--- a/apps/desktop/src/utils/auth/index.ts
+++ b/apps/desktop/src/utils/auth/index.ts
@@ -1,7 +1,6 @@
"use client";
import callbackTemplate from "./callback.template";
-import { setCookie } from "cookies-next";
const dynamicImports = {
invoke: () => import("@tauri-apps/api").then(({ invoke }) => invoke),
diff --git a/apps/web/app/api/desktop/session/authenticate/route.ts b/apps/web/app/api/desktop/session/authenticate/route.ts
new file mode 100644
index 000000000..9edbd763d
--- /dev/null
+++ b/apps/web/app/api/desktop/session/authenticate/route.ts
@@ -0,0 +1,33 @@
+import { NextResponse, type NextRequest } from "next/server";
+
+export async function GET(req: NextRequest) {
+ const searchParams = req.nextUrl.searchParams;
+ const token = searchParams.get("token");
+ const redirect = searchParams.get("redirect");
+
+ console.log("Route.ts fired");
+
+ console.log("Token: ", token);
+ console.log("Redirect: ", redirect);
+
+ if (!token) {
+ return new Response(
+ JSON.stringify({ error: true, message: "Token not supplied" }),
+ {
+ status: 401,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ }
+ );
+ }
+
+ const response = NextResponse.next();
+ response.cookies.set({
+ name: "next-auth.session-token",
+ value: token,
+ path: "/",
+ });
+
+ return NextResponse.redirect(redirect || "/");
+}
diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts
new file mode 100644
index 000000000..27e68587a
--- /dev/null
+++ b/apps/web/middleware.ts
@@ -0,0 +1,28 @@
+import { NextRequest, NextResponse } from "next/server";
+
+export function middleware(request: NextRequest) {
+ if (request.nextUrl.pathname.startsWith("/api/desktop/")) {
+ const token = request.headers.get("authorization")?.split(" ")[1];
+ const cookieIsSet = request.cookies.get("next-auth.session-token");
+
+ if (cookieIsSet) {
+ return NextResponse.next();
+ }
+
+ if (token && !cookieIsSet) {
+ const response = NextResponse.next();
+ response.cookies.set({
+ name: "next-auth.session-token",
+ value: token,
+ path: "/",
+ });
+ return response;
+ }
+ }
+
+ return NextResponse.next();
+}
+
+export const config = {
+ matcher: ["/api/desktop/(.*)"],
+};
diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs
index c5625a80c..a4bb34d20 100644
--- a/apps/web/next.config.mjs
+++ b/apps/web/next.config.mjs
@@ -89,7 +89,7 @@ const nextConfig = {
},
{
key: "Access-Control-Allow-Headers",
- value: "*",
+ value: "Content-Type, Authorization",
},
{
key: "Access-Control-Allow-Credentials",
@@ -110,7 +110,7 @@ const nextConfig = {
},
{
key: "Access-Control-Allow-Headers",
- value: "*",
+ value: "Content-Type, Authorization",
},
{
key: "Access-Control-Allow-Credentials",