next-auth-mfelfelani72 0.0.21 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,37 +1,10 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from "next/server";
|
|
2
2
|
export declare function loginHandler(apiUrl: string): (req: NextRequest) => Promise<NextResponse<{
|
|
3
|
-
message: string;
|
|
4
3
|
success: boolean;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
status: number;
|
|
8
|
-
contentType: string | null;
|
|
9
|
-
responsePreview: string;
|
|
10
|
-
};
|
|
4
|
+
message: any;
|
|
5
|
+
error: any;
|
|
11
6
|
}> | NextResponse<{
|
|
12
|
-
message: string;
|
|
13
7
|
success: boolean;
|
|
14
|
-
debug: {
|
|
15
|
-
url: string;
|
|
16
|
-
responseText: string;
|
|
17
|
-
};
|
|
18
|
-
}> | NextResponse<{
|
|
19
8
|
message: string;
|
|
20
|
-
success: boolean;
|
|
21
9
|
user: any;
|
|
22
|
-
}> | NextResponse<{
|
|
23
|
-
message: any;
|
|
24
|
-
success: boolean;
|
|
25
|
-
debug: {
|
|
26
|
-
status: number;
|
|
27
|
-
responseData: any;
|
|
28
|
-
};
|
|
29
|
-
}> | NextResponse<{
|
|
30
|
-
message: any;
|
|
31
|
-
success: boolean;
|
|
32
|
-
debug: {
|
|
33
|
-
errorName: any;
|
|
34
|
-
errorMessage: any;
|
|
35
|
-
stack: any;
|
|
36
|
-
};
|
|
37
10
|
}>>;
|
|
@@ -2,21 +2,9 @@
|
|
|
2
2
|
import { NextResponse } from "next/server";
|
|
3
3
|
export function loginHandler(apiUrl) {
|
|
4
4
|
return async function POST(req) {
|
|
5
|
-
var _a, _b
|
|
6
|
-
console.log("═══════════════════════════════════════");
|
|
7
|
-
console.log("🔐 LOGIN HANDLER STARTED");
|
|
8
|
-
console.log("═══════════════════════════════════════");
|
|
5
|
+
var _a, _b;
|
|
9
6
|
try {
|
|
10
|
-
// 1. لاگ کردن آدرس
|
|
11
|
-
console.log(`📍 API URL: ${apiUrl}`);
|
|
12
|
-
console.log(`📍 Request method: ${req.method}`);
|
|
13
|
-
console.log(`📍 Request headers:`, Object.fromEntries(req.headers));
|
|
14
|
-
// 2. لاگ کردن body
|
|
15
7
|
const body = await req.json();
|
|
16
|
-
console.log(`📦 Request body:`, JSON.stringify(body, null, 2));
|
|
17
|
-
// 3. ارسال درخواست به API
|
|
18
|
-
console.log(`🚀 Sending request to: ${apiUrl}`);
|
|
19
|
-
const startTime = Date.now();
|
|
20
8
|
const response = await fetch(apiUrl, {
|
|
21
9
|
method: "POST",
|
|
22
10
|
headers: {
|
|
@@ -25,120 +13,52 @@ export function loginHandler(apiUrl) {
|
|
|
25
13
|
},
|
|
26
14
|
body: JSON.stringify(body),
|
|
27
15
|
});
|
|
28
|
-
const endTime = Date.now();
|
|
29
|
-
console.log(`⏱️ Request took: ${endTime - startTime}ms`);
|
|
30
|
-
// 4. لاگ کردن پاسخ
|
|
31
|
-
console.log(`📥 Response status: ${response.status} ${response.statusText}`);
|
|
32
|
-
console.log(`📥 Response headers:`, Object.fromEntries(response.headers));
|
|
33
|
-
// 5. بررسی Content-Type
|
|
34
|
-
const contentType = response.headers.get("content-type");
|
|
35
|
-
console.log(`📄 Content-Type: ${contentType}`);
|
|
36
|
-
// 6. خواندن پاسخ به صورت text اولیه
|
|
37
16
|
const responseText = await response.text();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (!contentType || !contentType.includes("application/json")) {
|
|
41
|
-
console.error(`❌ ERROR: Response is not JSON! Content-Type: ${contentType}`);
|
|
42
|
-
console.error(`❌ Response starts with: ${responseText.substring(0, 100)}`);
|
|
43
|
-
// اگر پاسخ HTML است، احتمالاً خطای 404 یا 500 است
|
|
44
|
-
if (responseText.trim().startsWith('<!DOCTYPE') || responseText.trim().startsWith('<html')) {
|
|
45
|
-
console.error(`❌ Server returned HTML page (probably 404 or 500)`);
|
|
46
|
-
console.error(`❌ URL might be wrong: ${apiUrl}`);
|
|
47
|
-
return NextResponse.json({
|
|
48
|
-
message: `Server returned HTML page instead of JSON. Please check API URL: ${apiUrl}`,
|
|
49
|
-
success: false,
|
|
50
|
-
debug: {
|
|
51
|
-
url: apiUrl,
|
|
52
|
-
status: response.status,
|
|
53
|
-
statusText: response.statusText,
|
|
54
|
-
contentType: contentType,
|
|
55
|
-
responsePreview: responseText.substring(0, 200),
|
|
56
|
-
}
|
|
57
|
-
}, { status: 500 });
|
|
58
|
-
}
|
|
59
|
-
// اگر JSON نیست ولی HTML هم نیست
|
|
17
|
+
// ==== اینجا خطا رو برمیگردونیم ====
|
|
18
|
+
if (responseText.startsWith('<!DOCTYPE') || responseText.startsWith('<html')) {
|
|
60
19
|
return NextResponse.json({
|
|
61
|
-
message: `API returned non-JSON response. Content-Type: ${contentType}`,
|
|
62
20
|
success: false,
|
|
63
|
-
|
|
21
|
+
message: 'API returned HTML instead of JSON',
|
|
22
|
+
error: {
|
|
64
23
|
url: apiUrl,
|
|
65
24
|
status: response.status,
|
|
66
|
-
|
|
67
|
-
responsePreview: responseText.substring(0, 200),
|
|
25
|
+
htmlPreview: responseText.substring(0, 200),
|
|
68
26
|
}
|
|
69
27
|
}, { status: 500 });
|
|
70
28
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
try {
|
|
74
|
-
data = JSON.parse(responseText);
|
|
75
|
-
console.log(`✅ Response parsed as JSON:`, JSON.stringify(data, null, 2).substring(0, 500));
|
|
76
|
-
}
|
|
77
|
-
catch (parseError) {
|
|
78
|
-
console.error(`❌ Failed to parse JSON:`, parseError);
|
|
79
|
-
console.error(`❌ Response text:`, responseText);
|
|
29
|
+
const data = JSON.parse(responseText);
|
|
30
|
+
if (!data.success || !((_b = (_a = data.data) === null || _a === void 0 ? void 0 : _a.token) === null || _b === void 0 ? void 0 : _b.access_token)) {
|
|
80
31
|
return NextResponse.json({
|
|
81
|
-
message: "Failed to parse JSON response",
|
|
82
32
|
success: false,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
}, { status: 500 });
|
|
88
|
-
}
|
|
89
|
-
// 9. بررسی ساختار داده
|
|
90
|
-
console.log(`🔍 Checking response structure...`);
|
|
91
|
-
console.log(` - Has success: ${data.success !== undefined}`);
|
|
92
|
-
console.log(` - Has data: ${data.data !== undefined}`);
|
|
93
|
-
console.log(` - Has token: ${((_a = data.data) === null || _a === void 0 ? void 0 : _a.token) !== undefined}`);
|
|
94
|
-
console.log(` - Has access_token: ${((_c = (_b = data.data) === null || _b === void 0 ? void 0 : _b.token) === null || _c === void 0 ? void 0 : _c.access_token) !== undefined}`);
|
|
95
|
-
// 10. پردازش موفقیتآمیز
|
|
96
|
-
if (data.success && ((_e = (_d = data.data) === null || _d === void 0 ? void 0 : _d.token) === null || _e === void 0 ? void 0 : _e.access_token)) {
|
|
97
|
-
console.log(`✅ Login successful for user:`, ((_f = data.data.user) === null || _f === void 0 ? void 0 : _f.email) || ((_g = data.data.user) === null || _g === void 0 ? void 0 : _g.username) || 'unknown');
|
|
98
|
-
const res = NextResponse.json({
|
|
99
|
-
message: "Login successful",
|
|
100
|
-
success: true,
|
|
101
|
-
user: data.data.user,
|
|
102
|
-
});
|
|
103
|
-
const cookieOptions = {
|
|
104
|
-
secure: process.env.NODE_ENV === "production",
|
|
105
|
-
sameSite: "strict",
|
|
106
|
-
path: "/",
|
|
107
|
-
maxAge: 60 * 60 * 24 * 7,
|
|
108
|
-
};
|
|
109
|
-
// تنظیم کوکیها
|
|
110
|
-
console.log(`🍪 Setting cookies...`);
|
|
111
|
-
res.cookies.set(Object.assign(Object.assign({ name: "accessToken", value: data.data.token.access_token }, cookieOptions), { httpOnly: true }));
|
|
112
|
-
res.cookies.set(Object.assign(Object.assign({ name: "tokenType", value: data.data.token.token_type || "Bearer" }, cookieOptions), { httpOnly: true }));
|
|
113
|
-
res.cookies.set(Object.assign(Object.assign({ name: "user", value: JSON.stringify(data.data.user) }, cookieOptions), { httpOnly: false }));
|
|
114
|
-
res.cookies.set(Object.assign(Object.assign({ name: "isLoggedIn", value: "true" }, cookieOptions), { httpOnly: false }));
|
|
115
|
-
console.log(`✅ Login completed successfully`);
|
|
116
|
-
console.log("═══════════════════════════════════════");
|
|
117
|
-
return res;
|
|
33
|
+
message: data.message || 'Login failed',
|
|
34
|
+
error: data,
|
|
35
|
+
}, { status: 401 });
|
|
118
36
|
}
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
37
|
+
// موفقیت
|
|
38
|
+
const res = NextResponse.json({
|
|
39
|
+
success: true,
|
|
40
|
+
message: "Login successful",
|
|
41
|
+
user: data.data.user,
|
|
42
|
+
});
|
|
43
|
+
res.cookies.set({
|
|
44
|
+
name: "accessToken",
|
|
45
|
+
value: data.data.token.access_token,
|
|
46
|
+
httpOnly: true,
|
|
47
|
+
secure: process.env.NODE_ENV === "production",
|
|
48
|
+
sameSite: "strict",
|
|
49
|
+
path: "/",
|
|
50
|
+
maxAge: 60 * 60 * 24 * 7,
|
|
51
|
+
});
|
|
52
|
+
return res;
|
|
130
53
|
}
|
|
131
54
|
catch (err) {
|
|
132
|
-
|
|
133
|
-
console.error(`Error name: ${err.name}`);
|
|
134
|
-
console.error(`Error message: ${err.message}`);
|
|
135
|
-
console.error(`Error stack:`, err.stack);
|
|
55
|
+
// ==== هر خطای دیگه ====
|
|
136
56
|
return NextResponse.json({
|
|
137
|
-
message: err.message || "Internal server error",
|
|
138
57
|
success: false,
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
58
|
+
message: err.message || 'Internal server error',
|
|
59
|
+
error: {
|
|
60
|
+
name: err.name,
|
|
61
|
+
message: err.message,
|
|
142
62
|
stack: err.stack,
|
|
143
63
|
}
|
|
144
64
|
}, { status: 500 });
|