-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
55 lines (47 loc) · 1.61 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
import NextAuth from "next-auth"
import Credentials from "next-auth/providers/credentials"
import { cookies } from "next/headers"
// Your own logic for dealing with plaintext password strings; be careful!
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
// You can specify which fields should be submitted, by adding keys to the `credentials` object.
// e.g. domain, username, password, 2FA token, etc.
credentials: {
email: { label: "email", placeholder: "email" },
password: { label: "password", placeholder: "password" },
},
authorize: async (credentials) => {
if (!credentials) {
return null
}
try {
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/auth/sign_in`, {
method: 'POST',
body: JSON.stringify(credentials),
headers: { "Content-Type": "application/json" }
})
const authorizationHeader = res.headers.get("authorization");
if (res.status === 200 && authorizationHeader) {
const user = await res.json()
cookies().set("authorization", authorizationHeader);
return {
id: user.data.id,
email: user.data.uid,
name: user.data.nickname,
};
} else {
return null;
}
} catch (error) {
console.error("Error authenticating:", error);
return null;
}
// return user object with their profile data
},
}),
],
pages: {
signIn: "/signin",
},
})