-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
70 lines (65 loc) · 1.89 KB
/
auth.config.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
import type { NextAuthConfig } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { LOGIN_URL } from "./src/lib/apiEndPoints";
import myAxios from "./src/lib/axios.config";
import { AxiosError } from "axios";
export default {
pages: {
signIn: "/login",
error: "/error",
},
callbacks: {
async jwt({ token, user }) {
if (user) {
return {
...token,
...user,
};
}
return token;
},
async session({ session, token }) {
session.user = token as any;
return session;
},
},
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "text" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
if (!credentials?.email || !credentials?.password) {
throw new Error("Email and password are required");
}
try {
const res = await myAxios.post(LOGIN_URL, {
email: credentials.email,
password: credentials.password,
});
const user = res.data?.user;
if (!user || !user.token) {
throw new Error("Invalid response from server");
}
return user;
} catch (error) {
if (error instanceof AxiosError) {
if (error.response?.status === 404) {
throw new Error("Account not found");
}
if (error.response?.status === 401) {
throw new Error("Invalid credentials");
}
if (error.response?.data?.message) {
throw new Error(error.response.data.message);
}
}
console.error("Auth error:", error);
throw new Error("An error occurred during authentication");
}
},
}),
],
} satisfies NextAuthConfig;