-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
61 lines (59 loc) · 1.9 KB
/
Copy pathauth.js
File metadata and controls
61 lines (59 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import Google from "next-auth/providers/google";
import { connectDB } from "@/lib/mongodb";
import User from "@/models/User";
import bcrypt from "bcryptjs";
function generateReferralCode(name) {
const cleanName = name.replace(/[^a-zA-Z]/g, "").toUpperCase().slice(0, 5);
const randomNum = Math.floor(1000 + Math.random() * 9000);
return `${cleanName}${randomNum}`;
}
export const { handlers, signIn, signOut, auth } = NextAuth({
trustHost: true,
providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
Credentials({
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
await connectDB();
const user = await User.findOne({ email: credentials.email });
if (!user) throw new Error("No user found");
const isValid = await bcrypt.compare(credentials.password, user.password);
if (!isValid) throw new Error("Invalid password");
return { id: user._id.toString(), name: user.name, email: user.email };
},
}),
],
callbacks: {
async signIn({ user, account }) {
if (account?.provider === "google") {
await connectDB();
const existing = await User.findOne({ email: user.email });
if (!existing) {
// Create new user from Google login
await User.create({
name: user.name,
email: user.email,
password: null,
referralCode: generateReferralCode(user.name),
bonusGenerations: 0,
});
}
return true;
}
return true;
},
},
pages: {
signIn: "/login",
},
session: { strategy: "jwt" },
secret: process.env.NEXTAUTH_SECRET,
});