dynamic worker routes based on env
This commit is contained in:
@ -11,7 +11,12 @@ export const revalidate = 0
|
||||
|
||||
const getUserData = async (id: string) => {
|
||||
const userRes = await fetch(
|
||||
`https://database.ishaan1013.workers.dev/api/user?id=${id}`
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
const userData: User = await userRes.json()
|
||||
return userData
|
||||
@ -19,7 +24,12 @@ const getUserData = async (id: string) => {
|
||||
|
||||
const getSandboxData = async (id: string) => {
|
||||
const sandboxRes = await fetch(
|
||||
`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox?id=${id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
const sandboxData: Sandbox = await sandboxRes.json()
|
||||
return sandboxData
|
||||
@ -33,7 +43,12 @@ const getSharedUsers = async (usersToSandboxes: UsersToSandboxes[]) => {
|
||||
const shared = await Promise.all(
|
||||
usersToSandboxes.map(async (user) => {
|
||||
const userRes = await fetch(
|
||||
`https://database.ishaan1013.workers.dev/api/user?id=${user.userId}`
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.userId}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
const userData: User = await userRes.json()
|
||||
return { id: userData.id, name: userData.name }
|
||||
|
@ -12,12 +12,22 @@ export default async function DashboardPage() {
|
||||
}
|
||||
|
||||
const userRes = await fetch(
|
||||
`https://database.ishaan1013.workers.dev/api/user?id=${user.id}`
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
const userData = (await userRes.json()) as User
|
||||
|
||||
const sharedRes = await fetch(
|
||||
`https://database.ishaan1013.workers.dev/api/sandbox/share?id=${user.id}`
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox/share?id=${user.id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
const shared = (await sharedRes.json()) as {
|
||||
id: string
|
||||
|
@ -1,30 +1,36 @@
|
||||
import { User } from "@/lib/types";
|
||||
import { currentUser } from "@clerk/nextjs";
|
||||
import { redirect } from "next/navigation";
|
||||
import { User } from "@/lib/types"
|
||||
import { currentUser } from "@clerk/nextjs"
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
export default async function AppAuthLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const user = await currentUser();
|
||||
const user = await currentUser()
|
||||
|
||||
if (!user) {
|
||||
redirect("/");
|
||||
redirect("/")
|
||||
}
|
||||
|
||||
const dbUser = await fetch(
|
||||
`https://database.ishaan1013.workers.dev/api/user?id=${user.id}`
|
||||
);
|
||||
const dbUserJSON = (await dbUser.json()) as User;
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
const dbUserJSON = (await dbUser.json()) as User
|
||||
|
||||
if (!dbUserJSON.id) {
|
||||
const res = await fetch(
|
||||
"https://database.ishaan1013.workers.dev/api/user",
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
id: user.id,
|
||||
@ -32,8 +38,8 @@ export default async function AppAuthLayout({
|
||||
email: user.emailAddresses[0].emailAddress,
|
||||
}),
|
||||
}
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
return <>{children}</>
|
||||
}
|
||||
|
@ -18,7 +18,12 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const res = await fetch(
|
||||
`https://database.ishaan1013.workers.dev/api/user?id=${clerkUser.id}`
|
||||
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${clerkUser.id}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
const user = (await res.json()) as User
|
||||
|
||||
|
Reference in New Issue
Block a user