From 08dc2eb29a98ce1e6739680d82779964eab032b4 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Fri, 28 Jun 2024 13:27:11 +0800 Subject: [PATCH 1/3] docs(console): update the sveltekit guide update the sveltekit guide --- .../docs/guides/web-sveltekit/README.mdx | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/packages/console/src/assets/docs/guides/web-sveltekit/README.mdx b/packages/console/src/assets/docs/guides/web-sveltekit/README.mdx index 97b8ddb26d6..61344be4081 100644 --- a/packages/console/src/assets/docs/guides/web-sveltekit/README.mdx +++ b/packages/console/src/assets/docs/guides/web-sveltekit/README.mdx @@ -5,6 +5,7 @@ import InlineNotification from '@/ds-components/InlineNotification'; import Steps from '@/mdx-components/Steps'; import Step from '@/mdx-components/Step'; import Checkpoint from '../../fragments/_checkpoint.md'; +import RedirectUrisWeb from '../../fragments/_redirect-uris-web.mdx'; import { generateStandardSecret } from '@logto/shared/universal'; export const cookieEncryptionKey = generateStandardSecret(); @@ -46,7 +47,7 @@ Create a `hooks.server.ts` file in your project `src` root if you don't have one In your `hooks.server.ts` file, add the following code to inject the Logto hook into your server: - + {`import { handleLogto } from '@logto/sveltekit'; export const handle = handleLogto( @@ -63,7 +64,7 @@ export const handle = handleLogto( Since these information are sensitive, it's recommended to use environment variables: - + {`import { handleLogto } from '@logto/sveltekit'; import { env } from '$env/dynamic/private'; @@ -81,7 +82,7 @@ export const handle = handleLogto( If you have multiple hooks, you can use [the sequence() helper function](https://kit.svelte.dev/docs/modules#sveltejs-kit-hooks) to chain them: -```ts +```ts title="src/hooks.server.ts" import { sequence } from '@sveltejs/kit/hooks'; export const handle = sequence(handleLogto, handleOtherHook); @@ -89,7 +90,7 @@ export const handle = sequence(handleLogto, handleOtherHook); Now you can access the Logto client in the `locals` object. For TypeScript, you can add the type to `app.d.ts`: -```ts +```ts title="src/app.d.ts" import type { LogtoClient, UserInfoResponse } from '@logto/sveltekit'; declare global { @@ -106,25 +107,17 @@ We'll discuss the `user` object later. - - - - In the following steps, we assume your app is running on http://localhost:3000. - - -First, let's enter your redirect URI. E.g. `http://localhost:3000/callback`. [Redirect URI](https://www.oauth.com/oauth2-servers/redirect-uris/) is an OAuth 2.0 concept which implies the location should redirect after authentication. + - + -After signing out, it'll be great to redirect user back to your website. For example, add `http://localhost:3000` as the post sign-out redirect URI below. - - + + In the page where you want to implement sign-in and sign-out, define the following actions: - - {`// +page.server.ts -import type { Actions } from './$types'; + + {`import type { Actions } from './$types'; export const actions: Actions = { signIn: async ({ locals }) => { @@ -141,8 +134,7 @@ export const actions: Actions = { Then use these actions in your Svelte component: -```html -{/* +page.svelte */} +```html title="src/routes/+page.svelte"
@@ -154,8 +146,7 @@ Then use these actions in your Svelte component: To display the user's information, you can inject the `locals.user` object into the layout, thus making it available to all pages: -```ts -// +layout.server.ts +```ts title="src/routes/+layout.server.ts" import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = async ({ locals }) => { @@ -165,8 +156,7 @@ export const load: LayoutServerLoad = async ({ locals }) => { In your Svelte component: -```html -{/* +page.svelte */} +```html title="src/routes/+page.svelte"