Skip to content

Commit

Permalink
authOptions issue solved
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulsainlll committed Aug 12, 2024
1 parent 5967f5a commit f28f808
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 83 deletions.
81 changes: 2 additions & 79 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,5 @@
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;
}

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

if (!user) {
console.log("User not found");
return null;
}

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

if (!isPasswordValid) {
console.log("Invalid password");
return null;
}

return {
id: user.id.toString(),
email: user.email,
};
} catch (error) {
console.error("Error in authorization:", error);
return null;
}
},
}),
],
callbacks: {
session: ({ session, token }) => {
return {
...session,
user: {
...session.user,
id: token.id,
},
};
},
jwt: ({ token, user }) => {
if (user) {
return {
...token,
};
}
return token;
},
},
pages: {
signIn: "/auth/signin",
},
};
import { authOptions } from "@/lib/auth";
import NextAuth from "next-auth/next";

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
2 changes: 1 addition & 1 deletion app/api/bookmarks/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../../auth/[...nextauth]/route";
import { authOptions } from "@/lib/auth";

export async function DELETE(req: Request) {
const session = await getServerSession(authOptions);
Expand Down
2 changes: 1 addition & 1 deletion app/api/bookmarks/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]/route";
import { authOptions } from "@/lib/auth";

export async function GET(req: Request) {
const session = await getServerSession(authOptions);
Expand Down
2 changes: 1 addition & 1 deletion app/api/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { authOptions } from "@/lib/auth";
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);
Expand Down
2 changes: 1 addition & 1 deletion app/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table";
import { authOptions } from "../api/auth/[...nextauth]/route";
import { authOptions } from "@/lib/auth";
import DeleteBookmarkButton from "@/components/DeleteBookmarkButton";
import Link from "next/link";

Expand Down
81 changes: 81 additions & 0 deletions lib/auth.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;
}

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

if (!user) {
console.log("User not found");
return null;
}

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

if (!isPasswordValid) {
console.log("Invalid password");
return null;
}

return {
id: user.id.toString(),
email: user.email,
};
} catch (error) {
console.error("Error in authorization:", error);
return null;
}
},
}),
],
callbacks: {
session: ({ session, token }) => {
return {
...session,
user: {
...session.user,
id: token.id,
},
};
},
jwt: ({ token, user }) => {
if (user) {
return {
...token,
id: user.id,
};
}
return token;
},
},
pages: {
signIn: "/auth/signin",
},
};

0 comments on commit f28f808

Please sign in to comment.