SvelteKit Quickstart
This is a quickstart guide to help you integrate Descope with your SvelteKit application. Follow the steps below to get started.
Configure Auth.js with Descope
To enable authentication in SvelteKit, import SvelteKitAuth
and set up Descope as an authentication provider. Ensure environment variables are loaded to securely manage credentials. Then, configure Descope within SvelteKitAuth
to handle authentication seamlessly.
Create or update your src/hooks.server.ts
file:
AUTH_DESCOPE_ID
: Can be found in your Descope account under the Project Settings.AUTH_DESCOPE_SECRET
: Can be generated in your Descope account under the Access Keys page.AUTH_DESCOPE_ISSUER
: Can be found in your Descope account under the Applications page.
Note
You can get all the above required links by going to the Federated Apps in the Descope Console.
Add TypeScript Types (Optional but Recommended)
This tells TypeScript that session data exists in Locals and PageData. It helps prevent TypeScript errors while working with authentication.
Update your src/app.d.ts
:
Basic Authentication Component
The authentication flow works by checking whether a user is signed in. If they are, their email and a logout button are displayed; otherwise, a login button is shown. Clicking "Sign in with Descope" initiates the login process, while clicking "Sign out" logs the user out, ensuring a seamless authentication experience.
Protected API Route
If the user is not logged in, the system returns a 401 Unauthorized error. However, if the user is authenticated, it responds with a JSON object containing the user's details.
Protected Page Route
If the user is not logged in, they are redirected to the login page /auth/signin
. If they are logged in, their details are loaded.
Note
To set up a Descope Federated App, log into the Descope Console and navigate to Applications > Federated Apps to create a new app. You will find all your credentials here in the Federated Apps.
Custom Callback URL (Advanced Configuration)
You can customize the callback URL in your configuration:
Congratulations
Now that you've got the authentication down, go focus on building out the rest of your app! If you're curious to learn more, take a look at this Svelte Sample App.
Checkpoint
Your application is now integrated with Descope. Please test with sign-up or sign-in use case.
Customize
Now that you have the end-to-end application working, you can choose to configure and personalize many different areas of Descope, including your brand, style, custom user authentication journeys, etc. We recommend starting with customizing your user-facing screens, such as signup and login.