Skip to content

Commit d563f90

Browse files
author
Lennart Zellmer
committed
Init
Add dynamic module options Add defu dependency explicitly Create npm-publish.yml
0 parents  commit d563f90

File tree

16 files changed

+5943
-0
lines changed

16 files changed

+5943
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
indent_size = 2
5+
indent_style = space
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

.eslintrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": [
3+
"@nuxtjs/eslint-config-typescript"
4+
],
5+
"rules": {
6+
"@typescript-eslint/no-unused-vars": [
7+
"off"
8+
]
9+
}
10+
}

.github/workflows/npm-publish.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3+
4+
name: Node.js Package
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
publish-npm:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-node@v3
16+
with:
17+
node-version: '16.x'
18+
registry-url: 'https://registry.npmjs.org'
19+
scope: '@zellart'
20+
- run: yarn install --frozen-lockfile
21+
- run: yarn dev:prepare
22+
- run: yarn prepack
23+
- run: yarn publish --access public
24+
env:
25+
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}

.gitignore

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Dependencies
2+
node_modules
3+
4+
# Logs
5+
*.log*
6+
7+
# Temp directories
8+
.temp
9+
.tmp
10+
.cache
11+
12+
# Yarn
13+
**/.yarn/cache
14+
**/.yarn/*state*
15+
16+
# Generated dirs
17+
dist
18+
19+
# Nuxt
20+
.nuxt
21+
.output
22+
.vercel_build_output
23+
.build-*
24+
.env
25+
.netlify
26+
27+
# Env
28+
.env
29+
30+
# Testing
31+
reports
32+
coverage
33+
*.lcov
34+
.nyc_output
35+
36+
# VSCode
37+
.vscode/*
38+
!.vscode/settings.json
39+
!.vscode/tasks.json
40+
!.vscode/launch.json
41+
!.vscode/extensions.json
42+
!.vscode/*.code-snippets
43+
44+
# Intellij idea
45+
*.iml
46+
.idea
47+
48+
# OSX
49+
.DS_Store
50+
.AppleDouble
51+
.LSOverride
52+
.AppleDB
53+
.AppleDesktop
54+
Network Trash Folder
55+
Temporary Items
56+
.apdisk

.nuxtrc

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
imports.autoImport=false
2+
typescript.includeWorkspace=true

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Nuxt Module
2+
3+
## Development
4+
5+
- Run `npm run dev:prepare` to generate type stubs.
6+
- Use `npm run dev` to start [playground](./playground) in development mode.

package.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@zellart/nuxt-basic-auth",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"type": "module",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/lennartzellmer/nuxt-basic-auth.git"
9+
},
10+
"exports": {
11+
".": {
12+
"import": "./dist/module.mjs",
13+
"require": "./dist/module.cjs"
14+
}
15+
},
16+
"main": "./dist/module.cjs",
17+
"types": "./dist/types.d.ts",
18+
"files": [
19+
"dist"
20+
],
21+
"scripts": {
22+
"prepack": "nuxt-module-build",
23+
"dev": "nuxi dev playground",
24+
"dev:build": "nuxi build playground",
25+
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
26+
},
27+
"dependencies": {
28+
"@nuxt/kit": "^3.0.0",
29+
"defu": "^6.1.1"
30+
},
31+
"devDependencies": {
32+
"@nuxt/module-builder": "^0.2.1",
33+
"@nuxt/schema": "^3.0.0",
34+
"@nuxtjs/eslint-config-typescript": "^12.0.0",
35+
"eslint": "^8.29.0",
36+
"nuxt": "^3.0.0"
37+
}
38+
}

playground/app.vue

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<template>
2+
<div>
3+
Nuxt module playground!
4+
</div>
5+
</template>
6+
7+
<script setup>
8+
</script>

playground/nuxt.config.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineNuxtConfig } from 'nuxt/config'
2+
import MyModule from '..'
3+
4+
export default defineNuxtConfig({
5+
modules: [MyModule],
6+
basicAuth: {
7+
password: 'password',
8+
username: 'username',
9+
realm: 'defaultRealm',
10+
excludedRoutes: ['/health'],
11+
enableOnDev: false
12+
}
13+
})

playground/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"private": true,
3+
"name": "playground"
4+
}

playground/server/routes/health.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineEventHandler } from 'h3'
2+
3+
export default defineEventHandler(() => {
4+
return {
5+
state: 'healthy'
6+
}
7+
})

src/module.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { defineNuxtModule, addServerHandler, createResolver } from '@nuxt/kit'
2+
import { defu } from 'defu'
3+
4+
export interface ModuleOptions {
5+
username?: string
6+
password?: string
7+
realm: string
8+
whitelistedRoutes: string[]
9+
enabled: boolean
10+
}
11+
12+
export default defineNuxtModule<ModuleOptions>({
13+
meta: {
14+
name: 'nuxt-basic-auth',
15+
configKey: 'basicAuth'
16+
},
17+
defaults: {
18+
username: process.env.BASIC_AUTH_USERNAME,
19+
password: process.env.BASIC_AUTH_PASSWORD,
20+
realm: process.env.BASIC_AUTH_REALM || 'default',
21+
whitelistedRoutes: [],
22+
enabled: true
23+
},
24+
setup (options, nuxt) {
25+
// Check if enabled
26+
if (!options.enabled) {
27+
return
28+
}
29+
// Check if config is present
30+
if (!options.username) {
31+
throw new Error('BasicAuth username not set. Either use runtimeConfig or \'.env\' with BASIC_AUTH_USERNAME')
32+
}
33+
if (!options.password) {
34+
throw new Error('BasicAuth password not set. Either use runtimeConfig or \'.env\' with BASIC_AUTH_PASSWORD')
35+
}
36+
37+
// Copy module config in server runtimeConfig
38+
nuxt.options.runtimeConfig.basicAuth = defu(nuxt.options.runtimeConfig.basicAuth, options)
39+
40+
// Transpile runtime
41+
const { resolve } = createResolver(import.meta.url)
42+
const runtimeDir = resolve('./runtime')
43+
nuxt.options.build.transpile.push(runtimeDir)
44+
45+
addServerHandler({
46+
middleware: true,
47+
handler: resolve(runtimeDir, 'server/middleware/serverHandler')
48+
})
49+
}
50+
})
51+
52+
declare module '@nuxt/schema' {
53+
interface RuntimeConfig
54+
{
55+
basicAuth: ModuleOptions
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { defineEventHandler } from 'h3'
2+
import { useRuntimeConfig } from '#imports'
3+
4+
export default defineEventHandler((event) => {
5+
const {
6+
username,
7+
password,
8+
realm,
9+
whitelistedRoutes
10+
} = useRuntimeConfig().basicAuth
11+
12+
// Allow access on whitelisted routes
13+
if (whitelistedRoutes.includes(event.node.req.url || '')) {
14+
return
15+
}
16+
17+
// Check authentication
18+
let authenticated = false
19+
const base64Credentials = event.node.req.headers?.authorization?.split(' ')?.[1]
20+
if (base64Credentials) {
21+
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii')
22+
const [credentialsUsername, credentialsPassword] = credentials.split(':')
23+
authenticated = credentialsUsername === username && credentialsPassword === password
24+
}
25+
26+
// Reject authentication
27+
if (!authenticated) {
28+
event.node.res.statusCode = 401
29+
event.node.res.setHeader('WWW-Authenticate', `Basic realm="${realm}"`)
30+
event.node.res.end('Unauthorized')
31+
}
32+
})

tsconfig.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./playground/.nuxt/tsconfig.json",
3+
"include": ["./src"]
4+
}

0 commit comments

Comments
 (0)