next-auth-mfelfelani72 0.0.18 → 0.0.19
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,5 +1,37 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from "next/server";
|
|
2
2
|
export declare function loginHandler(apiUrl: string): (req: NextRequest) => Promise<NextResponse<{
|
|
3
|
+
message: string;
|
|
4
|
+
success: boolean;
|
|
5
|
+
debug: {
|
|
6
|
+
url: string;
|
|
7
|
+
status: number;
|
|
8
|
+
contentType: string | null;
|
|
9
|
+
responsePreview: string;
|
|
10
|
+
};
|
|
11
|
+
}> | NextResponse<{
|
|
12
|
+
message: string;
|
|
13
|
+
success: boolean;
|
|
14
|
+
debug: {
|
|
15
|
+
url: string;
|
|
16
|
+
responseText: string;
|
|
17
|
+
};
|
|
18
|
+
}> | NextResponse<{
|
|
19
|
+
message: string;
|
|
20
|
+
success: boolean;
|
|
21
|
+
user: any;
|
|
22
|
+
}> | NextResponse<{
|
|
23
|
+
message: any;
|
|
24
|
+
success: boolean;
|
|
25
|
+
debug: {
|
|
26
|
+
status: number;
|
|
27
|
+
responseData: any;
|
|
28
|
+
};
|
|
29
|
+
}> | NextResponse<{
|
|
3
30
|
message: any;
|
|
4
31
|
success: boolean;
|
|
32
|
+
debug: {
|
|
33
|
+
errorName: any;
|
|
34
|
+
errorMessage: any;
|
|
35
|
+
stack: any;
|
|
36
|
+
};
|
|
5
37
|
}>>;
|
|
@@ -1,23 +1,100 @@
|
|
|
1
|
-
|
|
2
|
-
* @Author: Mohammad Felfelani
|
|
3
|
-
* @Email: mfelfelani72@gmail.com
|
|
4
|
-
* @Team:
|
|
5
|
-
* @Date: 2025-12-31 06:00:17
|
|
6
|
-
* @Description: Login handler with Next.js native cookies
|
|
7
|
-
*/
|
|
1
|
+
// src/libraries/auth/loginHandler.ts
|
|
8
2
|
import { NextResponse } from "next/server";
|
|
9
3
|
export function loginHandler(apiUrl) {
|
|
10
4
|
return async function POST(req) {
|
|
11
|
-
var _a, _b;
|
|
5
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
6
|
+
console.log("═══════════════════════════════════════");
|
|
7
|
+
console.log("🔐 LOGIN HANDLER STARTED");
|
|
8
|
+
console.log("═══════════════════════════════════════");
|
|
12
9
|
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
|
|
13
15
|
const body = await req.json();
|
|
14
|
-
|
|
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
|
+
const response = await fetch(apiUrl, {
|
|
15
21
|
method: "POST",
|
|
16
|
-
headers: {
|
|
22
|
+
headers: {
|
|
23
|
+
"Content-Type": "application/json",
|
|
24
|
+
"Accept": "application/json",
|
|
25
|
+
},
|
|
17
26
|
body: JSON.stringify(body),
|
|
18
27
|
});
|
|
19
|
-
const
|
|
20
|
-
|
|
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
|
+
const responseText = await response.text();
|
|
38
|
+
console.log(`📝 Response text (first 500 chars):`, responseText.substring(0, 500));
|
|
39
|
+
// 7. بررسی اینکه پاسخ JSON است یا HTML
|
|
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 هم نیست
|
|
60
|
+
return NextResponse.json({
|
|
61
|
+
message: `API returned non-JSON response. Content-Type: ${contentType}`,
|
|
62
|
+
success: false,
|
|
63
|
+
debug: {
|
|
64
|
+
url: apiUrl,
|
|
65
|
+
status: response.status,
|
|
66
|
+
contentType: contentType,
|
|
67
|
+
responsePreview: responseText.substring(0, 200),
|
|
68
|
+
}
|
|
69
|
+
}, { status: 500 });
|
|
70
|
+
}
|
|
71
|
+
// 8. Parsing JSON
|
|
72
|
+
let data;
|
|
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);
|
|
80
|
+
return NextResponse.json({
|
|
81
|
+
message: "Failed to parse JSON response",
|
|
82
|
+
success: false,
|
|
83
|
+
debug: {
|
|
84
|
+
url: apiUrl,
|
|
85
|
+
responseText: responseText.substring(0, 200),
|
|
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');
|
|
21
98
|
const res = NextResponse.json({
|
|
22
99
|
message: "Login successful",
|
|
23
100
|
success: true,
|
|
@@ -29,26 +106,41 @@ export function loginHandler(apiUrl) {
|
|
|
29
106
|
path: "/",
|
|
30
107
|
maxAge: 60 * 60 * 24 * 7,
|
|
31
108
|
};
|
|
32
|
-
//
|
|
109
|
+
// تنظیم کوکیها
|
|
110
|
+
console.log(`🍪 Setting cookies...`);
|
|
33
111
|
res.cookies.set(Object.assign(Object.assign({ name: "accessToken", value: data.data.token.access_token }, cookieOptions), { httpOnly: true }));
|
|
34
|
-
// tokenType
|
|
35
112
|
res.cookies.set(Object.assign(Object.assign({ name: "tokenType", value: data.data.token.token_type || "Bearer" }, cookieOptions), { httpOnly: true }));
|
|
36
|
-
// user
|
|
37
113
|
res.cookies.set(Object.assign(Object.assign({ name: "user", value: JSON.stringify(data.data.user) }, cookieOptions), { httpOnly: false }));
|
|
38
|
-
// isLoggedIn
|
|
39
114
|
res.cookies.set(Object.assign(Object.assign({ name: "isLoggedIn", value: "true" }, cookieOptions), { httpOnly: false }));
|
|
115
|
+
console.log(`✅ Login completed successfully`);
|
|
116
|
+
console.log("═══════════════════════════════════════");
|
|
40
117
|
return res;
|
|
41
118
|
}
|
|
119
|
+
// 11. لاگین ناموفق
|
|
120
|
+
console.log(`❌ Login failed:`, data.message || "Unknown error");
|
|
121
|
+
console.log(`❌ Response data:`, JSON.stringify(data, null, 2));
|
|
42
122
|
return NextResponse.json({
|
|
43
123
|
message: data.message || "Login failed",
|
|
44
124
|
success: false,
|
|
125
|
+
debug: {
|
|
126
|
+
status: response.status,
|
|
127
|
+
responseData: data,
|
|
128
|
+
}
|
|
45
129
|
}, { status: response.status || 401 });
|
|
46
130
|
}
|
|
47
131
|
catch (err) {
|
|
48
|
-
console.error(
|
|
132
|
+
console.error(`❌❌❌ CATCH BLOCK ERROR ❌❌❌`);
|
|
133
|
+
console.error(`Error name: ${err.name}`);
|
|
134
|
+
console.error(`Error message: ${err.message}`);
|
|
135
|
+
console.error(`Error stack:`, err.stack);
|
|
49
136
|
return NextResponse.json({
|
|
50
137
|
message: err.message || "Internal server error",
|
|
51
138
|
success: false,
|
|
139
|
+
debug: {
|
|
140
|
+
errorName: err.name,
|
|
141
|
+
errorMessage: err.message,
|
|
142
|
+
stack: err.stack,
|
|
143
|
+
}
|
|
52
144
|
}, { status: 500 });
|
|
53
145
|
}
|
|
54
146
|
};
|