|
| 1 | +# Better-Auth + Prisma example |
| 2 | + |
| 3 | +This example shows how to implement **authentication** using [Better-Auth](https://better-auth.com/), [Astro](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/betterauth-astro |
| 13 | +``` |
| 14 | + |
| 15 | +Then, navigate into the project directory: |
| 16 | + |
| 17 | +``` |
| 18 | +cd betterauth-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/betterauth-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 | + |
| 46 | +2. Add your database url to the `.env` |
| 47 | + |
| 48 | +That's it, your project is now configured to use Prisma Postgres! |
| 49 | + |
| 50 | +### 3. Generate and migrate Prisma client |
| 51 | + |
| 52 | +1. Run the following command to generate the Prisma Client. This is what you will be using to interact with your database. |
| 53 | + |
| 54 | +``` |
| 55 | +npx prisma generate |
| 56 | +``` |
| 57 | + |
| 58 | +2. Migrate the DB |
| 59 | + |
| 60 | +``` |
| 61 | +npx prisma migrate dev --name init |
| 62 | +``` |
| 63 | + |
| 64 | +### 4. Set up Better-Auth |
| 65 | + |
| 66 | +1. Generate a Better-Auth secret |
| 67 | + |
| 68 | +``` |
| 69 | +npx @better-auth/cli@latest secret |
| 70 | +``` |
| 71 | + |
| 72 | +2. Add the secret to the `.env`. |
| 73 | + |
| 74 | +3. (Optional) If running on a port other than 4321, add that url to the `trustedOrigins` field in `auth-client.ts` |
| 75 | + |
| 76 | +```diff |
| 77 | +export const auth = betterAuth({ |
| 78 | + database: prismaAdapter(prisma, { |
| 79 | + provider: 'postgresql', |
| 80 | + }), |
| 81 | + emailAndPassword: { |
| 82 | + enabled: true, |
| 83 | + }, |
| 84 | ++ trustedOrigins: ['http://localhost:4322'], |
| 85 | +}) |
| 86 | +``` |
| 87 | + |
| 88 | +### 5. Start the development server |
| 89 | + |
| 90 | +``` |
| 91 | +npm run dev |
| 92 | +``` |
| 93 | + |
| 94 | +The server is now running at http://localhost:4321 |
| 95 | + |
| 96 | +## Switch to another database |
| 97 | + |
| 98 | +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. |
| 99 | + |
| 100 | +## Next steps |
| 101 | + |
| 102 | +- Check out the [Prisma docs](https://www.prisma.io/docs) |
| 103 | +- [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. |
| 104 | +- [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. |
| 105 | +- [Follow us on X](https://pris.ly/x?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for the latest updates. |
| 106 | +- 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