Skip to content

Commit 7086e9f

Browse files
authored
Login Feature (#135)
1 parent 263c13b commit 7086e9f

File tree

15 files changed

+746
-247
lines changed

15 files changed

+746
-247
lines changed

.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66
CRATES_PRO_HOST=http://210.28.134.203:31688
77
CRATES_PRO_INTERNAL_HOST=http://210.28.134.203:31688
88

9-
SECRET_KEY=$YOUR_SECRET_KEY #(not prefixed with NEXT_PUBLIC_ )
9+
# SECRET_KEY=$YOUR_SECRET_KEY #(not prefixed with NEXT_PUBLIC_ )
1010

11+
12+
AUTH_HOST=true
Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,67 @@
1-
import { handlers } from "@/app/auth" // Referring to the auth.ts we just created
2-
export const { GET, POST } = handlers
1+
import { handlers } from "@/app/auth"
2+
import { NextResponse } from 'next/server'
3+
import { NextRequest } from 'next/server'
4+
import { getProxyConfig } from "@/proxy-config"
5+
6+
const TIMEOUT_DURATION = 60000;
7+
8+
// 定义超时 Promise 的类型
9+
type TimeoutPromise = Promise<never>
10+
11+
export async function GET(request: NextRequest): Promise<Response> {
12+
try {
13+
const { isEnabled } = getProxyConfig();
14+
15+
if (isEnabled) {
16+
// 使用超时机制
17+
const timeoutPromise: TimeoutPromise = new Promise((_, reject) =>
18+
setTimeout(() => reject(new Error('Request timeout')), TIMEOUT_DURATION)
19+
);
20+
const response = await Promise.race([handlers.GET(request), timeoutPromise]);
21+
return response instanceof Response ? response : NextResponse.json(response);
22+
} else {
23+
// 不使用超时机制
24+
const response = await handlers.GET(request);
25+
return response instanceof Response ? response : NextResponse.json(response);
26+
}
27+
} catch (error) {
28+
console.error('Auth GET Error:', error);
29+
return NextResponse.json(
30+
{
31+
error: 'Authentication error',
32+
details: error instanceof Error ? error.message : 'Unknown error',
33+
timestamp: new Date().toISOString()
34+
},
35+
{ status: 500 }
36+
);
37+
}
38+
}
39+
40+
export async function POST(request: NextRequest): Promise<Response> {
41+
try {
42+
const { isEnabled } = getProxyConfig();
43+
44+
if (isEnabled) {
45+
// 使用超时机制
46+
const timeoutPromise: TimeoutPromise = new Promise((_, reject) =>
47+
setTimeout(() => reject(new Error('Request timeout')), TIMEOUT_DURATION)
48+
);
49+
const response = await Promise.race([handlers.POST(request), timeoutPromise]);
50+
return response instanceof Response ? response : NextResponse.json(response);
51+
} else {
52+
// 不使用超时机制
53+
const response = await handlers.POST(request);
54+
return response instanceof Response ? response : NextResponse.json(response);
55+
}
56+
} catch (error) {
57+
console.error('Auth POST Error:', error);
58+
return NextResponse.json(
59+
{
60+
error: 'Authentication error',
61+
details: error instanceof Error ? error.message : 'Unknown error',
62+
timestamp: new Date().toISOString()
63+
},
64+
{ status: 500 }
65+
);
66+
}
67+
}

app/api/profile/route.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { NextResponse } from 'next/server';
2+
3+
export async function POST(request: Request) {
4+
const endpoint = process.env.CRATES_PRO_INTERNAL_HOST;
5+
const apiUrl = `${endpoint}/api/profile`;
6+
const requestBody = await request.json();
7+
console.log("Request Body:", requestBody);
8+
9+
try {
10+
const response = await fetch(apiUrl, {
11+
method: 'POST',
12+
headers: {
13+
'Content-Type': 'application/json', // 确保发送 JSON
14+
},
15+
body: JSON.stringify({
16+
requestBody
17+
}), // 发送请求体
18+
});
19+
console.log("response in get profile info:", response);
20+
if (!response.ok) {
21+
return NextResponse.json({ error: 'Failed to submit data' }, { status: response.status });
22+
}
23+
24+
const result = await response.json(); // 确认返回的是 JSON 格式
25+
console.log("results:", result);
26+
return NextResponse.json({ message: 'Submission successful', code: 2012, crates: result });
27+
} catch (error) {
28+
console.log(error);
29+
return NextResponse.json({ error: 'An error occurred while submitting data.' }, { status: 500 });
30+
}
31+
}

app/api/submit/route.ts renamed to app/api/submitCrate/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { NextResponse } from 'next/server';
22

33
export async function POST(request: Request) {
4-
const apiUrl = process.env.API_URL; // 读取环境变量
4+
const endpoint = process.env.CRATES_PRO_INTERNAL_HOST;
5+
const apiUrl = `${endpoint}/api/submitCrate`;
56
const formData = await request.formData(); // 解析请求体
67

78
console.log("Request FormData:", formData);

app/api/submitUserinfo/route.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { NextResponse } from 'next/server';
2+
3+
4+
export async function POST(request: Request) {
5+
const endpoint = process.env.CRATES_PRO_INTERNAL_HOST;
6+
const apiUrl = `${endpoint}/api/submitUserinfo`;
7+
const requestBody = await request.json();
8+
9+
10+
11+
console.log("Request Body new!:", requestBody);
12+
try {
13+
const response = await fetch(apiUrl, {
14+
method: 'POST',
15+
headers: {
16+
'Content-Type': 'application/json', // 确保发送 JSON
17+
'Accept': 'application/json'
18+
},
19+
body: JSON.stringify({
20+
requestBody
21+
}), // 发送 name 字段
22+
});
23+
console.log("response:", response);
24+
if (!response.ok) {
25+
return NextResponse.json({ error: 'Failed to submit data' }, { status: response.status });
26+
}
27+
28+
const result = await response.json(); // 确认返回的是 JSON 格式
29+
console.log("results:", result);
30+
return NextResponse.json({ message: 'Submission successful', code: 2012, data: result });
31+
} catch (error) {
32+
console.log(error);
33+
return NextResponse.json({ error: 'An error occurred while submitting data.' }, { status: 500 });
34+
}
35+
}

app/auth.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,65 @@
1-
21
import NextAuth from "next-auth"
32
import GitHub from "next-auth/providers/github"
3+
import { setupGlobalFetch } from "@/proxy-config"
4+
5+
// 设置全局代理(如果启用)
6+
setupGlobalFetch();
7+
8+
// 打印环境变量(开发环境调试用)
9+
console.log('Environment Config:', {
10+
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
11+
PROXY_ENABLED: !!process.env.HTTPS_PROXY,
12+
PROXY_URL: process.env.HTTPS_PROXY || 'Not configured'
13+
});
14+
15+
if (!process.env.AUTH_GITHUB_ID || !process.env.AUTH_GITHUB_SECRET) {
16+
throw new Error('Missing GITHUB_ID or GITHUB_SECRET environment variable')
17+
}
418

519
export const { handlers, signIn, signOut, auth } = NextAuth({
6-
providers: [GitHub],
7-
})
20+
providers: [
21+
GitHub({
22+
clientId: process.env.AUTH_GITHUB_ID,
23+
clientSecret: process.env.AUTH_GITHUB_SECRET,
24+
authorization: {
25+
params: {
26+
prompt: "consent",
27+
access_type: "offline",
28+
response_type: "code"
29+
}
30+
}
31+
})
32+
],
33+
trustHost: true,
34+
debug: true,
35+
callbacks: {
36+
async signIn({ user, account, profile, email, credentials }) {
37+
console.log('Sign in attempt:', { user, account, profile, email, credentials });
38+
39+
return true;
40+
},
41+
async redirect({ url, baseUrl }) {
42+
console.log('Redirect:', { url, baseUrl });
43+
return baseUrl;
44+
},
45+
async session({ session, user, token }) {
46+
console.log('Session:', { session, user, token });
47+
return session;
48+
},
49+
async jwt({ token, user, account, profile }) {
50+
console.log('JWT:', { token, user, account, profile });
51+
return token;
52+
}
53+
},
54+
events: {
55+
async signIn(message) { console.log('signIn:', message) },
56+
async signOut(message) { console.log('signOut:', message) },
57+
},
58+
pages: {
59+
signIn: '/auth/signin',
60+
error: '/auth/error',
61+
}
62+
})
63+
64+
65+

app/lib/all_interface.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,49 @@ export interface cveListInfo {
106106
"end_version": string,
107107
},
108108
]
109-
}
109+
}
110+
111+
//六、用户github信息接口
112+
export interface userinfo {
113+
user: {
114+
"email": string,
115+
"image": string,
116+
"name": string,
117+
}
118+
expires: string
119+
}
120+
121+
122+
//用户个人主页接口
123+
export interface profile {
124+
upload_crates:
125+
[
126+
{
127+
"name": string,
128+
"time": string,
129+
},
130+
{
131+
"name": string,
132+
"time": string,
133+
},
134+
]
135+
136+
}
137+
138+
export interface profileResult {
139+
140+
"data": [
141+
{
142+
"name": string,
143+
"time": string,
144+
},
145+
{
146+
"name": string,
147+
"time": string,
148+
},
149+
]
150+
151+
152+
}
153+
154+
File renamed without changes.

0 commit comments

Comments
 (0)