A quick start SvelteKit template demonstrating user authentication and authorization using Neon RLS with WorkOS integration. This example showcases how to use WorkOS for authentication and Neon RLS for securing your database with Row Level Security (RLS).
- SvelteKit application with TypeScript
- User authentication powered by WorkOS
- Row-level security using Neon RLS
- Database migrations with Drizzle ORM
- Ready-to-deploy configuration for Netlify
Deploy this example to Netlify with a single click:
Important: After deploying, ensure your WorkOS Redirect URI is set to your deployment URL (e.g.,
https://your-app-name.netlify.app/callback
) in your WorkOS Application settings.
-
Navigate to your WorkOS dashboard and create an AuthKit connection.
-
Configure the Redirect URI to
http://localhost:5173/callback
. -
Copy the Client ID and API Key for the next steps.
-
Open your Neon Console and click "RLS" in your project's settings.
-
Add a new authentication provider.
-
Set the JWKS URL to:
{YOUR_WORKOS_URL}/.well-known/jwks.json
Replace
{YOUR_WORKOS_URL}
with your WorkOS domain (e.g.,https://api.workos.com/sso/jwks/{YOUR_CLIENT_ID}
). You can find the exact format in your.env.example
asWORKOS_JWKS_URL
. -
Follow the steps in the UI to setup the roles for Neon RLS. You should ignore the schema related steps if you're following this guide.
-
Note down the connection strings for both the
neondb_owner
role and theauthenticated, passwordless
role. You'll need both. Theneondb_owner
role has full privileges and is used for migrations, while theauthenticated
role will be used by the application and will have its access restricted by RLS.
-
Clone the repository:
git clone https://github.com/neondatabase-labs/workos-drizzle-sveltekit-neon-rls cd workos-drizzle-sveltekit-neon-rls
-
Install dependencies:
npm install
-
Create a
.env
file based on.env.example
and fill in the necessary values:# For the admin `neondb_owner` role. DATABASE_URL= # For the `authenticated`, passwordless role. DATABASE_AUTHENTICATED_URL= # For the `anonymous` role, passwordless role. DATABASE_ANONYMOUS_URL= WEBSITE_URL=http://localhost:5173 # Change to your deployment URL # from the WorkOS dashboard WORKOS_API_KEY= # from the WorkOS dashboard WORKOS_CLIENT_ID= # format is https://api.workos.com/sso/jwks/{client_id} WORKOS_JWKS_URL= # run `openssl rand -base64 64` to generate a new password WORKOS_COOKIE_PASSWORD=
-
Set up the database:
npm run db:generate # Generate migrations npm run db:migrate # Run migrations
-
Start the development server:
npm run dev -- --open
-
Visit
http://localhost:5173
to see the application running.
Update your WorkOS environment to production and the JWKS URL in Neon RLS accordingly.
Note: Before deploying to production, ensure you have configured the correct Redirect URI in your WorkOS Application settings to match your deployed application's URL (e.g.,
https://your-app-name.netlify.app/callback
).
Contributions are welcome! Please feel free to submit a Pull Request.