Skip to content

Commit

Permalink
feat: added next-auth and completed config for user auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yeasin2002 committed Aug 21, 2024
1 parent 2825cae commit fe64de2
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 98 deletions.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"node": ">=20.0.0"
},
"dependencies": {
"@auth/mongodb-adapter": "^3.2.0",
"@auth/mongodb-adapter": "^3.4.2",
"@auth/prisma-adapter": "^2.4.1",
"@edgestore/react": "^0.1.7",
"@edgestore/server": "^0.1.7",
Expand All @@ -38,7 +38,7 @@
"@radix-ui/react-tooltip": "^1.0.7",
"@react-email/components": "0.0.22",
"@tailwindcss/typography": "^0.5.12",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"chart.js": "^4.4.3",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
Expand All @@ -48,11 +48,11 @@
"jose": "^5.6.2",
"lint-staged": "^15.2.5",
"lucide-react": "^0.396.0",
"mongodb": "^6.7.0",
"mongodb": "^6.8.0",
"mongoose": "^8.5.3",
"negotiator": "^0.6.3",
"next": "14.1.4",
"next-auth": "^5.0.0-beta.19",
"next-auth": "^5.0.0-beta.20",
"next-connect": "^1.0.0",
"next-videos": "^1.4.1",
"nextjs-toploader": "^1.6.12",
Expand All @@ -72,6 +72,7 @@
"sweetalert2": "^11.12.0",
"swiper": "^11.1.0",
"tailwind-merge": "^2.2.2",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.23.8"
},
"devDependencies": {
Expand All @@ -80,6 +81,7 @@
"@biomejs/biome": "1.8.3",
"@hookform/devtools": "^4.3.1",
"@types/bcrypt": "^5.0.2",
"@types/bcryptjs": "^2.4.6",
"@types/globalthis": "^1.0.4",
"@types/negotiator": "^0.6.3",
"@types/node": "^22.3.0",
Expand Down
8 changes: 2 additions & 6 deletions src/Internationalization/languages/bn.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@
"footerInfo": {
"contact": {
"title": "যোগাযোগ",
"value": [
"০১৭৩১-৫১৯০২৫",
"০১৫৩৩-৮৮১১০২",
"০১৬৭৯-১১২৭২৫"
]
"value": ["০১৭৩১-৫১৯০২৫", "০১৫৩৩-৮৮১১০২", "০১৬৭৯-১১২৭২৫"]
},
"address": {
"title": "ঠিকানা",
Expand Down Expand Up @@ -140,4 +136,4 @@
"bookAppointment3": "ডাক্তারদের সাথে চিকিৎসা নিতে "
}
}
}
}
8 changes: 2 additions & 6 deletions src/Internationalization/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,7 @@
"footerInfo": {
"contact": {
"title": "Contact",
"value": [
"01731-519025",
"01533-881102",
"01679-112725"
]
"value": ["01731-519025", "01533-881102", "01679-112725"]
},
"address": {
"title": "Address",
Expand Down Expand Up @@ -139,4 +135,4 @@
"bookAppointment3": "with doctor and get treatment."
}
}
}
}
2 changes: 2 additions & 0 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { handlers } from "@/auth";
export const { GET, POST } = handlers;
30 changes: 9 additions & 21 deletions src/auth.md → src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import NextAuth from "next-auth"
import { MongoDBAdapter } from "@auth/mongodb-adapter"
import client from "./lib/mongoDBClientPromise"

import mongoose from "mongoose";
import { MongoDBAdapter } from "@auth/mongodb-adapter";
import bcrypt from "bcryptjs";

import bcrypt from "bcrypt";
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";


import { signInSchema } from "@/schema";
import mongoose from "mongoose";
import connectDB from "./lib/connectDB";
import client from "@/lib/MongoDBClient";

export const {
handlers: { GET, POST },
signIn,
signOut,
auth,
} = NextAuth({
export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: MongoDBAdapter(client),

session: {
strategy: "jwt",
},
providers: [
Credentials({
credentials: {
Expand All @@ -31,10 +21,8 @@ export const {
if (credentials === null) return null;
const { email, password } = await signInSchema.parseAsync(credentials);

await prisma.$connect();
const user = await prisma.doctors.findFirst({
where: { email: email },
});
await connectDB();
const user = await mongoose.models.Doctor.findOne({ email: email });

if (!user) throw new Error("User not found");

Expand Down
37 changes: 37 additions & 0 deletions src/lib/MongoDBClient.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This approach is taken from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb
import { MongoClient, ServerApiVersion } from "mongodb";

if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}

const uri = process.env.MONGODB_URI;
const options = {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
};

let client: MongoClient;

if (process.env.NODE_ENV === "development") {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
const globalWithMongo = global as typeof globalThis & {
_mongoClient?: MongoClient;
};

if (!globalWithMongo._mongoClient) {
globalWithMongo._mongoClient = new MongoClient(uri, options);
}
client = globalWithMongo._mongoClient;
} else {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options);
}

// Export a module-scoped MongoClient. By doing this in a
// separate module, the client can be shared across functions.
export default client;
55 changes: 0 additions & 55 deletions src/lib/dbClientPromise.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./dbClientPromise";
// export * from "./prisma";
export * from "./MongoDBClient";
export * from "./connectDB";
export * from "./edgestore";
7 changes: 4 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { internationalization, sessionUpdate } from "@/middlewares";
import { auth as authSession } from "@/auth";

import { createEdgeRouter } from "next-connect";
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";

const router = createEdgeRouter<NextRequest, NextFetchEvent>();
router.use(internationalization);
// router.use(authSession);
// router.use(sessionUpdate);
router.all(() => {
return NextResponse.next();
});

router.all(() => NextResponse.next());

export function middleware(request: NextRequest, event: NextFetchEvent) {
return router.run(request, event);
Expand Down
3 changes: 2 additions & 1 deletion tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Config } from "tailwindcss";
import tailwindcssAnimate from "tailwindcss-animate";

const config = {
darkMode: ["class"],
Expand Down Expand Up @@ -110,7 +111,7 @@ const config = {
},
},
},
plugins: [],
plugins: [tailwindcssAnimate],
} satisfies Config;

export default config;

0 comments on commit fe64de2

Please sign in to comment.