Spaces:
Sleeping
Sleeping
Commit ·
96afe70
1
Parent(s): e0cec61
bug fix 20
Browse files
src/app/api/user/github/[username]/route.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* GitHub User Lookup API Route
|
| 3 |
+
*
|
| 4 |
+
* GET /api/user/github/[username]
|
| 5 |
+
* Fetches GitHub user profile data using server-side token to avoid client rate limits
|
| 6 |
+
*/
|
| 7 |
+
|
| 8 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 9 |
+
import { getCurrentUser } from "@/lib/auth";
|
| 10 |
+
import { Octokit } from "@octokit/rest";
|
| 11 |
+
|
| 12 |
+
export async function GET(
|
| 13 |
+
request: NextRequest,
|
| 14 |
+
context: { params: Promise<{ username: string }> }
|
| 15 |
+
) {
|
| 16 |
+
try {
|
| 17 |
+
const user = await getCurrentUser(request);
|
| 18 |
+
if (!user) {
|
| 19 |
+
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
const { username } = await context.params;
|
| 23 |
+
|
| 24 |
+
if (!username) {
|
| 25 |
+
return NextResponse.json({ error: "Username is required" }, { status: 400 });
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
// Use the authenticated user's GitHub token to fetch the profile
|
| 29 |
+
// This avoids rate limits that affect unauthenticated requests
|
| 30 |
+
if (!user.githubAccessToken) {
|
| 31 |
+
return NextResponse.json({ error: "GitHub token not available" }, { status: 400 });
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
const octokit = new Octokit({ auth: user.githubAccessToken });
|
| 35 |
+
|
| 36 |
+
try {
|
| 37 |
+
const { data: githubUser } = await octokit.users.getByUsername({
|
| 38 |
+
username: username,
|
| 39 |
+
});
|
| 40 |
+
|
| 41 |
+
return NextResponse.json({
|
| 42 |
+
login: githubUser.login,
|
| 43 |
+
username: githubUser.login,
|
| 44 |
+
name: githubUser.name,
|
| 45 |
+
avatar_url: githubUser.avatar_url,
|
| 46 |
+
avatarUrl: githubUser.avatar_url,
|
| 47 |
+
bio: githubUser.bio,
|
| 48 |
+
location: githubUser.location,
|
| 49 |
+
company: githubUser.company,
|
| 50 |
+
blog: githubUser.blog,
|
| 51 |
+
twitter_username: githubUser.twitter_username,
|
| 52 |
+
public_repos: githubUser.public_repos,
|
| 53 |
+
public_gists: githubUser.public_gists,
|
| 54 |
+
followers: githubUser.followers,
|
| 55 |
+
following: githubUser.following,
|
| 56 |
+
created_at: githubUser.created_at,
|
| 57 |
+
html_url: githubUser.html_url,
|
| 58 |
+
});
|
| 59 |
+
} catch (githubError: any) {
|
| 60 |
+
if (githubError.status === 404) {
|
| 61 |
+
return NextResponse.json({ error: "GitHub user not found" }, { status: 404 });
|
| 62 |
+
}
|
| 63 |
+
throw githubError;
|
| 64 |
+
}
|
| 65 |
+
} catch (error) {
|
| 66 |
+
console.error("GET /api/user/github/:username error:", error);
|
| 67 |
+
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
|
| 68 |
+
}
|
| 69 |
+
}
|