Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

wip: Working auth from web app to desktop

Richie McIlroy0f27b71522dc
diff --git a/apps/desktop/package.json b/apps/desktop/package.json
index fc4d7f5a8..4871ccc91 100644
--- a/apps/desktop/package.json
+++ b/apps/desktop/package.json
@@ -14,6 +14,7 @@
"@cap/utils": "workspace:*",
"@tauri-apps/api": "^1.5.0",
"autoprefixer": "^10.4.16",
+ "cookies-next": "^4.1.0",
"date-fns": "^3.2.0",
"dotenv": "^16.3.1",
"million": "^2.6.4",
diff --git a/apps/desktop/src/app/options/page.tsx b/apps/desktop/src/app/options/page.tsx
index 5ed29168b..7f1d750c4 100644
--- a/apps/desktop/src/app/options/page.tsx
+++ b/apps/desktop/src/app/options/page.tsx
@@ -13,7 +13,7 @@ export default function CameraPage() {
className="pt-4"
>
<WindowActions />
- <Recorder />
+ <SignIn />
</div>
);
}
diff --git a/apps/desktop/src/components/WindowActions.tsx b/apps/desktop/src/components/WindowActions.tsx
index f2f320dc8..f84fb99b7 100644
--- a/apps/desktop/src/components/WindowActions.tsx
+++ b/apps/desktop/src/components/WindowActions.tsx
@@ -1,4 +1,4 @@
-import { app } from "@tauri-apps/api/window";
+import { exit } from "@tauri-apps/api/process";
export const WindowActions = () => {
const actionButtonBase = "w-3 h-3 bg-gray-500 rounded-full m-0 p-0 block";
@@ -8,7 +8,7 @@ export const WindowActions = () => {
<div>
<button
onClick={() => {
- app.exit();
+ exit();
}}
className={`bg-red-500 hover:bg-red-700 transition-all ${actionButtonBase}`}
></button>
diff --git a/apps/desktop/src/components/windows/inner/SignIn.tsx b/apps/desktop/src/components/windows/inner/SignIn.tsx
index 29406cfd1..8a129aebf 100644
--- a/apps/desktop/src/components/windows/inner/SignIn.tsx
+++ b/apps/desktop/src/components/windows/inner/SignIn.tsx
@@ -3,9 +3,11 @@
import { Button, Logo } from "@cap/ui";
import { useState } from "react";
import { login } from "@/utils/auth";
+import { getCookie } from "cookies-next";
export const SignIn = () => {
const [loading, setLoading] = useState(false);
+ const cookie = getCookie("next-auth.session-token");
const handleSignIn = async () => {
setLoading(true);
@@ -13,6 +15,22 @@ export const SignIn = () => {
login();
};
+ const getData = async () => {
+ if (!cookie) {
+ return;
+ }
+
+ console.log("cookie:");
+ console.log(cookie);
+
+ const res = await fetch("http://localhost:3000/api/session/verify", {
+ credentials: "include",
+ });
+ const data = await res.json();
+ console.log("dataaa:");
+ console.log(data);
+ };
+
return (
<div
data-tauri-drag-region
@@ -32,6 +50,15 @@ export const SignIn = () => {
>
Sign in via Cap.so
</Button>
+ {cookie && (
+ <button
+ onClick={() => {
+ getData();
+ }}
+ >
+ get data
+ </button>
+ )}
</div>
</div>
);
diff --git a/apps/desktop/src/utils/auth/index.ts b/apps/desktop/src/utils/auth/index.ts
index 0d21f9afc..14f3aa1f1 100644
--- a/apps/desktop/src/utils/auth/index.ts
+++ b/apps/desktop/src/utils/auth/index.ts
@@ -2,18 +2,33 @@ import { invoke } from "@tauri-apps/api";
import { listen } from "@tauri-apps/api/event";
import callbackTemplate from "./callback.template";
import { open } from "@tauri-apps/api/shell";
+import { parse } from "url";
export const openSignIn = async (port: string) => {
await open(
- `http://localhost:3000/api/session/verify?redirectUrl=http://localhost:${port}`
+ `http://localhost:3000/api/session/request?redirectUrl=http://localhost:${port}`
);
};
export const login = () => {
// Wait for callback from tauri oauth plugin
- listen("oauth://url", (data) => {
- console.log("payload:");
- console.log(data.payload);
+ listen("oauth://url", (data: { payload: string }) => {
+ if (!data.payload.includes("token")) {
+ return;
+ }
+
+ const urlObject = parse(data.payload, true);
+
+ const token = urlObject.query.token as string;
+ const expires = urlObject.query.expires as string;
+
+ if (!token || !expires) {
+ return;
+ }
+
+ const expiresDate = new Date(parseInt(expires) * 1000);
+
+ document.cookie = `next-auth.session-token=${token}; expires=${expiresDate.toUTCString()}; path=/`;
});
// Start tauri oauth plugin. When receive first request
diff --git a/apps/web/app/api/session/request/route.ts b/apps/web/app/api/session/request/route.ts
new file mode 100644
index 000000000..0b802c15e
--- /dev/null
+++ b/apps/web/app/api/session/request/route.ts
@@ -0,0 +1,44 @@
+import { type NextRequest } from "next/server";
+import { getServerSession } from "next-auth";
+import { authOptions } from "@cap/database/auth/auth-options";
+import { getToken, encode } from "next-auth/jwt";
+
+export async function GET(req: NextRequest) {
+ const session = await getServerSession(authOptions);
+ const searchParams = req.nextUrl.searchParams;
+ const redirectUrl = searchParams.get("redirectUrl") || "";
+
+ const token = await getToken({
+ req,
+ secret: process.env.NEXTAUTH_SECRET ?? "",
+ });
+
+ if (!token) {
+ return new Response(JSON.stringify({ isLoggedIn: false }), {
+ status: 401,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+ }
+
+ const encodedToken = await encode({
+ token: token,
+ secret: process.env.NEXTAUTH_SECRET ?? "",
+ });
+
+ const returnUrl = new URL(
+ `${redirectUrl}?token=${encodedToken}&expires=${token?.exp}`
+ );
+
+ if (session) {
+ return Response.redirect(returnUrl);
+ } else {
+ return new Response(JSON.stringify({ isLoggedIn: false }), {
+ status: 401,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+ }
+}
diff --git a/apps/web/app/api/session/verify/route.ts b/apps/web/app/api/session/verify/route.ts
index c39815479..7cd18d1be 100644
--- a/apps/web/app/api/session/verify/route.ts
+++ b/apps/web/app/api/session/verify/route.ts
@@ -4,13 +4,24 @@ import { authOptions } from "@cap/database/auth/auth-options";
export async function GET(req: NextRequest) {
const session = await getServerSession(authOptions);
- const searchParams = req.nextUrl.searchParams;
- const redirectUrl = searchParams.get("redirectUrl") || "";
+ const cookies = req.cookies;
+
+ console.log("req:::::", req);
+
+ console.log("session:::::", session);
+ console.log("cookies:::::", cookies);
if (session) {
- return Response.redirect(redirectUrl);
+ return new Response(
+ JSON.stringify({ isLoggedIn: true, user: session.user }),
+ {
+ status: 200,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ }
+ );
} else {
- // No active session
return new Response(JSON.stringify({ isLoggedIn: false }), {
status: 401,
headers: {
diff --git a/apps/web/app/dashboard/layout.tsx b/apps/web/app/dashboard/layout.tsx
index 6bb596dc2..cecd8302e 100644
--- a/apps/web/app/dashboard/layout.tsx
+++ b/apps/web/app/dashboard/layout.tsx
@@ -19,8 +19,6 @@ export default async function DashboardLayout({
}) {
const session = await getCurrentUser();
- console.log("session", session);
-
if (!session) {
redirect("/login");
}
diff --git a/apps/web/next.config.mjs b/apps/web/next.config.mjs
index 796c3e22a..259707024 100644
--- a/apps/web/next.config.mjs
+++ b/apps/web/next.config.mjs
@@ -25,7 +25,7 @@ const nextConfig = {
async headers() {
return [
{
- source: "/api/session/verify",
+ source: "/api/session/(.*)",
headers: [
{
key: "Access-Control-Allow-Origin",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 0978f5082..728033219 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -41,6 +41,9 @@ importers:
autoprefixer:
specifier: ^10.4.16
version: 10.4.17([email protected])
+ cookies-next:
+ specifier: ^4.1.0
+ version: 4.1.0
date-fns:
specifier: ^3.2.0
version: 3.2.0