Skip to content

Commit 48a4eb7

Browse files
committed
downGraded from auth.js to nextjs or v5 to v4
- added files needed for middleware and other also - removed -auth.ts since not needed in nextauthv4
1 parent e2126c1 commit 48a4eb7

File tree

12 files changed

+495
-79
lines changed

12 files changed

+495
-79
lines changed

.env.example

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// "use client";
2+
3+
// import { useSession } from "next-auth/react";
4+
// import { redirect } from "next/navigation";
5+
6+
// const Dashboard = () => {
7+
// const { data: session } = useSession({
8+
// required: true,
9+
// // onUnauthenticated() {
10+
// // redirect("/signIn");
11+
// // },
12+
// });
13+
// return (
14+
// <div className=" text-2xl min-h-screen flex justify-center items-center">
15+
// Dashboard
16+
// </div>
17+
// );
18+
// };
19+
20+
// export default Dashboard;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from "react";
2+
3+
const Dashboard = () => {
4+
return (
5+
<div className=" min-h-screen flex justify-center items-center text-2xl">
6+
Dashboard (protected)
7+
</div>
8+
);
9+
};
10+
11+
export default Dashboard;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from "react";
2+
3+
const Register = () => {
4+
return (
5+
<div className=" text-2xl flex justify-center items-center min-h-screen">
6+
Register
7+
</div>
8+
);
9+
};
10+
11+
export default Register;

app/(authentication)/signIn/page.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from "react";
2+
3+
const SignIn = () => {
4+
return (
5+
<div className=" text-2xl min-h-screen flex items-center justify-center">
6+
SignIn
7+
</div>
8+
);
9+
};
10+
11+
export default SignIn;

app/api/auth/[...nextauth]/route.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
1-
import { handlers } from "@/auth"; // Referring to the auth.ts we just created
2-
export const { GET, POST } = handlers;
1+
import connectDB from "@/lib/mongodb";
2+
import NextAuth, { NextAuthOptions } from "next-auth";
3+
import CredentialsProvider from "next-auth/providers/credentials";
4+
5+
// Define a type for your user
6+
interface User {
7+
id: string;
8+
name: string;
9+
email: string;
10+
}
11+
12+
export const authOptions: NextAuthOptions = {
13+
providers: [
14+
CredentialsProvider({
15+
name: "Credentials",
16+
credentials: {
17+
username: { label: "Username", type: "text" },
18+
password: { label: "Password", type: "password" },
19+
},
20+
async authorize(credentials: Record<string, string> | undefined, req) {
21+
await connectDB();
22+
23+
const username = credentials?.username;
24+
const password = credentials?.password;
25+
26+
// Replace this with your actual user validation logic
27+
if (username === "test" && password === "test") {
28+
const user: User = {
29+
id: "1",
30+
name: "Test User",
31+
32+
};
33+
return user;
34+
} else {
35+
return null;
36+
}
37+
},
38+
}),
39+
],
40+
41+
session: {
42+
strategy: "jwt",
43+
},
44+
secret: process.env.NEXTAUTH_SECRET,
45+
pages: {
46+
signIn: "/",
47+
},
48+
};
49+
50+
const handler = NextAuth(authOptions);
51+
52+
export { handler as GET, handler as POST };

app/layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Metadata } from "next";
22
import { Space_Grotesk as FontSans } from "next/font/google";
33
import "./globals.css";
44
import { cn } from "@/lib/utils";
5+
import Provider from "@/provider/sessionProvider";
56

67
const fontSans = FontSans({
78
subsets: ["latin"],
@@ -37,7 +38,7 @@ export default function RootLayout({
3738
fontSans.variable
3839
)}
3940
>
40-
{children}
41+
<Provider> {children}</Provider>
4142
</body>
4243
</html>
4344
);

auth.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

middleware.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
export { auth as middleware } from "@/auth";
1+
export { default } from "next-auth/middleware";
2+
3+
export const config = { matcher: ["/dashboard"] };

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
"dependencies": {
1212
"@radix-ui/react-icons": "^1.3.0",
1313
"@radix-ui/react-slot": "^1.1.0",
14+
"@types/bcrypt": "^5.0.2",
15+
"bcrypt": "^5.1.1",
1416
"class-variance-authority": "^0.7.0",
1517
"clsx": "^2.1.1",
1618
"mongoose": "^8.5.1",
1719
"next": "14.2.5",
18-
"next-auth": "5.0.0-beta.19",
20+
"next-auth": "^4.24.7",
1921
"react": "^18",
2022
"react-dom": "^18",
2123
"react-icons": "^5.2.1",

0 commit comments

Comments
 (0)