-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: use Svelte context to pass user to child components to prevent f…
…lashing
- Loading branch information
Showing
4 changed files
with
25 additions
and
16 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { pb } from '$lib/pocketbase' | ||
import { currentUser } from '$lib/stores/user' | ||
import { setContext } from 'svelte' | ||
|
||
pb.authStore.loadFromCookie(document.cookie) | ||
pb.authStore.onChange(() => { | ||
currentUser.set(pb.authStore.model) | ||
setContext('user', pb.authStore.model) | ||
document.cookie = pb.authStore.exportToCookie({ httpOnly: false }) | ||
}, true) |
This file was deleted.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
<h1>Welcome to SvelteKit</h1> | ||
<script lang="ts"> | ||
import type { AuthModel } from 'pocketbase' | ||
import { getContext } from 'svelte' | ||
const user = getContext<AuthModel | undefined>('user') | ||
</script> | ||
|
||
{#if user} | ||
<p>Logged in as {user.email}</p> | ||
{:else} | ||
<p>Not logged in</p> | ||
{/if} | ||
|
||
<p> | ||
Visit <a href="https://kit.svelte.dev">kit.svelte.dev</a> to read the documentation | ||
Visit | ||
<a href="https://github.com/jianyuan/pocketbase-sveltekit-auth"> | ||
our GitHub repository | ||
</a> | ||
to read the documentation. | ||
</p> |