-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.js
84 lines (79 loc) · 2.27 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
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
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import {
SignInWithEmailAndPassword,
oauthSignIn,
} from "@/actions/user/user.actions";
const providers = [
Google,
GitHub,
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const user = await SignInWithEmailAndPassword(credentials); // User verification logic
if (user.status === 400) {
return null;
}
return user.data;
},
}),
];
export const providerMap = providers.map((provider) => {
if (typeof provider === "function") {
const providerData = provider();
return { id: providerData.id, name: providerData.name };
} else {
return { id: provider.id, name: provider.name };
}
});
export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
providers,
pages: {
signIn: "/auth/sign-in",
},
callbacks: {
async signIn({ user, account }) {
if (account.provider === "google" || account.provider === "github") {
const { name, email, image } = user;
const payload = {
name,
email,
avatar: image,
authType: "Oauth",
};
const res = await oauthSignIn(payload);
user.id = res.data._id.toString();
user.isVerified = res.data.isVerified;
user.image = res.data.avatar;
return user;
}
// Default to allow sign-in
return user;
},
async jwt({ token, user }) {
if (user) {
const id = user._id?.toString() || user.id;
token.id = id;
token.email = user.email;
token.name = user.name;
token.isVerified = user.isVerified;
token.picture = user.avatar || user.image;
}
return token;
},
async session({ session, token, newSession }) {
session.user.id = token.id;
session.user.email = token.email;
session.user.name = token.name;
session.user.image = token.picture;
session.user.isVerified = token.isVerified;
return session;
},
},
});