Skip to content

Commit

Permalink
email
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsainlll committed Aug 11, 2024
1 parent 27dcd78 commit 5a79214
Show file tree
Hide file tree
Showing 19 changed files with 1,276 additions and 561 deletions.
38 changes: 19 additions & 19 deletions actions/saveBookmark.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
"use server";
// import { db } from "@/db";
import { auth, currentUser } from "@clerk/nextjs/server";
// import { projects } from "@/db/schema";
import { redirect } from "next/navigation";
// "use server";
// // import { db } from "@/db";
// import { auth, currentUser } from "@clerk/nextjs/server";
// // import { projects } from "@/db/schema";
// import { redirect } from "next/navigation";

export async function createProject(formData: FormData) {
const { userId } = auth();
// export async function createProject(formData: FormData) {
// const { userId } = auth();

const project = {
name: formData.get("name") as string,
description: formData.get("description") as string,
url: formData.get("url") as string,
userId,
};
// const project = {
// name: formData.get("name") as string,
// description: formData.get("description") as string,
// url: formData.get("url") as string,
// userId,
// };

// const [newProject] = await db
// .insert(projects)
// .values(project)
// .returning({ insertedId: projects.id });
// // const [newProject] = await db
// // .insert(projects)
// // .values(project)
// // .returning({ insertedId: projects.id });

// redirect(`/projects/${newProject.insertedId}/instructions`);
}
// // redirect(`/projects/${newProject.insertedId}/instructions`);
// }
81 changes: 81 additions & 0 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import prisma from "@/lib/prisma";
import { compare } from "bcrypt";
import NextAuth, { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";

export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
name: "Sign in",
credentials: {
email: {
label: "Email",
type: "email",
placeholder: "[email protected]",
},
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
if (!credentials?.email || !credentials.password) {
return null;
}

const user = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});

if (!user) {
return null;
}

const isPasswordValid = await compare(
credentials.password,
user.password
);

if (!isPasswordValid) {
return null;
}

return {
id: user.id + "",
email: user.email,
randomKey: "Hey cool",
};
},
}),
],
// callbacks: {
// session: ({ session, token }) => {
// console.log("Session Callback", { session, token });
// return {
// ...session,
// user: {
// ...session.user,
// id: token.id,
// randomKey: token.randomKey,
// },
// };
// },
// jwt: ({ token, user }) => {
// console.log("JWT Callback", { token, user });
// if (user) {
// const u = user as unknown as any;
// return {
// ...token,
// id: u.id,
// randomKey: u.randomKey,
// };
// }
// return token;
// },
// },
};

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
136 changes: 78 additions & 58 deletions app/api/bookmarks/route.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,78 @@
// post :
// Checks if the request method is POST.
// Retrieves the user’s ID using Clerk’s getAuth function.
// Creates a new bookmark associated with the logged-in user using Prisma’s create method.
// Returns the created bookmark or an error if something goes wrong.

import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { auth } from "@clerk/nextjs/server";

export async function GET(req: Request) {
const { userId } = auth();

if (!userId) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

try {
const bookmarks = await prisma.bookmark.findMany({
where: { userId },
});
return NextResponse.json(bookmarks);
} catch (error) {
return NextResponse.json(
{ error: "Failed to fetch bookmarks" },
{ status: 500 }
);
}
}

export async function POST(req: Request) {
const { userId } = auth();

if (!userId) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}

try {
const body = await req.json();
const { name, url, description } = body;

const bookmark = await prisma.bookmark.create({
data: {
name,
url,
description,
userId,
},
});

return NextResponse.json(bookmark, { status: 201 });
} catch (error) {
return NextResponse.json(
{ error: "Failed to create bookmark" },
{ status: 500 }
);
}
}
// // post :
// // Checks if the request method is POST.
// // Retrieves the user’s ID using Clerk’s getAuth function.
// // Creates a new bookmark associated with the logged-in user using Prisma’s create method.
// // Returns the created bookmark or an error if something goes wrong.

// import { NextResponse } from "next/server";
// import prisma from "@/lib/prisma";
// import { auth } from "@clerk/nextjs/server";

// export async function GET(req: Request) {
// const { userId } = auth();

// if (!userId) {
// return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
// }

// try {
// const bookmarks = await prisma.bookmark.findMany({
// where: { userId },
// });
// return NextResponse.json(bookmarks);
// } catch (error) {
// return NextResponse.json(
// { error: "Failed to fetch bookmarks" },
// { status: 500 }
// );
// }
// }

// export async function POST(req: Request) {
// const { userId } = auth();

// if (!userId) {
// return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
// }

// try {
// // Check if the user exists
// const userExists = await prisma.user.findUnique({
// where: { id: userId },
// });

// if (!userExists) {
// return NextResponse.json(
// { error: "User does not exist" },
// { status: 404 }
// );
// }

// const body = await req.json();
// const { name, url, description } = body;

// if (!name || !url) {
// return NextResponse.json(
// { error: "Name and URL are required" },
// { status: 400 }
// );
// }

// const bookmark = await prisma.bookmark.create({
// data: {
// name,
// url,
// description: description || null,
// userId,
// },
// });

// return NextResponse.json(bookmark, { status: 201 });
// } catch (error) {
// console.error("Failed to create bookmark:", error); // Added logging for better debugging
// return NextResponse.json(
// { error: "Failed to create bookmark" },
// { status: 500 }
// );
// }
// }
16 changes: 16 additions & 0 deletions app/api/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getServerSession } from "next-auth/next";
import { NextResponse } from "next/server";
import { authOptions } from "./auth/[...nextauth]/route";

export async function GET(request: Request) {
const session = await getServerSession(authOptions);

if (!session) {
return new NextResponse(JSON.stringify({ error: "unauthorized" }), {
status: 401,
});
}

console.log("GET API", session);
return NextResponse.json({ authenticated: !!session });
}
36 changes: 23 additions & 13 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import {
ClerkProvider,
} from "@clerk/nextjs";
import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import PageHeader from "@/components/page-header";
import clsx from "clsx";
import { Providers } from "./provider";

const inter = Inter({ subsets: ["latin"] });

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: {
}: Readonly<{
children: React.ReactNode;
}) {
}>) {
return (
<ClerkProvider>
<html lang="en">
<body>
<PageHeader />
{children}
</body>
</html>
</ClerkProvider>
<html lang="en">
<body
className={clsx(inter.className, "max-w-[1200px] mx-auto w-full p-4")}
>
<PageHeader />
<main>
<Providers>{children}</Providers>
</main>
</body>
</html>
);
}
Loading

0 comments on commit 5a79214

Please sign in to comment.