This is an example of full-stack SvelteKit app using Cloudflare Pages as a deployment environment.
The tech stack is as follows:
- SvelteKit
- Flowbite Svelte
- TailwindCSS based component library
- Auth.js
- Authentication library
- Cloudflare D1
- Cloudflare native serverless relational database
- Wrangler
- Development/deployment tool by Cloudflare
The app itself is very simple. It allows SSO login with Google and when you log in first time an user is created in the database based on the received token. Then the logged in user can add and delete notes that will be saved on the Cloudflare D1 database.
To use this app few initial steps are required.
Create database:
npm run create:database
Modify d1_databases in wrangler.toml so it matches what the command printed. For example:
[[d1_databases]]
binding = "DB"
database_name = "prod-devnotes"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
Create database schema for the local database:
npm run create:schema:local
This app uses Google SSO for the user logins. You need to setup this in Google Cloud.
- Create an OAuth consent screen in APIs & Services
- You need to add your email address to "Test users"
- Create Credentials
- Create OAuth 2.0 Client ID
- Add URLs to the redirect URL
- http://localhost:5173/auth/callback/google
- For local development
- http://localhost:8788/auth/callback/google
- For preview build of app (with
npm run preview
)
- For preview build of app (with
- https://example.com/auth/callback/google
- Whatever domain you will be using in production
- http://localhost:5173/auth/callback/google
- Add the client secrets to environment variables
-
For local development add them to
.env
and.dev.vars
files at projects root:AUTH_GOOGLE_ID=YOUR-CLIENT-ID AUTH_GOOGLE_SECRET=YOUR-CLIENT-SECRET
-
The
.env
file is used when runningnpm run dev
and.dev.vars
when runningnpm run preview
-
In production you need to set them in Cloudflare
- This is described in the deployment section of this document
-
- Create Auth.js secret that it uses for encrypting cookies and tokens
-
Run
npm exec auth secret
-
Locally you can add it to
.env
file at projects root:AUTH_SECRET=YOUR-SECRET
-
The
.env
file is used when runningnpm run dev
and.dev.vars
when runningnpm run preview
-
In production you need to set it in Cloudflare
- This is described in the deployment section of this document
-
npm run dev
# or start the server and open the app in a new browser tab
npm run dev -- --open
To create a production version of your app:
npm run build
You can preview the production build with npm run preview
.
Deploy the "infrastructure" by running:
npm run deploy
If this is your first deploy then you need to create the database schema:
npm run create:schema:remote
After the deployment you also need to set ALL the secrets for the Google SSO.
You should already have the values on your local .env
or .dev.vars
, but
it is recommended to create a new AUTH_SECRET for the production environment.
npm exec auth secret
Then create the variables in the cloud with Wrangler or from the Cloudflare dashboard. The process is
- Log in to Cloudflare dashboard and select your account.
- Select Workers & Pages.
- In Overview, select your Pages > Settings.
- Under Production, select Add variable.
- Input a Variable name and its value, which will be made available to your Pages app.
- Select Encrypt to protect the secret’s value. This will prevent the value from being visible via Wrangler and the dashboard.
- To add multiple secrets, select Add variable.
- Select Save to implement your changes.
After adding the environment variables you need to deploy again:
npm run deploy
You will also need to update the redirect URL in gor Google OAuth 2.0 Client ID to contain the URL where the app is deployed to:
- https://example.com/auth/callback/google
- Use domain Cloudflare gave you instead of example.com