diff --git a/apps/web/app/api/ensureAuth.ts b/apps/web/app/api/ensureAuth.ts index a8c43cdc..1fcd2914 100644 --- a/apps/web/app/api/ensureAuth.ts +++ b/apps/web/app/api/ensureAuth.ts @@ -1,6 +1,6 @@ import { NextRequest } from "next/server"; import { db } from "../../server/db"; -import { sessions, users } from "../../server/db/schema"; +import { accounts, sessions, users } from "../../server/db/schema"; import { eq } from "drizzle-orm"; export async function ensureAuth(req: NextRequest) { @@ -16,12 +16,84 @@ export async function ensureAuth(req: NextRequest) { return undefined; } - const sessionData = await db + let sessionData = await db .select() .from(sessions) .innerJoin(users, eq(users.id, sessions.userId)) .where(eq(sessions.sessionToken, token!)); + const isMobile = + token.split("?") && token.split("?")[1] === `source="mobile"`; + + if (isMobile) { + // remove everything after ? in token + const newToken = token.split("?").slice(0, -1).join("?"); + + console.log(token, newToken); + + const authUserFetch = await fetch( + `https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=${newToken}`, + ); + + if (!authUserFetch.ok) { + console.error( + "Error fetching Google user,", + authUserFetch.statusText, + await authUserFetch.text(), + ); + console.log("Google user not found or error."); + return undefined; + } + + const authUserData = (await authUserFetch.json()) as { + email: string; + audience: string; + issued_to: string; + }; + + console.log(authUserData); + + if ( + !( + authUserData.audience.split("-")[0] === + process.env.GOOGLE_CLIENT_ID.split("-")[0] && + authUserData.issued_to.split("-")[0] === + process.env.GOOGLE_CLIENT_ID.split("-")[0] + ) + ) { + console.log( + "Google user not authorized because of audience or issued_to mismatch", + ); + return undefined; + } + + const authUserEmail = authUserData.email; + + let user = await db + .select() + .from(users) + .where(eq(users.email, authUserEmail)) + .limit(1); + + if (!user || user.length === 0) { + // create the user + user = await db + .insert(users) + .values({ + email: authUserEmail, + name: authUserEmail.split("@")[0], + }) + .returning(); + } + + sessionData = [ + { + ...sessionData[0]!, + user: user[0]!, + }, + ]; + } + if (!sessionData || sessionData.length === 0) { return undefined; } diff --git a/apps/web/app/api/mobile/newUser/route.ts b/apps/web/app/api/mobile/newUser/route.ts deleted file mode 100644 index ed7cbdf3..00000000 --- a/apps/web/app/api/mobile/newUser/route.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { NextRequest } from "next/server"; -import { z } from "zod"; - -export const runtime = "edge"; - -const newMobileUserBody = z.object({ - // this is a string in the format - encodedUserString: z.string(), -}); - -export async function POST(req: NextRequest) { - const body = await req.json(); -} diff --git a/apps/web/cf-env.d.ts b/apps/web/cf-env.d.ts index 650cf4a3..7381d63e 100644 --- a/apps/web/cf-env.d.ts +++ b/apps/web/cf-env.d.ts @@ -17,6 +17,8 @@ declare global { CLOUDFLARE_ACCOUNT_ID: string; CLOUDFLARE_DATABASE_ID: string; CLOUDFLARE_D1_TOKEN: string; + + MOBILE_TRUST_TOKEN: string; } } } diff --git a/bun.lockb b/bun.lockb index 146baec7..cd064755 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 0befe291..da1c1b7e 100644 --- a/package.json +++ b/package.json @@ -72,17 +72,7 @@ "@radix-ui/react-toast": "^1.1.5", "@radix-ui/react-tooltip": "^1.1.2", "@tldraw/assets": "^2.2.0", - "@types/react-responsive-masonry": "^2.1.3", - "@types/readline-sync": "^1.4.8", - "ai": "^3.1.14", - "aws4fetch": "^1.0.18", - "cheerio": "^1.0.0-rc.12", - "compromise": "^14.13.0", - "drizzle-orm": "0.30.0", - "eslint-config-turbo": "^2.0.6", - "framer-motion": "^11.2.6", - "geist": "^1.3.0", - "grammy": "^1.25.1", + "jose": "^5.6.3", "katex": "^0.16.10", "lucide-react": "^0.379.0", "next-app-theme": "^0.1.10",