Skip to content

Commit

Permalink
fix: give gh api reqs user agent, use gh api helper in auth hook
Browse files Browse the repository at this point in the history
  • Loading branch information
xynydev committed Apr 20, 2024
1 parent e32733d commit 0ccd7f4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ghApiGet } from "$lib/ts/github/api";
import type { Handle } from "@sveltejs/kit";

export const handle: Handle = async ({ event, resolve }) => {
const githubToken = event.cookies.get("github_oauth_token") ?? null;
const githubUserResponse = await fetch("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${githubToken}`
}
});
event.locals.githubUser = githubUserResponse.ok ? await githubUserResponse.json() : null;
if (githubToken !== null) {
const githubUserResponse = await ghApiGet(githubToken, "/user");
event.locals.githubUser = githubUserResponse.ok ? githubUserResponse.data : null;
}

return await resolve(event);
};
21 changes: 18 additions & 3 deletions src/lib/ts/github/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ export async function ghApiPost(
): Promise<{ ok: boolean; data: object }> {
const res = await fetch(`https://api.github.com${path}`, {
method: "post",
headers: { Authorization: `Bearer ${token}`, "X-GitHub-Api-Version": "2022-11-28" },
headers: {
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "BlueBuild Workshop",
Accept: "application/vnd.github+json"
},
body: JSON.stringify(data)
});
return { data: await res.json(), ok: res.ok };
Expand All @@ -18,7 +23,12 @@ export async function ghApiPut(
): Promise<{ ok: boolean; data: object }> {
const res = await fetch(`https://api.github.com${path}`, {
method: "put",
headers: { Authorization: `Bearer ${token}`, "X-GitHub-Api-Version": "2022-11-28" },
headers: {
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "BlueBuild Workshop",
Accept: "application/vnd.github+json"
},
body: JSON.stringify(data)
});
return { data: await res.json(), ok: res.ok };
Expand All @@ -30,7 +40,12 @@ export async function ghApiGet(
): Promise<{ ok: boolean; data: object }> {
const res = await fetch(`https://api.github.com${path}`, {
method: "get",
headers: { Authorization: `Bearer ${token}`, "X-GitHub-Api-Version": "2022-11-28" }
headers: {
Authorization: `Bearer ${token}`,
"X-GitHub-Api-Version": "2022-11-28",
"User-Agent": "BlueBuild Workshop",
Accept: "application/vnd.github+json"
}
});
return { data: await res.json(), ok: res.ok };
}
13 changes: 2 additions & 11 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
export async function load({ locals, cookies }) {
const githubToken = cookies.get("github_oauth_token") ?? null;
const githubUserResponse = await fetch("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${githubToken}`,
"User-Agent": "BlueBuild Workshop"
}
});

export async function load({ locals }) {
return {
githubUser: githubUserResponse.ok ? await githubUserResponse.json() : null,
status: githubUserResponse.statusText + " " + githubUserResponse.status
githubUser: locals.githubUser
};
}

0 comments on commit 0ccd7f4

Please sign in to comment.