Skip to content

Commit

Permalink
fix: nextauth route and eslint config
Browse files Browse the repository at this point in the history
  • Loading branch information
nawinsharma committed May 11, 2024
1 parent 1faa03c commit 64a701a
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 196 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "next/core-web-vitals"
}
}
67 changes: 1 addition & 66 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,6 @@
import { authOptions } from "@/utils/authOptions";
import NextAuth from "next-auth";
import { Account, User as AuthUser } from "next-auth";
import GithubProvider from "next-auth/providers/github";
import CredentialsProvider from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
import User from "@/models/User";
import connect from "@/utils/db";

export const authOptions: any = {
// Configure one or more authentication providers
providers: [
CredentialsProvider({
id: "credentials",
name: "Credentials",
credentials: {
email: { label: "Email", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials: any) {
await connect();
try {
const user = await User.findOne({ email: credentials.email });
if (user) {
const isPasswordCorrect = await bcrypt.compare(
credentials.password,
user.password
);
if (isPasswordCorrect) {
return user;
}
}
} catch (err: any) {
throw new Error(err);
}
},
}),
GithubProvider({
clientId: process.env.GITHUB_ID ?? "",
clientSecret: process.env.GITHUB_SECRET ?? "",
}),
// ...add more providers here
],
callbacks: {
async signIn({ user, account }: { user: AuthUser; account: Account }) {
if (account?.provider == "credentials") {
return true;
}
if (account?.provider == "github") {
await connect();
try {
const existingUser = await User.findOne({ email: user.email });
if (!existingUser) {
const newUser = new User({
email: user.email,
});

await newUser.save();
return true;
}
return true;
} catch (err) {
console.log("Error saving user", err);
return false;
}
}
},
},
};

const handler = NextAuth(authOptions);

Expand Down
Loading

0 comments on commit 64a701a

Please sign in to comment.