-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jake shenanigans to get middleware up and user created
- Loading branch information
1 parent
aecb723
commit 65a56a0
Showing
8 changed files
with
152 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { User } from "@prisma/client"; | ||
|
||
declare module "next" { | ||
interface NextApiRequest { | ||
user?: User; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import prisma from "./client"; | ||
|
||
async function createArtEntries() { | ||
try { | ||
await prisma.art.create({ | ||
data: { | ||
boxCount: 5, | ||
Author: { | ||
create: { | ||
email: "[email protected]", | ||
name: "test", | ||
ClerkId: "test" | ||
} | ||
} | ||
} | ||
}); | ||
|
||
await prisma.art.create({ | ||
data: { | ||
boxCount: 10, | ||
Author: { | ||
create: { | ||
email: "[email protected]", | ||
name: "test2", | ||
ClerkId: "test2" | ||
} | ||
} | ||
} | ||
}); | ||
|
||
console.log("Art entries created successfully"); | ||
} catch (error) { | ||
console.error("Error creating art entries:", error); | ||
} finally { | ||
await prisma.$disconnect(); | ||
} | ||
} | ||
|
||
export default createArtEntries; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useAuth } from "@clerk/nextjs"; | ||
import { auth, currentUser } from "@clerk/nextjs/server"; | ||
import { useEffect } from "react"; | ||
import prisma from "../../../../prisma/client"; | ||
import { NextApiHandler } from "next"; | ||
import { optionalUser } from "@/middleware/optionalUser"; | ||
import { redirect } from "next/navigation"; | ||
|
||
|
||
|
||
export const GET = optionalUser(async (req, res) => { | ||
console.log(req.user) | ||
return ( | ||
redirect("/") | ||
); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import prisma from "../../../prisma/client"; | ||
import { ArtInterpreter } from "../ArtInterpreter"; | ||
|
||
export const FeedScreen = () => { | ||
|
||
|
||
|
||
return ( | ||
<div> | ||
Feed: | ||
{prisma.art.findMany().then((arts) => { | ||
return arts.map((art) => { | ||
return ( | ||
<ArtInterpreter key={art.id} boxCount={art.boxCount} /> | ||
); | ||
}); | ||
})} | ||
|
||
</div> | ||
); | ||
}; | ||
|
||
export default FeedScreen; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { clerkMiddleware } from "@clerk/nextjs/server"; | ||
import { clerkMiddleware, currentUser } from "@clerk/nextjs/server"; | ||
import prisma from "../prisma/client"; | ||
|
||
export default clerkMiddleware(); | ||
|
||
export const config = { | ||
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"], | ||
}; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useAuth } from "@clerk/nextjs"; | ||
import { auth, currentUser } from "@clerk/nextjs/server"; | ||
import { useEffect } from "react"; | ||
import { NextApiHandler } from "next"; | ||
import prisma from "../../prisma/client"; | ||
|
||
|
||
|
||
|
||
|
||
export function optionalUser(handler: NextApiHandler): NextApiHandler { | ||
return async (req, res) => { | ||
const clerkId = auth().userId; | ||
|
||
if (!clerkId) { | ||
return handler(req, res) | ||
} | ||
else { | ||
const user = await prisma.user.findUnique({ | ||
where: { | ||
ClerkId: clerkId | ||
} | ||
|
||
}) | ||
|
||
if (user) { | ||
// append user to request context | ||
req.user = user; | ||
} else { | ||
const curr = await currentUser(); | ||
|
||
// otherwise create a new user and append to request context | ||
req.user = await prisma.user.create({ | ||
data: { | ||
ClerkId: clerkId, | ||
email: curr?.emailAddresses[0].emailAddress || "", | ||
name: curr?.username | ||
} | ||
}) | ||
|
||
} | ||
} | ||
|
||
return handler(req, res) | ||
|
||
} | ||
} |