-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
65 lines (60 loc) · 1.82 KB
/
auth.ts
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
import { connectDb } from "@/db/dbConfig";
import User from "@/model/user.model";
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Google({
clientId: process.env.AUTH_GOOGLE_ID,
clientSecret: process.env.AUTH_GOOGLE_SECRET,
}),
],
secret: process.env.AUTH_SECRET,
// debug: true,
callbacks: {
async jwt({ token, user }) {
// When user signs in, persist their ID in the JWT token
if (user) {
token.id = user.id;
}
return token;
},
async redirect({ url, baseUrl }) {
// If the user is signing in, redirect to '/home'
if (url === '/api/auth/') {
return `${baseUrl}/home`;
}
// Allows relative callback URLs
if (url.startsWith("/")) return `${baseUrl}${url}`;
// Allows callback URLs on the same origin
if (new URL(url).origin === baseUrl) return url;
// Default redirect to the baseUrl
return baseUrl;
},
async session({ session, token }) {
// Add the user ID from the token to the session object
if (token) {
session.user.id = token.id as string;
}
return session;
},
async signIn({ user, account, profile }) {
try {
await connectDb(); // Ensure the database is connected
let dbUser = await User.findOne({ email: user.email });
if (!dbUser) {
dbUser = await User.create({
name: user.name,
email: user.email,
avatar: user.image,
socialAuthentication: true,
});
}
return true; // Allow the sign-in
} catch (error) {
console.error("Sign-in error:", error);
return false; // Reject the sign-in on error
}
},
},
});