Skip to content

Commit

Permalink
chore: 깃헙 로그인 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Daco2020 committed Dec 19, 2023
1 parent bc4c700 commit a193f48
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 135 deletions.
179 changes: 86 additions & 93 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
},
"type": "module",
"dependencies": {
"@supabase/supabase-js": "^2.39.0"
"@supabase/auth-helpers-sveltekit": "^0.10.7",
"@supabase/auth-ui-shared": "^0.1.8",
"@supabase/auth-ui-svelte": "^0.2.8",
"@supabase/supabase-js": "^2.39.1"
}
}
17 changes: 17 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// src/app.d.ts

import { SupabaseClient, Session } from '@supabase/supabase-js'

declare global {
namespace App {
interface Locals {
supabase: SupabaseClient
getSession(): Promise<Session | null>
}
interface PageData {
session: Session | null
}
// interface Error {}
// interface Platform {}
}
}
44 changes: 33 additions & 11 deletions src/components/nav.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,42 @@
import { page } from '$app/stores';
import { derived } from 'svelte/store';
export let signInWithGithub;
export let signOut;
export let currentUser;
let user;
$: currentUser().then((u) => {
user = u;
});
const currentPath = derived(page, ($page) => $page.url.pathname);
</script>

<nav class="fixed top-0 left-0 w-full z-10 flex justify-center bg-mainPurple py-4">
<div class="w-full max-w-6xl text-base text-gray-200 flex justify-center md:justify-end">
<a class="mx-4 nav-link" href="/" class:font-bold={$currentPath === '/'}>월간 메이커스</a>
<a class="mx-4 nav-link" href="/projects" class:font-bold={$currentPath === '/projects'}
>프로젝트 등록</a
>
<a class="mx-4 nav-link" href="/my" class:font-bold={$currentPath === '/my'}>내 프로젝트 관리</a
>
<a class="mx-4 nav-link" href="/community" class:font-bold={$currentPath === '/community'}
>커뮤니티</a
>
<nav class="fixed top-0 left-0 w-full z-10 flex justify-center bg-mainPurple py-4 h-16">
<div
class="w-full max-w-6xl text-base text-gray-200 flex justify-center md:justify-between items-center"
>
<div>
<a class="mx-4 nav-link" href="/" class:font-bold={$currentPath === '/'}>월간 메이커스</a>
<a class="mx-4 nav-link" href="/projects" class:font-bold={$currentPath === '/projects'}
>프로젝트 등록</a
>
<a class="mx-4 nav-link" href="/my" class:font-bold={$currentPath === '/my'}
>내 프로젝트 관리</a
>
<a class="mx-4 nav-link" href="/community" class:font-bold={$currentPath === '/community'}
>커뮤니티</a
>
</div>
<div class="mx-4 flex justify-center md:justify-start">
{#if user}
<img class="w-8 h-8 rounded-full mx-4" src={user.user_metadata.avatar_url} alt="avatar" />
<button on:click={signOut}>Sign out</button>
{:else}
<button on:click={signInWithGithub}>Sign in with GitHub</button>
{/if}
</div>
</div>
</nav>

Expand Down
28 changes: 28 additions & 0 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// src/hooks.server.ts
import { PUBLIC_SUPABASE_URL, PUBLIC_SUPABASE_ANON_KEY } from '$env/static/public'
import { createSupabaseServerClient } from '@supabase/auth-helpers-sveltekit'
import type { Handle } from '@sveltejs/kit'

export const handle: Handle = async ({ event, resolve }) => {
event.locals.supabase = createSupabaseServerClient({
supabaseUrl: PUBLIC_SUPABASE_URL,
supabaseKey: PUBLIC_SUPABASE_ANON_KEY,
event,
})

/**
* A convenience helper so we can just call await getSession() instead const { data: { session } } = await supabase.auth.getSession()
*/
event.locals.getSession = async () => {
const {
data: { session },
} = await event.locals.supabase.auth.getSession()
return session
}

return resolve(event, {
filterSerializedResponseHeaders(name) {
return name === 'content-range'
},
})
}
Empty file added src/lib/auth.js
Empty file.
6 changes: 6 additions & 0 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// src/routes/+layout.server.ts
export const load = async ({ locals: { getSession } }) => {
return {
session: await getSession(),
}
}
72 changes: 60 additions & 12 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@
<script>
import "../app.css";
import Nav from '../components/nav.svelte';
</script>
import '../app.css';
import Nav from '../components/nav.svelte';
<style>
:global(body) {
/* background-color: #0e0719; */
background-color: #F3F4F6;
}
</style>
import { invalidate } from '$app/navigation';
import { onMount } from 'svelte';
export let data;
let { supabase, session } = data;
$: ({ supabase, session } = data);
onMount(() => {
const { data } = supabase.auth.onAuthStateChange((event, _session) => {
if (_session?.expires_at !== session?.expires_at) {
invalidate('supabase:auth');
}
});
return () => data.subscription.unsubscribe();
});
async function currentUser() {
try {
const { data } = await supabase.auth.getUser();
return data.user;
} catch (error) {
console.error('Error fetching user:', error);
return null;
}
}
async function signInWithGithub() {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: 'github',
options: {
redirectTo: '/'
}
});
console.log(error);
}
async function signOut() {
const { error } = await supabase.auth.signOut();
if (!error) {
// 로그아웃 성공 후 페이지 새로고침
location.reload();
} else {
// 오류 처리 (필요한 경우)
console.error('Logout failed:', error);
}
}
</script>
<div class="mx-auto">
<Nav></Nav>
<Nav {signInWithGithub} {signOut} {currentUser}></Nav>
</div>
<div class="max-w-6xl mx-auto mt-32">
<slot />
</div>
<slot />
</div>
<style>
:global(body) {
/* background-color: #0e0719; */
background-color: #f3f4f6;
}
</style>
20 changes: 20 additions & 0 deletions src/routes/+layout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// src/routes/+layout.ts
import { PUBLIC_SUPABASE_ANON_KEY, PUBLIC_SUPABASE_URL } from '$env/static/public'
import { createSupabaseLoadClient } from '@supabase/auth-helpers-sveltekit'

export const load = async ({ fetch, data, depends }) => {
depends('supabase:auth')

const supabase = createSupabaseLoadClient({
supabaseUrl: PUBLIC_SUPABASE_URL,
supabaseKey: PUBLIC_SUPABASE_ANON_KEY,
event: { fetch },
serverSession: data.session,
})

const {
data: { session },
} = await supabase.auth.getSession()

return { supabase, session }
}
18 changes: 0 additions & 18 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,6 @@
}
export let data;
async function signInWithGithub() {
const { data, error } = await supabase.auth.signInWithOAuth({
provider: 'github',
options: {
redirectTo: '/'
}
});
console.log(data);
}
async function signOut() {
const { error } = await supabase.auth.signOut();
}
</script>

<svelte:head>
Expand Down Expand Up @@ -62,7 +48,3 @@
<li>{project.title}</li>
{/each}
</ul>

<button on:click={signInWithGithub}>Sign in with GitHub</button>

<button on:click={signOut}>Sign out</button>

0 comments on commit a193f48

Please sign in to comment.