Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import NextAuth from "next-auth";
import { authOptions } from "../../lib/auth";
import { authOptions } from "../../../lib/auth";

const handler = NextAuth(authOptions);

Expand Down
129 changes: 120 additions & 9 deletions apps/X/app/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import db from "@repo/db/client";
import bcrypt from "bcrypt";
import { Session } from "inspector/promises";
import { JWT } from "next-auth/jwt";

import CredentialsProvider from "next-auth/providers/credentials";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import z from "zod";

interface credentialsTypes {
name: string;
username: string;
password: string;
email: string;
name: string;
}



const userInput = z.object({
username: z.string(),
name: z.string(),
email: z.string().email(),
password: z.string(),
});

export const authOptions = {
providers: [
Expand All @@ -25,21 +36,121 @@ export const authOptions = {
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "username", type: "text", placeholder: "" },
email: { label: "email", type: "text", placeholder: "" },
username: { label: "Username", type: "text", placeholder: "" },
name: { label: "Name", type: "text", placeholder: "" },
email: { label: "Email", type: "text", placeholder: "" },
password: { label: "Password", type: "password", placeholder: "" },
},

async authorize(credentials, req) {
async authorize(credentials) {
if (!credentials) return null;
const { username, email, password } = credentials;
return {
id: "user",
const { username, email, password, name } = credentials;

const validatedUserInput = (creds: credentialsTypes) => {
return userInput.safeParse({
username: creds.username,
email: creds.email,
password: creds.password,
name: creds.name,
});
};

console.log(credentials, "User credentilas");

if (!validatedUserInput) return null;

const hashedPassword = await bcrypt.hash(credentials.password, 10);
console.log(hashedPassword);

const existingUser = await db.user.findFirst({
where: {
username: credentials.username,
email: credentials.email,
},
});

if (existingUser) {
try {
const passwordValidation = await bcrypt.compare(
credentials.password,
existingUser.password
);
if (passwordValidation) {
return {
id: existingUser?.id.toString(),
usernname: existingUser.username,
email: existingUser.email,
name: existingUser.name,
};
}
} catch (error) {
console.log(error, "Error while LogIn");
}
return null;
}

try {
const user = await db.user.create({
data: {
username: credentials.username,
email: credentials.email,
name: credentials.name,
password: hashedPassword,
},
});
return {
id: user.id.toString(),
name: user.name,
username: user.username,
email: user.email,
};
} catch (error) {
console.log(error, "Not able to Create new user");
}

return null;
},
}),
],
Secret: process.env.NEXTAUTH_SECRET || "secr3t",

callbacks: {
async session({
token,
session,
}: {
token: JWT;
session: Session & { user: { id: string | undefined } };
}) {
session.user.id = token.sub;
return session;
},
//},

async signIn({ user, account, profile }: any) {
if (account?.privider === "github" || account?.provider === "google") {
const existingUser = await db.user.findUnique({
where: { email: user.email },
});
if (!existingUser) {
try {
await db.user.create({
data: {
email: user.email,
name: user.name,
username: user.email.split("@")[0],
password: "",
},
});
} catch (error) {
console.log("Error while creating user from Github", error);
return false;
}
}
return true;
}
},
},
page: {
signIn: "/auth",
},
Expand Down
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@
"workspaces": [
"apps/*",
"packages/*"
]
],
"dependencies": {
"@types/bcrypt": "^5.0.2",
"bcrypt": "^5.1.1"
}
}
Loading
Loading