-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
54 lines (43 loc) · 1.23 KB
/
auth.js
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 NextAuth from "next-auth";
import google from "next-auth/providers/google";
import connectToDB from "@utils/database";
import User from "@models/user";
const authOption = {
providers: [
google({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
secret:process.env.NEXTAUTH_SECRET,
callbacks: {
async session({ session }) {
try {
await connectToDB();
const sessionUser = await User.findOne({ email: session.user.email });
session.user.id = sessionUser._id.toString();
return session;
} catch (e) {
console.log("ERROR IN SESSION", e);
}
},
async signIn({ profile }) {
try {
const res=await connectToDB();
console.log('res database',res);
const userExists = await User.findOne({ email: profile.email });
if (!userExists) {
await User.create({
email: profile.email,
username: profile.name.replace(" ", "").toLowerCase(),
image: profile.picture,
});
}
return true;
} catch (e) {
console.log("ERROR IN SINGIN", e);
}
},
},
};
export const { auth, handlers} = NextAuth(authOption);