-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: admin login to use admin table (#14)
- Loading branch information
1 parent
b8d0230
commit 42f236a
Showing
11 changed files
with
160 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
version: '3' | ||
|
||
services: | ||
authcompanion: | ||
build: . | ||
container_name: authcompanion | ||
environment: | ||
- DB_PATH=./authdata/authcompanion_users.db | ||
- ADMIN_KEY_PATH=./authdata/adminkey | ||
- KEY_PATH=./authdata/serverkey | ||
ports: | ||
- "3002:3002" | ||
volumes: | ||
- authdata:/home/nodejs/app/authdata | ||
|
||
|
||
volumes: | ||
authdata: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,67 @@ | ||
import { readFileSync, existsSync } from "fs"; | ||
import Database from "better-sqlite3"; | ||
import config from "../../config.js"; | ||
import fastifyPlugin from "fastify-plugin"; | ||
import { readFileSync, existsSync, readdirSync } from 'fs'; | ||
import { extname } from 'path'; | ||
import Database from 'better-sqlite3'; | ||
import config from '../../config.js'; | ||
import fastifyPlugin from 'fastify-plugin'; | ||
|
||
const VERSION = 2; | ||
|
||
const migrate = (db, version) => { | ||
const allFiles = readdirSync('./plugins/db/schema/'); | ||
const sqlFiles = allFiles.filter((file) => extname(file) === '.sql'); | ||
sqlFiles.sort(); | ||
if (version === null || version === undefined) { | ||
version = 1; | ||
} | ||
|
||
for (const sqlFile of sqlFiles) { | ||
if (!sqlFile.includes(`${version}`)) { | ||
continue; | ||
} | ||
console.log(`Migrating to ${sqlFile}`); | ||
const migration = readFileSync(`./plugins/db/schema/${sqlFile}`, 'utf8'); | ||
db.exec(migration); | ||
version++; | ||
} | ||
}; | ||
|
||
const dbPlugin = async function (fastify) { | ||
let db = {}; | ||
try { | ||
//create test database to support CI | ||
if (process.env.NODE_ENV === "test") { | ||
config.DBPATH = "./test.db"; | ||
console.log("Test database - ENABLED"); | ||
if (process.env.NODE_ENV === 'test') { | ||
config.DBPATH = './test.db'; | ||
console.log('Test database - ENABLED'); | ||
} | ||
|
||
//make sure the database is available, if not, create one | ||
if (!existsSync(config.DBPATH)) { | ||
//create database if it does not exist and migrate | ||
db = new Database(config.DBPATH); | ||
db.pragma("journal_mode = WAL"); | ||
|
||
const migration = readFileSync("./plugins/db/1__main.sql", "utf8"); | ||
db.exec(migration); | ||
|
||
db.pragma('journal_mode = WAL'); | ||
migrate(db, 1); | ||
fastify.log.info(`Generated Sqlite3 Database: ${config.DBPATH}...`); | ||
} else { | ||
db = new Database(config.DBPATH); | ||
db.pragma("journal_mode = WAL"); | ||
db.pragma('journal_mode = WAL'); | ||
|
||
//if the database is available, make sure it has the right schema | ||
const stmt = db.prepare("SELECT version from authc_version"); | ||
const stmt = db.prepare('SELECT version from authc_version'); | ||
const { version } = stmt.get(); | ||
|
||
if (!version || version !== 1) { | ||
throw new Error("Database is an unexpected version, please try again"); | ||
if (!version || version > VERSION) { | ||
throw new Error('Database is an unexpected version, please try again'); | ||
} else if (version < VERSION) { | ||
migrate(db, VERSION); | ||
} | ||
} | ||
fastify.log.info(`Using Sqlite3 Database: ${config.DBPATH}`); | ||
} catch (error) { | ||
console.log(error); | ||
throw new Error( | ||
"There was an error setting and connecting up the Database, please try again!" | ||
); | ||
throw new Error('There was an error setting and connecting up the Database, please try again!'); | ||
} | ||
//make available the database across the server by calling "db" | ||
fastify.decorate("db", db); | ||
fastify.decorate('db', db); | ||
}; | ||
//wrap the function with the fastly plugin to expose outside of the registered scope | ||
export default fastifyPlugin(dbPlugin, { fastify: "4.x" }); | ||
export default fastifyPlugin(dbPlugin, { fastify: '4.x' }); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
BEGIN TRANSACTION; | ||
CREATE TABLE admin ( | ||
id INTEGER PRIMARY KEY ASC, | ||
uuid TEXT NOT NULL, | ||
name TEXT NOT NULL, | ||
email TEXT NOT NULL UNIQUE, | ||
password TEXT NOT NULL, | ||
challenge TEXT, | ||
jwt_id TEXT NOT NULL, | ||
active INTEGER NOT NULL, | ||
created_at TEXT NOT NULL, | ||
updated_at TEXT NOT NULL | ||
); | ||
|
||
UPDATE authc_version SET version = 2 WHERE version = 1; | ||
|
||
COMMIT TRANSACTION; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.