-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #749 from ShivanshPlays/seller-login
login for seller feature
- Loading branch information
Showing
9 changed files
with
124 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use client" | ||
import { useSession } from "next-auth/react"; | ||
|
||
const Dashboard = () => { | ||
|
||
const session= useSession(); | ||
|
||
return ( | ||
<> | ||
{session.data?.user.role} | ||
{session.data?.user.name} | ||
</> | ||
); | ||
} | ||
|
||
export default Dashboard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { NextResponse } from 'next/server'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import bcrypt from 'bcrypt'; | ||
|
||
const prisma = new PrismaClient(); | ||
|
||
export async function POST(request: Request) { | ||
const { email, password , mobileNumber,name } = await request.json(); | ||
|
||
// Check if the user already exists | ||
const existingUser = await prisma.user.findUnique({ | ||
where: { email }, | ||
}); | ||
|
||
if (existingUser) { | ||
return NextResponse.json({ message: 'Seller already exists' }, { status: 400 }); | ||
} | ||
|
||
// Hash the password | ||
const passwordHash = await bcrypt.hash(password, 10); | ||
|
||
// Create the user | ||
const user = await prisma.seller.create({ | ||
data: { | ||
email, | ||
passwordHash, | ||
mobileNumber, | ||
name | ||
}, | ||
}); | ||
|
||
|
||
|
||
return NextResponse.json({ message: 'Signup successful!',user }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,60 @@ | ||
import { PrismaClient } from "@prisma/client"; | ||
import CredentialsProvider from "next-auth/providers/credentials"; | ||
import bcrypt from "bcrypt"; | ||
import { NextAuthOptions } from "next-auth"; | ||
|
||
import { DefaultUser, Session } from "next-auth"; | ||
import { JWT } from "next-auth/jwt"; | ||
const prisma = new PrismaClient(); | ||
|
||
|
||
export const NEXT_AUTH_CONFIG = { | ||
export const NEXT_AUTH_CONFIG: NextAuthOptions = { | ||
providers: [ | ||
CredentialsProvider({ | ||
name: "Credentials", | ||
credentials: { | ||
email: { label: "email", type: "text", placeholder: "" }, | ||
password: { label: "password", type: "password", placeholder: "" }, | ||
email: { label: "Email", type: "text" }, | ||
password: { label: "Password", type: "password" }, | ||
}, | ||
async authorize(credentials:Record<"email" | "password", string> | undefined) { | ||
|
||
async authorize(credentials) { | ||
if (!credentials?.email || !credentials?.password) { | ||
throw new Error("Invalid credentials"); | ||
} | ||
|
||
const prisma = new PrismaClient(); | ||
// console.log(credentials); | ||
const user = await prisma.user.findUnique({ | ||
where: { | ||
email: credentials.email, | ||
}, | ||
}); | ||
if (!user || !user.passwordHash) { | ||
// return NextResponse.json({msg:"invalid email, not found in DB"},{status:403}) | ||
console.log("invalid email, not found in DB"); | ||
return null; // Return null for invalid login | ||
const user = await prisma.user.findUnique({ where: { email: credentials.email } }); | ||
const seller = await prisma.seller.findUnique({ where: { email: credentials.email } }); | ||
|
||
const account = user || seller; | ||
if (!account || !account.passwordHash) { | ||
return null; | ||
} | ||
const isVerified = await bcrypt.compare( | ||
credentials.password, | ||
user.passwordHash | ||
); | ||
|
||
const isVerified = await bcrypt.compare(credentials.password, account.passwordHash); | ||
if (!isVerified) { | ||
console.log("Invalid password"); | ||
return null; // Return null if password doesn't match | ||
return null; | ||
} | ||
return { | ||
id: user.id, // Ensure id is included | ||
name: user.name, | ||
email: user.email, | ||
|
||
return { | ||
id: account.id, | ||
name: account.name, | ||
email: account.email, | ||
role: user ? "user" : "seller", | ||
}; | ||
}, | ||
}), | ||
], | ||
secret: process.env.NEXTAUTH_SECRET, | ||
callbacks: { | ||
jwt: async ({ user, token }:{user:DefaultUser,token:JWT}) => { | ||
if (user) { | ||
token.uid = user.id; | ||
} | ||
return token; | ||
}, | ||
session: ({ session, token }:{session:Session,token:JWT}) => { | ||
// if (session.user) { | ||
|
||
// session.user.id = token.uid | ||
// } | ||
if (session.user) { | ||
|
||
return { ...session, | ||
user: { ...session.user, | ||
id: token.uid, | ||
} | ||
} | ||
} | ||
return session | ||
} | ||
}, | ||
secret: process.env.NEXTAUTH_SECRET, | ||
callbacks: { | ||
jwt: async ({ token, user }) => { | ||
if (user) { | ||
token.uid = user.id; | ||
token.role = user.role; // Store role in JWT token | ||
} | ||
return token; | ||
}, | ||
session: async ({ session, token }) => { | ||
if (session.user) { | ||
session.user.id = token.uid; | ||
session.user.role = token.role; // Pass role to session | ||
} | ||
return session; | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
import type { DefaultSession } from "next-auth"; | ||
// next-auth.d.ts | ||
import NextAuth, { DefaultUser, Session } from "next-auth"; | ||
import { JWT } from "next-auth/jwt"; | ||
|
||
declare module "next-auth" { | ||
interface Session { | ||
user: DefaultSession["user"] & { | ||
user: { | ||
id: string; | ||
name?: string; | ||
email?: string; | ||
role: "user" | "seller"; // Extend with the role property | ||
}; | ||
} | ||
|
||
interface User extends DefaultUser { | ||
role: "user" | "seller"; // Add role to User object | ||
} | ||
} | ||
|
||
declare module "next-auth/jwt" { | ||
interface JWT { | ||
uid: string; | ||
role: "user" | "seller"; // Add role to JWT token | ||
} | ||
} |