|
| 1 | +# Clerk + Astro.js + Prisma example |
| 2 | + |
| 3 | +This example shows how to implement **authentication** using [Clerk](https://clerk.com/), [Astro.js](https://astro.build/) and [Prisma](https://www.prisma.io). |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +### 1. Download example and navigate into the project directory |
| 8 | + |
| 9 | +Download this example: |
| 10 | + |
| 11 | +``` |
| 12 | +npx try-prisma@latest --template orm/clerk-astro |
| 13 | +``` |
| 14 | + |
| 15 | +Then, navigate into the project directory: |
| 16 | + |
| 17 | +``` |
| 18 | +cd clerk-astro |
| 19 | +``` |
| 20 | + |
| 21 | +<details><summary><strong>Alternative:</strong> Clone the entire repo</summary> |
| 22 | + |
| 23 | +Clone this repository: |
| 24 | + |
| 25 | +``` |
| 26 | +git clone [email protected]:prisma/prisma-examples.git --depth=1 |
| 27 | +``` |
| 28 | + |
| 29 | +Install npm dependencies: |
| 30 | + |
| 31 | +``` |
| 32 | +cd prisma-examples/orm/clerk-astro |
| 33 | +npm install |
| 34 | +``` |
| 35 | + |
| 36 | +</details> |
| 37 | + |
| 38 | +Rename the `.env.example` file to `.env` |
| 39 | + |
| 40 | +### 2. Create a Prisma Postgres instance |
| 41 | + |
| 42 | +This example uses a [Prisma Postgres](https://prisma.io/postgres) database by default. To get started with the project, you will need to setup a Prisma Postgres connection string: |
| 43 | + |
| 44 | +1. Set up a new Prisma Postgres instance in the [Prisma Data Platform Console](https://console.prisma.io) and copy the database connection URL. |
| 45 | +2. Add your database url to the `.env` |
| 46 | +3. Run `npx prisma migrate dev --name init` |
| 47 | + |
| 48 | +That's it, your project is now configured to use Prisma Postgres! |
| 49 | + |
| 50 | +### 3. Set up Clerk |
| 51 | + |
| 52 | +#### 3.1. Create a new Clerk app |
| 53 | + |
| 54 | +Create a new app at [dashboard.clerk.com/apps/new](https://dashboard.clerk.com/apps/new). Provide a name and select whichever sign-in options you would like. |
| 55 | + |
| 56 | +Skip to Step 2 and copy the `PUBLIC_CLERK_PUBLISHABLE_KEY` and the `CLERK_SECRET_KEY`. Paste those into their respective positions in `.env` |
| 57 | + |
| 58 | +#### 3.2. Expose your server via [ngrok](https://ngrok.com) |
| 59 | + |
| 60 | +Install ngrok and expose it your local app: |
| 61 | + |
| 62 | +``` |
| 63 | +npm install --global ngrok |
| 64 | +ngrok http 4321 |
| 65 | +``` |
| 66 | + |
| 67 | +Copy the ngrok `Forwarding URL`. This will be used to set the webhook URL in Clerk. |
| 68 | + |
| 69 | +Navigate to the **_Webhooks_** section of your Clerk application located near the bottom of the **_Configure_** tab under **_Developers_**. |
| 70 | + |
| 71 | +Click **_Add Endpoint_** and paste the ngrok URL into the **_Endpoint URL_** field and add `/api/webhooks/clerk` to the end of the URL. It should look similar to this: |
| 72 | + |
| 73 | +``` |
| 74 | +https://a60b-99-42-62-240.ngrok-free.app/api/webhooks/clerk |
| 75 | +``` |
| 76 | + |
| 77 | +Copy the **_Signing Secret_** and add it to your `.env` file: |
| 78 | + |
| 79 | +Add your ngrok url without the `https://` to the allowedHosts array in your `astro.config.mjs` file: |
| 80 | + |
| 81 | +``` |
| 82 | +import { defineConfig } from 'astro/config'; |
| 83 | +
|
| 84 | +export default defineConfig({ |
| 85 | + // ... |
| 86 | + server: { |
| 87 | + allowedHosts: ['your-ngrok-url'], |
| 88 | + }, |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +### 4. Start the development server |
| 93 | + |
| 94 | +``` |
| 95 | +npm run dev |
| 96 | +``` |
| 97 | + |
| 98 | +The server is now running at http://localhost:4321 |
| 99 | + |
| 100 | +## Switch to another database |
| 101 | + |
| 102 | +If you want to try this example with another database rather than Prisma Postgres, refer to the [Databases](https://www.prisma.io/docs/orm/overview/databases) section in our documentation. |
| 103 | + |
| 104 | +## Next steps |
| 105 | + |
| 106 | +- Check out the [Prisma docs](https://www.prisma.io/docs) |
| 107 | +- [Join our community on Discord](https://pris.ly/discord?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) to share feedback and interact with other users. |
| 108 | +- [Subscribe to our YouTube channel](https://pris.ly/youtube?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for live demos and video tutorials. |
| 109 | +- [Follow us on X](https://pris.ly/x?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for the latest updates. |
| 110 | +- Report issues or ask [questions on GitHub](https://pris.ly/github?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section). |
0 commit comments