Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sveltekit): update more current docs to latest SvelteKitAuth API and finish example app migration to latest API #10191

Merged
merged 6 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions apps/dev/sveltekit/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script lang="ts">
import { page } from "$app/stores"
import { SignIn, SignOut } from "@auth/sveltekit/components"
import { signIn, signOut } from "@auth/sveltekit/client"
import { SignIn } from "@auth/sveltekit/components"
import { signIn } from "@auth/sveltekit/client"

let password = ""
</script>
Expand Down
2 changes: 1 addition & 1 deletion apps/examples/sveltekit/src/components/header.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{$page.data.session.user?.email ?? $page.data.session.user?.name}
</span>
<SignOut>
<div class="buttonPrimary">Sign out</div>
<div slot="submitButton" class="buttonPrimary">Sign out</div>
</SignOut>
{:else}
<span class="notSignedInText">You are not signed in</span>
Expand Down
117 changes: 86 additions & 31 deletions packages/frameworks-sveltekit/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@
* [origin]/auth/callback/[provider]
* ```
*
* ## Signing in and signing out
* ## Signing in and Signing out

Check warning on line 59 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L59

Added line #L59 was not covered by tests
*
* The data for the current session in this example was made available through the `$page` store which can be set through the root `+page.server.ts` file.
* It is not necessary to store the data there, however, this makes it globally accessible throughout your application simplifying state management.
* ### Server-side
*
* `<SignIn />` and `<SignOut />` are components that `@auth/sveltekit` provides out of the box - they handle the sign-in/signout flow, and can be used as-is as a starting point or customized for your own components. This is an example of how to use the `SignIn` and `SignOut` components to login and logout using SvelteKit's server-side form actions. You will need two things to make this work:
*
* 1. Using the components in your SvelteKit app's frontend
* 2. Add the required `page.server.ts` at `/signin` (for `SignIn`) and `/signout` (for `SignOut`) to handle the form actions

Check warning on line 66 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L61-L66

Added lines #L61 - L66 were not covered by tests
*
* ```ts
* <script>
Expand All @@ -71,38 +75,86 @@
* <div>
* {#if $page.data.session}
* {#if $page.data.session.user?.image}
* <span
* style="background-image: url('{$page.data.session.user.image}')"
* <img
* src={$page.data.session.user.image}

Check warning on line 79 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L78-L79

Added lines #L78 - L79 were not covered by tests
* class="avatar"
* alt="User Avatar"

Check warning on line 81 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L81

Added line #L81 was not covered by tests
* />
* {/if}
* <span class="signedInText">
* <small>Signed in as</small><br />
* <strong>{$page.data.session.user?.name ?? "User"}</strong>
* </span>
* <SignOut />
* <SignOut>
* <div slot="submitButton" class="buttonPrimary">Sign out</div>
* </SignOut>

Check warning on line 90 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L88-L90

Added lines #L88 - L90 were not covered by tests
* {:else}
* <span class="notSignedInText">You are not signed in</span>
* <SignIn provider="github"/>
* <SignIn provider="google"/>
* <SignIn>
* <div slot="submitButton" class="buttonPrimary">Sign in</div>
* </SignIn>

Check warning on line 95 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L93-L95

Added lines #L93 - L95 were not covered by tests
* <SignIn provider="facebook"/>
* {/if}
* </div>
* ```
*
* `<SignIn />` and `<SignOut />` are components that `@auth/sveltekit` provides out of the box - they handle the sign-in/signout flow, and can be used as-is as a starting point or customized for your own components.
* To set up the form actions, we need to define the files in `src/routes`:
*

Check warning on line 102 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L102

Added line #L102 was not covered by tests
* ```ts title="src/routes/signin/+page.server.ts"
* import { signIn } from "../../auth"
* import type { Actions } from "./$types"
* export const actions: Actions = { default: signIn }
* ```
*

Check warning on line 108 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L108

Added line #L108 was not covered by tests
* ```ts title="src/routes/signout/+page.server.ts"
* import { signOut } from "../../auth"
* import type { Actions } from "./$types"
* export const actions: Actions = { default: signOut }
* ```
*
* These routes are customizeable with the `signInPage` and `signOutPage` props on the respective comopnents.
*
* ### Client-Side
*
* We also export two methods from `@auth/sveltekit/client` in order to do client-side sign-in and sign-out actions.
*
* ```ts title="src/routes/index.svelte"
* import { signIn, signOut } from "@auth/sveltekit/client"
*
* <nav>
* <p>
* These actions are all using the methods exported from
* <code>@auth/sveltekit/client</code>
* </p>
* <div class="actions">
* <div class="wrapper-form">
* <button on:click={() => signIn("github")}>Sign In with GitHub</button>
* </div>
* <div class="wrapper-form">
* <button on:click={() => signIn("discord")}>Sign In with Discord</button>
* </div>
* <div class="wrapper-form">
* <div class="input-wrapper">
* <label for="password">Password</label>
* <input
* bind:value={password}
* type="password"
* id="password"
* name="password"
* required
* />
* </div>
* <button on:click={() => signIn("credentials", { password })}>
* Sign In with Credentials
* </button>
* <button on:click={() => signOut()})}>
* Sign Out
* </button>
* </div>
* </div>
* </nav>
* ```
*

Check warning on line 157 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L115-L157

Added lines #L115 - L157 were not covered by tests
* ## Managing the session
*
* The above example checks for a session available in `$page.data.session`, however that needs to be set by us somewhere.
Expand Down Expand Up @@ -162,39 +214,42 @@
* - Very easy to modify
*
* The way to handle authorization through the URI is to override your handle hook.
* The handle hook, available in `hooks.server.ts`, is a function that receives ALL requests sent to your SvelteKit webapp.
* You may intercept them inside the handle hook, add and modify things in the request, block requests, etc.
* Some readers may notice we are already using this handle hook for SvelteKitAuth which returns a handle itself, so we are going to use SvelteKit's sequence to provide middleware-like functions that set the handle hook.
* The handle hook, returned from `SvelteKitAuth` in your `src/auth.ts`, is a function that is designed to receive ALL requests sent to your SvelteKit webapp.
* You should export it from `src/auth.ts` and import it in your `src/hooks.server.ts`.
* To use multiple handles in your `hooks.server.ts`, we can use SvelteKit's `sequence` to execute all of them in series.

Check warning on line 219 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L217-L219

Added lines #L217 - L219 were not covered by tests
*
* ```ts
* ```ts title="src/auth.ts"

Check warning on line 221 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L221

Added line #L221 was not covered by tests
* import { SvelteKitAuth } from '@auth/sveltekit';
* import GitHub from '@auth/sveltekit/providers/github';
* import { GITHUB_ID, GITHUB_SECRET } from '$env/static/private';
*
* export const { handle, signIn, signOut } = SvelteKitAuth({
* providers: [GitHub]
* }),
* ```
*
* ```ts title="src/hooks.server.ts"

Check warning on line 230 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L224-L230

Added lines #L224 - L230 were not covered by tests
* import { redirect, type Handle } from '@sveltejs/kit';
* import { handle: authenticationHandle } from './auth';

Check warning on line 232 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L232

Added line #L232 was not covered by tests
* import { sequence } from '@sveltejs/kit/hooks';
*
* async function authorization({ event, resolve }) {
* // Protect any routes under /authenticated
* if (event.url.pathname.startsWith('/authenticated')) {
* const session = await event.locals.getSession();
* if (!session) {
* throw redirect(303, '/auth');
* }
* }
*
* // If the request is still here, just proceed as normally
* return resolve(event);
* async function authorizationHandle({ event, resolve }) {
* // Protect any routes under /authenticated
* if (event.url.pathname.startsWith('/authenticated')) {
* const session = await event.locals.getSession();
* if (!session) {
* // Redirect to the signin page
* throw redirect(303, '/auth/signin');
* }
* }
*
* // If the request is still here, just proceed as normally
* return resolve(event);

Check warning on line 246 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L235-L246

Added lines #L235 - L246 were not covered by tests
* }
*
* // First handle authentication, then authorization
* // Each function acts as a middleware, receiving the request handle
* // And returning a handle which gets passed to the next function
* export const handle: Handle = sequence(
* SvelteKitAuth({
* providers: [GitHub({ clientId: GITHUB_ID, clientSecret: GITHUB_SECRET })]
* }),
* authorization
* );
* export const handle: Handle = sequence(authenticationHandle, authorizationHandle)

Check warning on line 252 in packages/frameworks-sveltekit/src/lib/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/frameworks-sveltekit/src/lib/index.ts#L252

Added line #L252 was not covered by tests
* ```
*
* :::info
Expand Down
Loading