-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
116 lines (111 loc) · 3.2 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import type { Provider } from "next-auth/providers";
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import GitHub from "next-auth/providers/github";
import Credentials from "next-auth/providers/credentials";
const providers: Provider[] = [
Google,
GitHub,
Credentials({
credentials: {
email: {},
password: {}
},
authorize: async (credentials) => {
try {
let user = null;
console.log(credentials.email)
console.log(credentials.password)
// logic to verify if user exists
const res = await fetch(`${process.env.API_URL}/v1/api/sessions`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ email: credentials.email, password: credentials.password, provider: "credentials" }),
})
if (!res.ok) {
const errors = await res.json();
console.log(errors);
throw new Error();
}
user = await res.json()
console.log(user)
// return user object with the their profile data
return user
} catch (error) {
return null
}
},
})
];
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, signIn, signOut, auth } = NextAuth({
providers,
callbacks: {
jwt({ token, user, account }) {
if (user) {
token.email = user.email
token.provider = account?.provider
}
return token;
},
async session({ session, token }) {
try {
const res = await fetch(`${process.env.API_URL}/v1/api/users/show_by_email_and_provider`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ email: token.email, provider: token.provider })
});
const data = await res.json();
session.user.id = data.id;
} catch (error) {
console.log(error);
}
return session
},
async signIn({ user, account }) {
if (account?.provider === "credentials") {
return true;
}
const uid = account?.providerAccountId;
const name = user.name;
const email = user.email;
const image = user.image;
const provider = account?.provider;
try {
const res = await fetch(`${process.env.API_URL}/v1/api/auth/${provider}/callback`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ uid, name, email, image, provider }),
});
if (!res.ok) {
const errors = await res.json();
throw new Error(errors.message);
} else {
return true
}
} catch (error) {
console.log(error);
return false
}
}
},
pages: {
signIn: "/login",
},
});