Skip to content

Commit 2a230c5

Browse files
aidankmcalisterAmanVarshney01renovate[bot]
authored
DC-5241 Astro + Better-Auth (#8334)
* project added * readme's updated * test added * minor changes * typo * update graphql test * update test * another grapql udapte * revert graphql tests * Update GraphQL examples (#8327) * fix(deps): pin dependencies (#8335) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> * chore(deps): update dependency supertest to v7.1.4 (#8234) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> --------- Co-authored-by: Aman Varshney <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent 6b8ef56 commit 2a230c5

File tree

20 files changed

+503
-1
lines changed

20 files changed

+503
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
echo "🔍 Starting test setup..."
6+
7+
echo "📂 Current working directory before REPO_ROOT: $(pwd)"
8+
echo "📁 Listing contents:"
9+
ls -la
10+
11+
REPO_ROOT="$(git rev-parse --show-toplevel)"
12+
echo "📌 Detected repo root: $REPO_ROOT"
13+
14+
cd "$REPO_ROOT/orm/betterauth-astro"
15+
echo "📂 Changed directory to: $(pwd)"
16+
17+
echo "📦 Installing test deps..."
18+
npm install
19+
20+
# Go to Node script dir and install its deps
21+
NODE_SCRIPT_DIR="../../.github/get-ppg-dev"
22+
pushd "$NODE_SCRIPT_DIR" > /dev/null
23+
npm install
24+
25+
# Start Prisma Dev server
26+
LOG_FILE="./ppg-dev-url.log"
27+
rm -f "$LOG_FILE"
28+
touch "$LOG_FILE"
29+
30+
echo "🚀 Starting Prisma Dev in background..."
31+
node index.js >"$LOG_FILE" &
32+
NODE_PID=$!
33+
34+
# Wait for DATABASE_URL
35+
echo "🔎 Waiting for Prisma Dev to emit DATABASE_URL..."
36+
MAX_WAIT=60
37+
WAITED=0
38+
until grep -q '^prisma+postgres://' "$LOG_FILE"; do
39+
sleep 1
40+
WAITED=$((WAITED + 1))
41+
if [ "$WAITED" -ge "$MAX_WAIT" ]; then
42+
echo "❌ Timeout waiting for DATABASE_URL"
43+
cat "$LOG_FILE"
44+
kill "$NODE_PID" || true
45+
exit 1
46+
fi
47+
done
48+
49+
DB_URL=$(grep '^prisma+postgres://' "$LOG_FILE" | tail -1)
50+
export DATABASE_URL="$DB_URL"
51+
echo "✅ DATABASE_URL: $DATABASE_URL"
52+
53+
popd > /dev/null
54+
55+
npm install
56+
npx prisma migrate dev --name init --schema prisma/schema.prisma
57+
npm run dev &
58+
pid=$!
59+
60+
sleep 15
61+
62+
# check frontend
63+
curl --fail 'http://localhost:4321/'
64+
65+
66+
kill "$pid"
67+
echo "🛑 App stopped (PID $pid)"
68+
kill "$NODE_PID"
69+
wait "$NODE_PID" || true

orm/betterauth-astro/.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# build output
2+
dist/
3+
# generated types
4+
.astro/
5+
6+
# dependencies
7+
node_modules/
8+
9+
# logs
10+
npm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
pnpm-debug.log*
14+
15+
16+
# environment variables
17+
.env
18+
.env.production
19+
20+
# macOS-specific files
21+
.DS_Store
22+
23+
# jetbrains setting folder
24+
.idea/
25+
26+
/prisma/generated

orm/betterauth-astro/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @ts-check
2+
import { defineConfig } from 'astro/config';
3+
4+
// https://astro.build/config
5+
export default defineConfig({});

orm/betterauth-astro/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "better-auth-test",
3+
"type": "module",
4+
"version": "0.0.1",
5+
"scripts": {
6+
"dev": "astro dev",
7+
"build": "astro build",
8+
"preview": "astro preview",
9+
"astro": "astro"
10+
},
11+
"dependencies": {
12+
"@prisma/client": "^6.18.0",
13+
"@prisma/extension-accelerate": "^2.0.2",
14+
"astro": "^5.15.1",
15+
"better-auth": "^1.3.29",
16+
"dotenv": "^17.2.3"
17+
},
18+
"devDependencies": {
19+
"prisma": "^6.18.0",
20+
"tsx": "^4.20.6"
21+
}
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from "prisma/config";
2+
import "dotenv/config";
3+
4+
export default defineConfig({
5+
schema: "prisma/schema.prisma",
6+
migrations: {
7+
path: "prisma/migrations",
8+
},
9+
engine: "classic",
10+
datasource: {
11+
url: process.env.DATABASE_URL!,
12+
},
13+
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
5+
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
6+
7+
generator client {
8+
provider = "prisma-client"
9+
output = "../prisma/generated"
10+
}
11+
12+
datasource db {
13+
provider = "postgresql"
14+
url = env("DATABASE_URL")
15+
}
16+
17+
model User {
18+
id String @id
19+
name String
20+
email String
21+
emailVerified Boolean @default(false)
22+
image String?
23+
createdAt DateTime @default(now())
24+
updatedAt DateTime @default(now()) @updatedAt
25+
sessions Session[]
26+
accounts Account[]
27+
28+
@@unique([email])
29+
@@map("user")
30+
}
31+
32+
model Session {
33+
id String @id
34+
expiresAt DateTime
35+
token String
36+
createdAt DateTime @default(now())
37+
updatedAt DateTime @updatedAt
38+
ipAddress String?
39+
userAgent String?
40+
userId String
41+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
42+
43+
@@unique([token])
44+
@@map("session")
45+
}
46+
47+
model Account {
48+
id String @id
49+
accountId String
50+
providerId String
51+
userId String
52+
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
53+
accessToken String?
54+
refreshToken String?
55+
idToken String?
56+
accessTokenExpiresAt DateTime?
57+
refreshTokenExpiresAt DateTime?
58+
scope String?
59+
password String?
60+
createdAt DateTime @default(now())
61+
updatedAt DateTime @updatedAt
62+
63+
@@map("account")
64+
}
65+
66+
model Verification {
67+
id String @id
68+
identifier String
69+
value String
70+
expiresAt DateTime
71+
createdAt DateTime @default(now())
72+
updatedAt DateTime @default(now()) @updatedAt
73+
74+
@@map("verification")
75+
}
Lines changed: 9 additions & 0 deletions
Loading

orm/betterauth-astro/src/env.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="../.astro/types.d.ts" />
2+
3+
declare namespace App {
4+
interface Locals {
5+
user: import("better-auth").User | null;
6+
session: import("better-auth").Session | null;
7+
}
8+
}
9+
10+
interface ImportMetaEnv {
11+
readonly DATABASE_URL: string;
12+
}
13+
14+
interface ImportMeta {
15+
readonly env: ImportMetaEnv;
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createAuthClient } from "better-auth/client";
2+
3+
export const authClient = createAuthClient();
4+
5+
export const { signIn, signUp, signOut } = authClient;

0 commit comments

Comments
 (0)