feat: add basic auth to analytics

This commit is contained in:
wancat committed 2023-11-18 22:22:34 +08:00
1 parent 8430ce4428
commit 1ab394f187
4 files changed
+37

No files matched your search

+1
View File
@@ -12,6 +12,7 @@
"dependencies": {
"@linux-odyssey/models": "*",
"express": "^4.18.2",
"express-basic-auth": "^1.2.1",
"express-handlebars": "^7.1.2"
},
"devDependencies": {
+9
View File
@@ -1,7 +1,16 @@
function getOrFailed(key) {
if (!process.env[key]) {
throw new Error(`Missing required environment variable: ${key}`)
}
return process.env[key]
}
const config = {
host: process.env.HOST || 'localhost',
port: process.env.PORT || 3001,
db: process.env.MONGO_URL || 'mongodb://localhost:27017/odyssey-test',
username: getOrFailed('USERNAME'),
password: getOrFailed('PASSWORD'),
}
config.baseUrl = process.env.BASE_URL || `http://${config.host}:${config.port}`
+8
View File
@@ -1,6 +1,7 @@
import connectDB from '@linux-odyssey/models'
import express from 'express'
import { engine } from 'express-handlebars'
import basicAuth from 'express-basic-auth'
import config from './config.js'
import router from './router.js'
@@ -13,6 +14,13 @@ async function main() {
app.set('view engine', 'handlebars')
app.set('views', './views')
app.use(
basicAuth({
users: { [config.username]: config.password },
challenge: true,
realm: 'Linux Odyssey Admin Dashboard',
})
)
app.use(router)
app.use(express.static('./public'))