Skip to content

Commit

Permalink
fix: better auth validation, remove accidental top level "document" call
Browse files Browse the repository at this point in the history
  • Loading branch information
xynydev committed Apr 17, 2024
1 parent c3766f8 commit 5197910
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
18 changes: 18 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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}`
}
});
if (githubUserResponse.ok) {
const githubUser = await githubUserResponse.json();
event.locals.githubUser = githubUser;
} else {
event.locals.githubUser = null;
}

return resolve(event);
};
15 changes: 2 additions & 13 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
// import { githubAuth } from "$lib/ts/github/auth";
import type { RequestEvent } from "@sveltejs/kit";

export async function load(event: RequestEvent) {
const token = event.cookies.get("github_oauth_token") ?? null;

const githubUserResponse = await fetch("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${token}`
}
});
const githubUser = await githubUserResponse.json();
export async function load({ locals }) {
return {
githubUser
githubUser: locals.githubUser
};
}
2 changes: 1 addition & 1 deletion src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class="absolute h-6 w-6 translate-y-10 transition-transform dark:translate-y-0"
/>
</Button>
{#if data.githubUser.login}
{#if data.githubUser}
<Avatar.Root>
<Avatar.Image src={data.githubUser.avatar_url} alt={data.githubUser.login} />
<Avatar.Fallback>CN</Avatar.Fallback>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
>
My repositories
</h2>
{#if !data.githubUser.login}
{#if !data.githubUser}
<Button href="/api/login" size="lg" variant="default" class="mx-auto max-w-xl">
Log in with GitHub</Button
>
Expand Down
7 changes: 7 additions & 0 deletions src/routes/new/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { redirect } from "@sveltejs/kit";
export function load({ locals }) {
console.log(locals);
if (locals.githubUser === null) {
redirect(303, "/");
}
}
2 changes: 1 addition & 1 deletion src/routes/new/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let log: Array<string> = [];
export let data;
let repoName = document.getElementById("reponame");
let repoName = "";
async function createRepo() {
setupStep = "inprogress";
Expand Down

0 comments on commit 5197910

Please sign in to comment.