postbase 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/test.yml +74 -0
- package/CLA.md +60 -0
- package/CONTRIBUTORS.md +35 -0
- package/LICENSE +661 -0
- package/README.md +211 -0
- package/admin/404.html +33 -0
- package/admin/README.md +21 -0
- package/admin/index.html +15 -0
- package/admin/jsconfig.json +20 -0
- package/admin/lib/postbase.js +222 -0
- package/admin/package-lock.json +3746 -0
- package/admin/package.json +27 -0
- package/admin/public/assets/img/admin-ui.png +0 -0
- package/admin/public/assets/img/blank-profile-picture-960_720.webp +0 -0
- package/admin/public/assets/img/chart-active-users.png +0 -0
- package/admin/public/assets/img/icon-transparent.png +0 -0
- package/admin/src/App.jsx +48 -0
- package/admin/src/auth.js +11 -0
- package/admin/src/common/formatDateTime.js +18 -0
- package/admin/src/components/AuthPanel.jsx +88 -0
- package/admin/src/components/Header.jsx +67 -0
- package/admin/src/main.jsx +6 -0
- package/admin/src/pages/Dashboard.jsx +24 -0
- package/admin/src/pages/Home.jsx +52 -0
- package/admin/src/pages/Login.jsx +10 -0
- package/admin/src/pages/authentication/Users.jsx +199 -0
- package/admin/src/pages/firestore/Database.jsx +29 -0
- package/admin/src/pages/storage/files.jsx +29 -0
- package/admin/src/postbase.js +15 -0
- package/admin/src/styles.css +3 -0
- package/admin/tailwind.config.cjs +11 -0
- package/admin/template.env +2 -0
- package/admin/vite.config.js +21 -0
- package/assets/img/HomePageScreenshot.png +0 -0
- package/assets/img/better-auth-logo-dark.136b122f.png +0 -0
- package/assets/img/better-auth-logo-light.4b03f444.png +0 -0
- package/assets/img/expresjs.png +0 -0
- package/assets/img/icon-transparent.png +0 -0
- package/assets/img/icon.png +0 -0
- package/assets/img/letsencrypt-logo-horizontal.png +0 -0
- package/assets/img/logo.png +0 -0
- package/assets/img/node.js_logo.png +0 -0
- package/assets/img/nodejsLight.svg +39 -0
- package/assets/img/postgres.png +0 -0
- package/backend/README.md +49 -0
- package/backend/admin/auth.js +9 -0
- package/backend/app.js +68 -0
- package/backend/auth.js +92 -0
- package/backend/env.js +12 -0
- package/backend/lib/postbase/adminClient.js +520 -0
- package/backend/lib/postbase/compat/admin.js +44 -0
- package/backend/lib/postbase/db.js +17 -0
- package/backend/lib/postbase/genericRouter.js +603 -0
- package/backend/lib/postbase/local-storage.js +56 -0
- package/backend/lib/postbase/metadataCache.js +32 -0
- package/backend/lib/postbase/middlewares/auth.js +57 -0
- package/backend/lib/postbase/migrations/1765239687559_rtdb-nodes.js +93 -0
- package/backend/lib/postbase/package-lock.json +5873 -0
- package/backend/lib/postbase/package.json +19 -0
- package/backend/lib/postbase/rtdb/router.js +190 -0
- package/backend/lib/postbase/rtdb/rulesEngine.js +63 -0
- package/backend/lib/postbase/rtdb/ws.js +84 -0
- package/backend/lib/postbase/rulesEngine.js +62 -0
- package/backend/lib/postbase/storage.js +130 -0
- package/backend/lib/postbase/tests/README.md +22 -0
- package/backend/lib/postbase/tests/db.js +9 -0
- package/backend/lib/postbase/tests/rtdb.rest.test.js +46 -0
- package/backend/lib/postbase/tests/rtdb.ws.test.js +113 -0
- package/backend/lib/postbase/tests/rules.js +26 -0
- package/backend/lib/postbase/tests/testServer.js +46 -0
- package/backend/lib/postbase/websocket.js +131 -0
- package/backend/local.js +6 -0
- package/backend/main.js +20 -0
- package/backend/middlewares/auth_middleware.js +10 -0
- package/backend/migrations/1762137399366-init.sql +98 -0
- package/backend/migrations/1762137399367_init_jsonb_schema.js +68 -0
- package/backend/migrations/1762149999999_enable_realtime_changes.js +48 -0
- package/backend/migrations/1765224247654_rtdb-nodes.js +93 -0
- package/backend/package-lock.json +2374 -0
- package/backend/package.json +27 -0
- package/backend/postbase_db_rules.js +128 -0
- package/backend/postbase_rtdb_rules.js +27 -0
- package/backend/postbase_storage_rules.js +45 -0
- package/backend/template.env +10 -0
- package/backend-systemd/README.md +39 -0
- package/backend-systemd/your_website.com.service +12 -0
- package/frontend/404.html +33 -0
- package/frontend/README.md +25 -0
- package/frontend/index.html +15 -0
- package/frontend/jsconfig.json +20 -0
- package/frontend/lib/postbase/auth.js +132 -0
- package/frontend/lib/postbase/compat/firebase/app.js +3 -0
- package/frontend/lib/postbase/compat/firebase/auth.js +115 -0
- package/frontend/lib/postbase/compat/firebase/database.js +11 -0
- package/frontend/lib/postbase/compat/firebase/firestore/lite.js +61 -0
- package/frontend/lib/postbase/compat/firebase/storage.js +10 -0
- package/frontend/lib/postbase/db.js +657 -0
- package/frontend/lib/postbase/package-lock.json +6284 -0
- package/frontend/lib/postbase/package.json +17 -0
- package/frontend/lib/postbase/rtdb.js +108 -0
- package/frontend/lib/postbase/storage.js +293 -0
- package/frontend/lib/postbase/tests/rtdb.client.test.js +88 -0
- package/frontend/lib/postbase/tests/waitFor.js +13 -0
- package/frontend/lib/postbase/utils.js +1 -0
- package/frontend/package-lock.json +2977 -0
- package/frontend/package.json +24 -0
- package/frontend/src/App.jsx +38 -0
- package/frontend/src/auth.js +52 -0
- package/frontend/src/components/AuthPanel.jsx +85 -0
- package/frontend/src/components/Header.jsx +54 -0
- package/frontend/src/main.jsx +5 -0
- package/frontend/src/pages/Dashboard.jsx +24 -0
- package/frontend/src/pages/Home.jsx +178 -0
- package/frontend/src/pages/Login.jsx +10 -0
- package/frontend/src/postbase.js +14 -0
- package/frontend/src/styles.css +1 -0
- package/frontend/tailwind.config.cjs +11 -0
- package/frontend/template.env +2 -0
- package/frontend/vite.config.js +18 -0
- package/git/hooks/README.md +31 -0
- package/git/hooks/post-receive +26 -0
- package/nginx/README.md +84 -0
- package/nginx/apt/www.your_website.com.conf +80 -0
- package/nginx/homebrew/www.your_website.com.conf +80 -0
- package/nginx/letsencrypt/README +14 -0
- package/package.json +8 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getSession } from "./auth";
|
|
2
|
+
import { getDB } from "./lib/postbase";
|
|
3
|
+
|
|
4
|
+
async function getBetterAuthToken() {
|
|
5
|
+
const { data } = await getSession();
|
|
6
|
+
if (data && data.hasOwnProperty('session')) {
|
|
7
|
+
return data.session?.token || null;
|
|
8
|
+
}
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const db = getDB({
|
|
13
|
+
baseUrl: import.meta.env.VITE_API_BASE,
|
|
14
|
+
getAuthToken: getBetterAuthToken,
|
|
15
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineConfig, transformWithEsbuild } from 'vite';
|
|
2
|
+
import preact from '@preact/preset-vite';
|
|
3
|
+
import tailwindcss from '@tailwindcss/vite'
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
server: {
|
|
7
|
+
port: 5174,
|
|
8
|
+
},
|
|
9
|
+
plugins: [
|
|
10
|
+
preact({ include: /\.(mdx|js|jsx|ts|tsx)$/ }),
|
|
11
|
+
tailwindcss(),
|
|
12
|
+
],
|
|
13
|
+
optimizeDeps: {
|
|
14
|
+
exclude: [],
|
|
15
|
+
esbuildOptions: {
|
|
16
|
+
loader: {
|
|
17
|
+
'.js': 'jsx',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
});
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<svg width="267" height="80" viewBox="0 0 267 80" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<mask id="a" maskUnits="userSpaceOnUse" x="0" y="0" width="267" height="80" style="mask-type:luminance">
|
|
3
|
+
<path d="M267 0H0v79.378h267z" fill="#fff"/>
|
|
4
|
+
</mask>
|
|
5
|
+
<g mask="url(#a)">
|
|
6
|
+
<path d="M234.983 78.755a4 4 0 0 1-1.933-.51l-6.121-3.658c-.921-.51-.46-.695-.184-.787 1.242-.417 1.472-.51 2.761-1.25.138-.093.322-.047.46.046l4.694 2.825c.184.092.414.092.552 0l18.363-10.698c.183-.092.277-.277.277-.509V42.867c0-.232-.094-.417-.277-.51l-18.363-10.65c-.184-.093-.414-.093-.552 0l-18.362 10.65c-.184.093-.276.324-.276.51v21.347c0 .186.092.417.276.51l5.016 2.917c2.715 1.39 4.418-.232 4.418-1.852v-21.07c0-.278.23-.556.553-.556h2.347c.276 0 .552.232.552.556v21.07c0 3.658-1.979 5.788-5.431 5.788-1.058 0-1.886 0-4.234-1.157l-4.832-2.779a3.91 3.91 0 0 1-1.933-3.38V42.912c0-1.39.737-2.686 1.933-3.38l18.363-10.697c1.15-.649 2.715-.649 3.865 0l18.363 10.696a3.91 3.91 0 0 1 1.932 3.381V64.26c0 1.39-.736 2.686-1.932 3.381l-18.363 10.697c-.552.232-1.242.417-1.932.417" fill="#5FA04E"/>
|
|
7
|
+
<path d="M240.69 64.075c-8.055 0-9.712-3.705-9.712-6.854 0-.277.23-.555.552-.555h2.393c.277 0 .507.185.507.463.368 2.454 1.426 3.658 6.305 3.658 3.866 0 5.523-.88 5.523-2.963 0-1.204-.462-2.084-6.49-2.686-5.016-.51-8.146-1.621-8.146-5.65 0-3.75 3.13-5.974 8.376-5.974 5.892 0 8.791 2.038 9.159 6.484a.76.76 0 0 1-.137.416c-.094.093-.231.186-.371.186h-2.439a.54.54 0 0 1-.505-.417c-.553-2.547-1.98-3.38-5.753-3.38-4.234 0-4.74 1.481-4.74 2.593 0 1.342.598 1.76 6.305 2.5 5.66.741 8.33 1.806 8.33 5.788 0 4.076-3.36 6.391-9.157 6.391m26.51-22.413c0 1.945-1.612 3.566-3.546 3.566a3.556 3.556 0 0 1-3.543-3.566 3.556 3.556 0 0 1 3.543-3.565c1.888 0 3.546 1.574 3.546 3.565m-6.536 0c0 1.667 1.335 3.01 2.944 3.01 1.658 0 2.993-1.39 2.993-3.01 0-1.667-1.335-2.963-2.993-2.963a2.975 2.975 0 0 0-2.944 2.963m1.657-1.991h1.381c.46 0 1.381 0 1.381 1.065 0 .74-.462.88-.739.973.554.046.599.416.645.926.046.324.094.88.185 1.065h-.83c0-.186-.137-1.204-.137-1.25-.045-.232-.137-.325-.413-.325h-.691v1.62h-.782zm.736 1.76h.597c.508 0 .599-.37.599-.556 0-.556-.368-.556-.599-.556h-.642v1.112z" fill="#5FA04E"/>
|
|
8
|
+
<path fill-rule="evenodd" clip-rule="evenodd" d="M43.674 41.954c0-.834-.46-1.62-1.197-2.038L23.01 28.572c-.322-.185-.69-.278-1.058-.324h-.184c-.368 0-.736.139-1.059.324l-19.512 11.3A2.38 2.38 0 0 0 0 41.953l.046 30.378c0 .416.23.833.598 1.018.368.232.829.232 1.15 0l11.598-6.668a2.36 2.36 0 0 0 1.197-2.037V50.428c0-.833.46-1.62 1.196-2.037l4.924-2.871a2.2 2.2 0 0 1 1.197-.325c.414 0 .828.093 1.15.325l4.925 2.87a2.36 2.36 0 0 1 1.196 2.038v14.217c0 .833.46 1.62 1.197 2.037l11.505 6.668c.368.232.828.232 1.197 0 .368-.185.598-.602.598-1.018zM137.465.139c-.369-.185-.829-.185-1.151 0-.368.231-.598.602-.598 1.019v30.1a.89.89 0 0 1-.415.74.88.88 0 0 1-.828 0l-4.878-2.825a2.36 2.36 0 0 0-2.347 0L107.735 40.52a2.36 2.36 0 0 0-1.196 2.037V65.2c0 .834.46 1.621 1.196 2.038l19.513 11.345a2.36 2.36 0 0 0 2.347 0l19.513-11.345a2.36 2.36 0 0 0 1.196-2.038V8.752c0-.88-.46-1.667-1.196-2.084zm-1.795 57.606c0 .232-.092.417-.276.51l-6.674 3.89a.68.68 0 0 1-.598 0l-6.673-3.89c-.184-.093-.276-.325-.276-.51v-7.78c0-.231.092-.416.276-.509l6.673-3.89a.68.68 0 0 1 .598 0l6.674 3.89c.184.093.276.324.276.51zm66.728-7.965c.737-.417 1.151-1.204 1.151-2.038v-5.51c0-.834-.46-1.62-1.151-2.037l-19.375-11.3a2.36 2.36 0 0 0-2.347 0l-19.512 11.346a2.36 2.36 0 0 0-1.197 2.037v22.645c0 .833.46 1.62 1.197 2.037l19.374 11.114c.737.416 1.611.416 2.301 0l11.736-6.576c.368-.185.598-.602.598-1.019s-.23-.833-.598-1.018L174.97 58.115c-.368-.231-.598-.602-.598-1.018v-7.085c0-.417.23-.834.598-1.02l6.121-3.518a1.12 1.12 0 0 1 1.196 0l6.121 3.519c.368.231.598.602.598 1.019v5.557c0 .416.23.833.599 1.018.368.232.828.232 1.196 0z" fill="#fff"/>
|
|
9
|
+
<path fill-rule="evenodd" clip-rule="evenodd" d="M181.551 48.716a.42.42 0 0 1 .461 0l3.727 2.176c.138.092.23.231.23.417v4.352a.49.49 0 0 1-.23.417l-3.727 2.177a.42.42 0 0 1-.461 0l-3.727-2.177a.49.49 0 0 1-.23-.417V51.31c0-.185.092-.325.23-.417z" fill="#5FA04E"/>
|
|
10
|
+
<g transform="translate(53.428, 28.628)">
|
|
11
|
+
<path fill="url(#b)" d="M22.873.417a2.36 2.36 0 0 0-2.348 0L1.151 11.669C.414 12.086 0 12.873 0 13.707v22.551c0 .834.46 1.621 1.15 2.038l19.375 11.253a2.36 2.36 0 0 0 2.348 0l19.374-11.253c.737-.417 1.15-1.204 1.15-2.038V13.707c0-.834-.46-1.62-1.15-2.038z"/>
|
|
12
|
+
<path fill="url(#c)" clip-path="url(#d)" d="m21.699-1.047 21.506 12.995L21.7 51.073.153 38.055z"/>
|
|
13
|
+
<path fill="url(#e)" clip-path="url(#d)" d="M21.699-1.047.153 11.948l21.546 39.125 21.506-13.018z"/>
|
|
14
|
+
</g>
|
|
15
|
+
</g>
|
|
16
|
+
<defs>
|
|
17
|
+
<linearGradient id="b" x1="30.33" y1="8.56" x2="14.9" y2="44.7" gradientUnits="userSpaceOnUse">
|
|
18
|
+
<stop stop-color="#3F8B3D"/>
|
|
19
|
+
<stop offset=".64" stop-color="#3F873F"/>
|
|
20
|
+
<stop offset=".93" stop-color="#3DA92E"/>
|
|
21
|
+
<stop offset="1" stop-color="#3DAE2B"/>
|
|
22
|
+
</linearGradient>
|
|
23
|
+
<linearGradient id="c" x1="18.8" y1="26.8" x2="68" y2=".4" gradientUnits="userSpaceOnUse">
|
|
24
|
+
<stop offset=".14" stop-color="#3F873F"/>
|
|
25
|
+
<stop offset=".4" stop-color="#52A044"/>
|
|
26
|
+
<stop offset=".71" stop-color="#64B749"/>
|
|
27
|
+
<stop offset=".91" stop-color="#6ABF4B"/>
|
|
28
|
+
</linearGradient>
|
|
29
|
+
<linearGradient id="e" x1=".25" y1="24.5" x2="44" y2="24.5" gradientUnits="userSpaceOnUse">
|
|
30
|
+
<stop offset=".09" stop-color="#6ABF4B"/>
|
|
31
|
+
<stop offset=".29" stop-color="#64B749"/>
|
|
32
|
+
<stop offset=".6" stop-color="#52A044"/>
|
|
33
|
+
<stop offset=".86" stop-color="#3F873F"/>
|
|
34
|
+
</linearGradient>
|
|
35
|
+
<clipPath id="d">
|
|
36
|
+
<path d="M22.873.417a2.36 2.36 0 0 0-2.348 0L1.151 11.669C.414 12.086 0 12.873 0 13.707v22.551c0 .834.46 1.621 1.15 2.038l19.375 11.253a2.36 2.36 0 0 0 2.348 0l19.374-11.253c.737-.417 1.15-1.204 1.15-2.038V13.707c0-.834-.46-1.62-1.15-2.038z"/>
|
|
37
|
+
</clipPath>
|
|
38
|
+
</defs>
|
|
39
|
+
</svg>
|
|
Binary file
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Postbase Backend
|
|
2
|
+
|
|
3
|
+
Custom backend code written in Node.js and Express.js to use Firebase like API against PostgreSQL.
|
|
4
|
+
|
|
5
|
+
Also support Firebase like Rule based security. See backend/postbase_db_rules.js
|
|
6
|
+
|
|
7
|
+
Follow the steps below to setup this boilerplate
|
|
8
|
+
|
|
9
|
+
### Todo
|
|
10
|
+
|
|
11
|
+
- [ ] Install PostgreSQL - Easiest is to use Docker (Don't forget to set PGDATA=/var/lib/postgresql/data/pgdata)
|
|
12
|
+
```
|
|
13
|
+
docker volume create postgres_data
|
|
14
|
+
docker run --name postgres -d \
|
|
15
|
+
-p 5432:5432 \
|
|
16
|
+
-e POSTGRES_PASSWORD=yoursecretpassword \
|
|
17
|
+
-e PGDATA=/var/lib/postgresql/data \
|
|
18
|
+
--mount source=postgres_data,target=/var/lib/postgresql/data \
|
|
19
|
+
--restart=unless-stopped \
|
|
20
|
+
postgres
|
|
21
|
+
```
|
|
22
|
+
- [ ] Setup BetterAuth - https://www.better-auth.com/docs/installation#create-database-tables
|
|
23
|
+
- [ ] Move template.env to .env and set all the environment variables
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
cd postbase/backend
|
|
27
|
+
npm install
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
- [ ] Design or copy schema from backend/migrations/1762137399367_init_jsonb_schema.js and run migrations
|
|
31
|
+
```
|
|
32
|
+
npm run migrate:up
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
* Note: Because of PostgreSQL's pg_notify() function, JSONB data is limited to 8000 bytes (1 char = 1 byte). To avoid this limitation, do not use 3rd migration.
|
|
36
|
+
|
|
37
|
+
Learn more from https://synvinkel.org/notes/node-postgres-migrations
|
|
38
|
+
|
|
39
|
+
## Local (HTTP)
|
|
40
|
+
|
|
41
|
+
```
|
|
42
|
+
node local.js
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Production (HTTPS)
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
node main.js
|
|
49
|
+
```
|
package/backend/app.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
//import cors from 'cors';
|
|
3
|
+
import { WebSocketServer } from 'ws';
|
|
4
|
+
import { toNodeHandler } from "better-auth/node";
|
|
5
|
+
|
|
6
|
+
import { createPool } from './lib/postbase/db.js';
|
|
7
|
+
import { makeGenericRouter } from './lib/postbase/genericRouter.js';
|
|
8
|
+
import { createStorageRouter } from './lib/postbase/storage.js';
|
|
9
|
+
import { createLocalStorage } from './lib/postbase/local-storage.js';
|
|
10
|
+
import { createRtdbWs } from './lib/postbase/rtdb/ws.js';
|
|
11
|
+
import { createRtdbRouter } from './lib/postbase/rtdb/router.js';
|
|
12
|
+
//import { makePostbaseAdminClient } from './postbase/adminClient.js';
|
|
13
|
+
import rulesModuleDB from './postbase_db_rules.js';
|
|
14
|
+
import rulesModuleStorage from './postbase_storage_rules.js';
|
|
15
|
+
import rulesModuleRTDB from './postbase_rtdb_rules.js';
|
|
16
|
+
import { authenticate } from './middlewares/auth_middleware.js';
|
|
17
|
+
import { auth } from './auth.js';
|
|
18
|
+
|
|
19
|
+
const POSTBASE_STORAGE_ROOT_DIR = process.env.POSTBASE_STORAGE_ROOT_DIR || '/var/www/html/www.yourwebsite.com/uploads';
|
|
20
|
+
const POSTBASE_STORAGE_PUBLIC_URL = process.env.POSTBASE_STORAGE_PUBLIC_URL || 'http://localhost:5173/uploads';
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
// Initialize DB pool using env variables
|
|
24
|
+
const pool = createPool({
|
|
25
|
+
connectionString: process.env.DATABASE_URL
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// This is firestore alternative
|
|
29
|
+
//const db = makePostbaseAdminClient({ pool });
|
|
30
|
+
|
|
31
|
+
// This is firebase storage alternative
|
|
32
|
+
const bucket = createLocalStorage(
|
|
33
|
+
POSTBASE_STORAGE_ROOT_DIR,
|
|
34
|
+
POSTBASE_STORAGE_PUBLIC_URL // this is needed for making public urls
|
|
35
|
+
).bucket();
|
|
36
|
+
|
|
37
|
+
export const app = express();
|
|
38
|
+
const router = express.Router();
|
|
39
|
+
const wss = new WebSocketServer({ noServer: true });
|
|
40
|
+
|
|
41
|
+
// BetterAuth
|
|
42
|
+
router.all("/auth/*", toNodeHandler(auth));
|
|
43
|
+
|
|
44
|
+
const genericRouter = makeGenericRouter({ pool, rulesModule: rulesModuleDB, authField: 'auth' });
|
|
45
|
+
router.use('/db', authenticate, genericRouter);
|
|
46
|
+
|
|
47
|
+
router.use('/storage', authenticate, createStorageRouter(POSTBASE_STORAGE_ROOT_DIR, bucket, rulesModuleStorage));
|
|
48
|
+
|
|
49
|
+
const rtdbWs = createRtdbWs(wss);
|
|
50
|
+
|
|
51
|
+
router.use(
|
|
52
|
+
'/rtdb',
|
|
53
|
+
authenticate,
|
|
54
|
+
createRtdbRouter({
|
|
55
|
+
pool,
|
|
56
|
+
notify: rtdbWs.notify,
|
|
57
|
+
rulesModule: rulesModuleRTDB,
|
|
58
|
+
})
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// For local testing
|
|
62
|
+
// app.use(cors({
|
|
63
|
+
// origin: ["http://localhost:5173", "http://localhost:5174"],
|
|
64
|
+
// methods: ["GET", "POST", "PUT", "DELETE"],
|
|
65
|
+
// credentials: true,
|
|
66
|
+
// }));
|
|
67
|
+
app.use(express.json());
|
|
68
|
+
app.use('/api', router);
|
package/backend/auth.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { betterAuth } from "better-auth";
|
|
2
|
+
import { admin } from "better-auth/plugins"
|
|
3
|
+
import { createPool } from "./lib/postbase/db.js";
|
|
4
|
+
//import { phoneNumber } from "better-auth/plugins"
|
|
5
|
+
//import { makePostbaseAdminClient } from "./lib/postbase/adminClient.js";
|
|
6
|
+
|
|
7
|
+
const pool = createPool({
|
|
8
|
+
connectionString: process.env.DATABASE_URL
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// Enable if required
|
|
12
|
+
//const db = makePostbaseAdminClient({ pool });
|
|
13
|
+
|
|
14
|
+
export const auth = betterAuth({
|
|
15
|
+
// Following is only needed for local testing
|
|
16
|
+
// You can avoid this by using /etc/hosts and nginx servers
|
|
17
|
+
// baseURL: 'http://localhost:8081',
|
|
18
|
+
// trustedOrigins: ["http://localhost:8081", "http://localhost:5173", "http://localhost:5174"],
|
|
19
|
+
// advanced: {
|
|
20
|
+
// defaultCookieAttributes: {
|
|
21
|
+
// sameSite: "none",
|
|
22
|
+
// secure: true,
|
|
23
|
+
// httpOnly: true,
|
|
24
|
+
// },
|
|
25
|
+
// crossSubDomainCookies: {
|
|
26
|
+
// enabled: true,
|
|
27
|
+
// domain: "localhost",
|
|
28
|
+
// },
|
|
29
|
+
// },
|
|
30
|
+
database: pool,
|
|
31
|
+
emailAndPassword: {
|
|
32
|
+
enabled: true,
|
|
33
|
+
sendResetPassword: async ({ user, url, token }, request) => {
|
|
34
|
+
// await sendEmail({
|
|
35
|
+
// to: user.email,
|
|
36
|
+
// subject: "Reset your password",
|
|
37
|
+
// text: `Click the link to reset your password: ${url}`,
|
|
38
|
+
// });
|
|
39
|
+
},
|
|
40
|
+
// onPasswordReset: async ({ user }, request) => {
|
|
41
|
+
// // your logic here
|
|
42
|
+
// console.log(`Password for user ${user.email} has been reset.`);
|
|
43
|
+
// },
|
|
44
|
+
},
|
|
45
|
+
// emailVerification: {
|
|
46
|
+
// sendVerificationEmail: async ({ user, url }) => {
|
|
47
|
+
// // Send email using third-party APIs or your own SMTP server
|
|
48
|
+
// await sendEmail({
|
|
49
|
+
// to: user.email,
|
|
50
|
+
// subject: "Verify your email address",
|
|
51
|
+
// text: `Click the link to verify your email: ${url}`,
|
|
52
|
+
// });
|
|
53
|
+
// },
|
|
54
|
+
// sendOnSignIn: true,
|
|
55
|
+
// },
|
|
56
|
+
socialProviders: {
|
|
57
|
+
// Enable following for Sign in with Google
|
|
58
|
+
// google: {
|
|
59
|
+
// prompt: "select_account",
|
|
60
|
+
// clientId: process.env.GOOGLE_CLIENT_ID,
|
|
61
|
+
// clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
|
62
|
+
// },
|
|
63
|
+
},
|
|
64
|
+
user: {
|
|
65
|
+
deleteUser: {
|
|
66
|
+
enabled: true, // this is required to delete the user
|
|
67
|
+
beforeDelete: async (user) => {
|
|
68
|
+
// Optionally delete relevant relation if you were storing any
|
|
69
|
+
// try {
|
|
70
|
+
// await db.collection('users').doc(user.id).delete();
|
|
71
|
+
// } catch (err) {
|
|
72
|
+
// console.error('Error cleaning up users', err);
|
|
73
|
+
// }
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
plugins: [
|
|
78
|
+
admin(
|
|
79
|
+
// {
|
|
80
|
+
// // add user id for an admin
|
|
81
|
+
// adminUserIds: [
|
|
82
|
+
// process.env.ADMIN_USER_ID,
|
|
83
|
+
// ],
|
|
84
|
+
// }
|
|
85
|
+
),
|
|
86
|
+
// phoneNumber({
|
|
87
|
+
// sendOTP: ({ phoneNumber, code }, request) => {
|
|
88
|
+
// // Implement sending OTP code via SMS
|
|
89
|
+
// }
|
|
90
|
+
// })
|
|
91
|
+
]
|
|
92
|
+
});
|
package/backend/env.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
6
|
+
const __dirname = path.dirname(__filename);
|
|
7
|
+
|
|
8
|
+
const dotenvPath = path.join(__dirname, '.env');
|
|
9
|
+
console.log('Loading .env from:', dotenvPath);
|
|
10
|
+
dotenv.config({
|
|
11
|
+
path: dotenvPath,
|
|
12
|
+
});
|