mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-08-28 05:25:33 +00:00
stuff
This commit is contained in:
parent
5b44dcb1d1
commit
8652405fbd
5 changed files with 77 additions and 26 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
2
apps/web/cf-env.d.ts
vendored
2
apps/web/cf-env.d.ts
vendored
|
|
@ -17,6 +17,8 @@ declare global {
|
|||
CLOUDFLARE_ACCOUNT_ID: string;
|
||||
CLOUDFLARE_DATABASE_ID: string;
|
||||
CLOUDFLARE_D1_TOKEN: string;
|
||||
|
||||
MOBILE_TRUST_TOKEN: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
12
package.json
12
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",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue