Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the about section code and improved the authorisation a little #1112

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions app/(Customer)/About/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ export const metadata: Metadata = {
const About = () => {
return (
<div className="h-full dark:bg-DarkGray">
<div className="h-full ">
<div className="text-white flex items-center justify-center bg-customTeal dark:bg-gradient-to-r from-Green to-Yellow h-full mb-20 p-24">
<div className=" text-4xl md:text-7xl text-gray-200 font-bold font-handlee">About us</div>
</div>
</div>
<SeperatorHeading label="Learn About us"/>
<LearnAboutUs />
<header className="flex items-center justify-center h-[60vh] bg-customTeal dark:bg-gradient-to-r from-Green to-Yellow mb-20 p-8">
<h1 className="text-4xl md:text-7xl text-gray-200 font-bold font-handlee">About Us</h1>
</header>

<section aria-labelledby="about-us-heading">
<SeperatorHeading label="Learn About Us" />
<LearnAboutUs />
</section>

<div className="w-full border-b border-Green my-10"/>
<Features/>
<div className="w-full border-b border-Green my-10" />

<section aria-labelledby="features-heading">
<Features />
</section>
</div>
);
};
Expand Down
123 changes: 25 additions & 98 deletions lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@

import CredentialsProvider from "next-auth/providers/credentials";
import { NextAuthOptions } from "next-auth";
import sendEmail from "@/lib/sendEmail"; // Ensure this points to your sendEmail function
import sendEmail from "@/lib/sendEmail";
import prismadb from "./prismadb";

// const prisma = new PrismaClient();

export const NEXT_AUTH_CONFIG: NextAuthOptions = {
providers: [
CredentialsProvider({
Expand All @@ -16,67 +13,29 @@ export const NEXT_AUTH_CONFIG: NextAuthOptions = {
role: { label: "Role", type: "text" },
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.otp || !credentials?.role) {
throw new Error("Invalid credentials");
}
if (!credentials?.email || !credentials?.otp || !credentials?.role) throw new Error("Invalid credentials");

let account;
if (credentials.role === "user") {
account = await prismadb.user.findUnique({
where: { email: credentials.email },
});
} else if (credentials.role === "seller"){
account = await prismadb.seller.findUnique({
where: { email: credentials.email },
});
}else if (credentials.role === "admin"){
account = await prismadb.admin.findUnique({
where: { email: credentials.email },
});
}
else{
return null
}
if (credentials.role === "user") account = await prismadb.user.findUnique({ where: { email: credentials.email } });
else if (credentials.role === "seller") account = await prismadb.seller.findUnique({ where: { email: credentials.email } });
else if (credentials.role === "admin") account = await prismadb.admin.findUnique({ where: { email: credentials.email } });
else return null;

if (!account) {
return null;
}

// Verify OTP
if (credentials.otp !== account.otp) {
// Assuming 'otp' field exists in your User/Seller model
return null;
}
if (!account || credentials.otp !== account.otp) return null;

const updateData = { otp: null };
// Clear OTP after successful login
if (credentials.role === "user") {
await prismadb.user.update({
where: { email: credentials.email },
data: updateData, // Reset OTP or delete it after use
});
} else if(credentials.role === "seller"){
await prismadb.seller.update({
where: { email: credentials.email },
data: updateData, // Reset OTP or delete it after use
});
}else if (credentials.role === "admin"){
await prismadb.admin.update({
where: { email: credentials.email },
data: updateData, // Reset OTP or delete it after use
});
}
else{
return null
}
if (credentials.role === "user") await prismadb.user.update({ where: { email: credentials.email }, data: updateData });
else if (credentials.role === "seller") await prismadb.seller.update({ where: { email: credentials.email }, data: updateData });
else if (credentials.role === "admin") await prismadb.admin.update({ where: { email: credentials.email }, data: updateData });
else return null;

const role = account.role == "user" ? "user" : account.role == "seller"? "seller" : "admin"
const role = account.role === "user" ? "user" : account.role === "seller" ? "seller" : "admin";

return {
id: account.id,
name: account.name,
email: account.email,
role: role
role: role,
};
},
}),
Expand All @@ -86,68 +45,42 @@ export const NEXT_AUTH_CONFIG: NextAuthOptions = {
jwt: async ({ token, user }) => {
if (user) {
token.uid = user.id;
token.role = user.role; // Store role in JWT token
token.role = user.role;
}
return token;
},
session: async ({ session, token }) => {
if (session.user) {
session.user.id = token.uid;
session.user.role = token.role; // Pass role to session
session.user.role = token.role;
}
return session;
},
},
};

// Function to generate and send OTP
export const generateAndSendOTP = async (
email: string,
role: string
) => {
const otp = Math.floor(100000 + Math.random() * 900000).toString(); // Generate 6-digit OTP

// Store OTP in the user or seller record
export const generateAndSendOTP = async (email: string, role: string) => {
const otp = Math.floor(100000 + Math.random() * 900000).toString();

if (role === "user") {
try {
await prismadb.user.update({
where: { email },
data: { otp }, // Ensure 'otp' field exists in your User model
});
await prismadb.user.update({ where: { email }, data: { otp } });
} catch (err) {
console.error(
"DB Error sending OTP for user:",
err instanceof Error ? err.message : err
);
console.error("DB Error sending OTP for user:", err instanceof Error ? err.message : err);
return false;
}
} else if (role === "seller") {
try {
await prismadb.seller.update({
where: { email },
data: { otp }, // Ensure 'otp' field exists in your User model
});
await prismadb.seller.update({ where: { email }, data: { otp } });
} catch (err) {
console.error(
"DB Error sending OTP for seller:",
err instanceof Error ? err.message : err
);
console.error("DB Error sending OTP for seller:", err instanceof Error ? err.message : err);
return false;
}
}

else if (role === "admin") {
} else if (role === "admin") {
try {
await prismadb.admin.update({
where: { email },
data: { otp }, // Ensure 'otp' field exists in your User model
});
await prismadb.admin.update({ where: { email }, data: { otp } });
} catch (err) {
console.error(
"DB Error sending OTP for admin:",
err instanceof Error ? err.message : err
);
console.error("DB Error sending OTP for admin:", err instanceof Error ? err.message : err);
return false;
}
}
Expand All @@ -162,14 +95,8 @@ export const generateAndSendOTP = async (

console.log("OTP email sent successfully:", response);
return true;
// Handle success response if needed (e.g., logging messageId)
} catch (err) {
console.error(
"Error sending OTP:",
err instanceof Error ? err.message : err
);
console.error("Error sending OTP:", err instanceof Error ? err.message : err);
return false;
}
};

// Call generateAndSendOTP(email) before redirecting to the login page to send OTP to the user