-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.js
53 lines (50 loc) · 1.15 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
import connectDB from "@/db/dbConnect";
import { UserSchema } from "@/db/schema";
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
session: {
jwt: true
},
pages: {
signIn: "/login",
},
providers: [
GoogleProvider({
clientId:process.env.GOOGLE_CLIENT_ID,
clientSecret:process.env.GOOGLE_CLIENT_SECRET,
})
],
callbacks: {
async signIn({ user, account, profile, email }) {
try{
await connectDB();
const return_user = await UserSchema.findOne({email: user.email});
if (!return_user){
const new_user = await UserSchema.create({email: user.email, name: user.name})
console.log({new_user});
}
return true;
}catch (error){
console.log(error);
return false;
}
},
async jwt({ token, user }) {
if (user) {
token.id = user.id;
}
return token;
},
async session({ session, token }) {
session.user.id = token.id;
return session;
},
},
secret: process.env.NEXT_PUBLIC_SECRET
})