Skip to content

Commit

Permalink
oops i forgot
Browse files Browse the repository at this point in the history
  • Loading branch information
Hassunaama committed Sep 20, 2024
1 parent 85ef5b6 commit 6ef4270
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/app/api/data/misc/minecraftSkin/[username]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export async function GET(
request: Request,
{ params }: { params: { username: string } }
) {
const username = params.username
// Helper function to fetch the Mojang profile ID based on the username
async function getProfileId(username: string): Promise<string | null> {
const response = await fetch(`https://api.mojang.com/users/profiles/minecraft/${username}`);
if (response.ok) {
const data = await response.json();
return data.id;
}
return null;
}

// Helper function to fetch the session profile and decode the skin URL
async function getSkinUrl(profileId: string): Promise<string | null> {
const response = await fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${profileId}`);
if (response.ok) {
const data = await response.json();
const properties = data.properties.find((prop: any) => prop.name === "textures");
if (properties) {
const decoded = JSON.parse(atob(properties.value));
return decoded.textures.SKIN.url;
}
}
return null;
}

try {
const profileId = await getProfileId(username);
if (profileId) {
const skinUrl = await getSkinUrl(profileId);
return Response.json(skinUrl);
}
} catch (error) {
return Response.json("Failed to fetch skin URL:" + error);
}
}

0 comments on commit 6ef4270

Please sign in to comment.