AUTHMODE BABAY
This commit is contained in:
70
auth/index.ts
Normal file
70
auth/index.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { PrismaAdapter } from "@auth/prisma-adapter";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import NextAuth from "next-auth";
|
||||
import Credentials from "next-auth/providers/credentials";
|
||||
import Discord from "next-auth/providers/discord";
|
||||
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
export const { handlers, signIn, signOut, auth } = NextAuth({
|
||||
providers: [
|
||||
Discord({
|
||||
clientId: process.env.DISCORD_CLIENT_ID,
|
||||
clientSecret: process.env.DISCORD_CLIENT_SECRET,
|
||||
}),
|
||||
Credentials({
|
||||
credentials: {
|
||||
email: {},
|
||||
password: {},
|
||||
},
|
||||
authorize: async (credentials) => {
|
||||
let user = null;
|
||||
|
||||
const pwHash = await saltAndHashPassword(
|
||||
credentials.password as string
|
||||
);
|
||||
user = await prisma.user.findFirst({
|
||||
where: {
|
||||
email: credentials.email as string,
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
image: true,
|
||||
email: true,
|
||||
emailVerified: true,
|
||||
username: true,
|
||||
passwordHash: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
user = await prisma.user.create({
|
||||
data: {
|
||||
email: credentials.email as string,
|
||||
passwordHash: pwHash,
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
image: true,
|
||||
email: true,
|
||||
emailVerified: true,
|
||||
username: true,
|
||||
},
|
||||
});
|
||||
return user;
|
||||
}
|
||||
|
||||
user.passwordHash = null;
|
||||
|
||||
return user;
|
||||
},
|
||||
}),
|
||||
],
|
||||
adapter: PrismaAdapter(prisma),
|
||||
});
|
||||
async function saltAndHashPassword(password: string) {
|
||||
const hash = await bcrypt.hash(password, 10);
|
||||
return hash;
|
||||
}
|
Reference in New Issue
Block a user