Created
May 17, 2021 07:18
-
-
Save mschewe/188ec8016e07bc79644538462cef9164 to your computer and use it in GitHub Desktop.
firebase auth attempt with svelte kit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script context="module" lang="ts"> | |
import { authGuard } from '../auth/guard'; | |
import type { LoadInput, LoadOutput } from '@sveltejs/kit/types/page'; | |
export async function load({ page, fetch, session, context }: LoadInput): Promise<LoadOutput> { | |
return await authGuard({ page, fetch, session, context }); | |
} | |
</script> | |
<script lang="ts"> | |
import '../app.postcss'; | |
</script> | |
<div class="bg-gray-100 h-screen"> | |
<div class="mx-auto max-w-xl bg-white rounded shadow"> | |
<header> | |
<div class="p-2 bg-indigo-800 text-center text-white">HEADER</div> | |
</header> | |
<div class="py-2 px-1"> | |
<slot /> | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { User } from "firebase/auth"; | |
import { writable } from "svelte/store"; | |
export interface AuthState { | |
user: User | null | |
} | |
export const authStore = writable<AuthState>({ user: null }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { getAuth, onAuthStateChanged, Auth, signInWithEmailAndPassword, signOut as fbsignout } from "firebase/auth"; | |
import { initializeApp } from "firebase/app" | |
import { browser } from "$app/env"; | |
import { authStore } from "../stores/auth"; | |
const firebaseConfig = { | |
/// ... | |
}; | |
let auth: Auth; | |
// initialize firebase | |
if (browser) { | |
const firebaseApp = initializeApp(firebaseConfig); | |
auth = getAuth(firebaseApp); | |
onAuthStateChanged(auth, user => { | |
console.log("user:", user); | |
authStore.set({ user: user }) | |
}); | |
} | |
export async function signIn(email: string, password: string): Promise<boolean> { | |
try { | |
await signInWithEmailAndPassword(auth, email, password) | |
return true; | |
} catch (err) { | |
console.log("error: signIn:", err); | |
return false; | |
} | |
} | |
export async function signOut(): Promise<boolean> { | |
try { | |
await fbsignout(auth); | |
return true; | |
} catch (err) { | |
console.log("error: signout:", err); | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type { LoadInput, LoadOutput } from '@sveltejs/kit/types/page'; | |
import { authStore } from '../stores/auth'; | |
export async function authGuard({ page }: LoadInput): Promise<LoadOutput> { | |
let isLoggedIn = false; | |
authStore.subscribe(store => { | |
store.user ? true : false; | |
}) | |
console.log(page.path) | |
if (!isLoggedIn && page.path.startsWith('/app')) { | |
return { status: 302, redirect: '/auth' } | |
} | |
return {}; | |
} | |
export default { | |
authGuard | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment