Skip to content

Commit

Permalink
fix: auth now working
Browse files Browse the repository at this point in the history
Co-authored-by: fero <[email protected]>
  • Loading branch information
Wamy-Dev and ferothefox committed Aug 11, 2023
1 parent 197c2c0 commit 0034cf0
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 30 deletions.
1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"type": "module",
"dependencies": {
"jwt-decode": "^3.1.2",
"sugar": "^2.0.6",
"tailwind-preset-base": "workspace:^",
"ui": "workspace:^"
Expand Down
17 changes: 12 additions & 5 deletions apps/web/src/lib/components/layout/NavBar.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<script>
<script lang="ts">
import { Link } from 'ui/link';
import { Button } from 'ui/button';
import { env } from '$env/dynamic/public';
import { isAuthenticated, user } from '$lib/stores/user';
import { onMount } from 'svelte';
import jwt_decode from 'jwt-decode';
$: isAuthenticated;
$: user;
onMount(() => {
const cookie = document.cookie.split(';').find((cookie) => cookie.startsWith('auth='))?.split('=')[1]
if (cookie) {
$isAuthenticated = true
$user = jwt_decode(cookie)
}
})
</script>

<div class="beatforge-page-navbar flex w-full items-center py-8 px-4 mx-auto max-w-7xl z-[100] text-sm font-bold">
Expand All @@ -19,8 +26,8 @@
</Link>
{#if $isAuthenticated}
<Link variant="secondary" href="/dashboard">
{user.username}
<img slot="trailing" alt="user icon" src={user.avatar} draggable="false" class="w-10 h-10 rounded-full">
{$user.user.username}
<img alt="user icon" src={$user.user.avatar} draggable="false" class="w-10 h-10 rounded-full">
</Link>
{:else}
<Link variant="text" href="https://discord.gg/HTVrjFENaZ" external>
Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/lib/stores/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { writable } from "svelte/store";

export const isAuthenticated = writable(false);
export const user = writable(null);
export const user = writable(null);
export const jwtKey = writable(null);
15 changes: 15 additions & 0 deletions apps/web/src/routes/callback/github/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
<script lang="ts">
import { onMount } from 'svelte';
import { goto } from '$app/navigation';
import { isAuthenticated, user, jwtKey } from '$lib/stores/user';
import jwt_decode from 'jwt-decode';
onMount(() => {
document.cookie = `auth=${$jwtKey}; path=/; expires=${new Date(Date.now() + 1000 * 60 * 60 * 24).toUTCString()}`;
$isAuthenticated = true;
$user = jwt_decode($jwtKey);
goto('/')
})
</script>


<div class="h-screen">

</div>
33 changes: 10 additions & 23 deletions apps/web/src/routes/callback/github/+page.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,26 @@
import { env } from '$env/dynamic/public';
import type { PageLoad } from './$types';
import { redirect } from '@sveltejs/kit';
import { jwtKey } from '$lib/stores/user';

export const load = (async ({ url: { searchParams }, fetch }) => {
const code = searchParams.get('code');
if (!code) {
return {
status: 302,
headers: {
location: '/'
}
};
throw redirect(302, '/');
} else {
try {
const response = await fetch(`${env.PUBLIC_API_URL}/auth/github?code=${code}`, {
method: 'POST'
})
const { jwt } = await response.json();
return {
status: 302,
method: 'POST',
headers: {
location: '/',
'set-cookie': `jwt=${jwt}; Path=/; HttpOnly`
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json",
}
};
})
const jwt = await response.json();
jwtKey.set(jwt.jwt);
} catch (error) {
console.log(error)
return {
status: 500,
headers: {
location: '/'
},
body: {
error: error.message
}
};
throw redirect(302, '/');
}
}
}) satisfies PageLoad;
Empty file.
2 changes: 1 addition & 1 deletion apps/web/src/routes/mod/[mod]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
}
</script>

<div class="h-screen w-screen">
<div class="h-full w-full">
<div class="flex justify-center">
<div class="flex flex-col gap-2 w-full max-w-7xl px-4">
<div
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0034cf0

Please sign in to comment.