-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathauth.ts
More file actions
99 lines (89 loc) · 2.67 KB
/
auth.ts
File metadata and controls
99 lines (89 loc) · 2.67 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import { sql } from "@vercel/postgres";
import bcrypt from "bcrypt";
export const authOptions: NextAuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.AUTH_GOOGLE_ID!,
clientSecret: process.env.AUTH_GOOGLE_SECRET!,
}),
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) {
return null;
}
try {
// Find user in database
const result = await sql`
SELECT * FROM users WHERE google_email = ${credentials.email}
`;
if (result.rows.length === 0) {
return null;
}
const user = result.rows[0];
// Verify password
const isValid = await bcrypt.compare(credentials.password, user.password);
if (!isValid) {
return null;
}
// Update last login
await sql`
UPDATE users SET last_login = CURRENT_TIMESTAMP WHERE id = ${user.id}
`;
// Return only the basic user information
return {
id: user.id.toString(),
email: user.google_email,
name: user.name || user.google_email
};
} catch (error) {
console.error("Auth error:", error);
return null;
}
}
})
],
secret: process.env.NEXTAUTH_SECRET,
callbacks: {
async signIn({ user, account }) {
if (account?.provider === "google") {
try {
const response = await fetch(
`${process.env.APP_URL}/api/database/registration`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
googleEmail: user.email,
googleSub: account.providerAccountId,
lastLogin: new Date().toISOString(),
}),
}
);
if (!response.ok) {
console.error(
"There was an error creating the user:",
await response.text()
);
return false;
}
} catch (error) {
console.error("Error in the signIn callback:", error);
return false;
}
}
return true;
}
},
pages: {
signIn: '/login'
}
};
export default authOptions;