-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.config.ts
More file actions
54 lines (47 loc) · 1.53 KB
/
Copy pathauth.config.ts
File metadata and controls
54 lines (47 loc) · 1.53 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
import { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
import { getUserByEmail } from "./lib/data/user";
import { LoginSchema } from "./schemas";
export default {
providers: [
Credentials({
async authorize(credentials, request) {
const validatedCredentials = LoginSchema.safeParse(credentials);
if (!validatedCredentials.success) {
return null;
}
const { email, password } = validatedCredentials.data;
// console.log("password", password)
const user = await getUserByEmail(email);
if (!user || !user.password) {
return null;
}
const passwordsMatch = await bcrypt.compare(password, user.password);
if (passwordsMatch) {
// Normalize nullable fields to match NextAuth User type expectations
return {
...user,
provider: user.provider ?? undefined,
image: user.image ?? null,
} as any;
}
return null;
},
}),
],
pages: {
signIn: "/sign-in",
},
callbacks: {
redirect: ({ url, baseUrl }) => {
// Allows relative callback URLs
if (url.startsWith("/")) return `${baseUrl}${url}`;
// After sign in, redirect to dashboard
else if (url === baseUrl) return `${baseUrl}/dashboard`;
// Allows callback URLs on the same origin
else if (new URL(url).origin === baseUrl) return url;
return baseUrl;
},
},
} satisfies NextAuthConfig;