Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
Add dynamic module options

Add defu dependency explicitly

Create npm-publish.yml
  • Loading branch information
Lennart Zellmer committed Dec 7, 2022
0 parents commit d563f90
Show file tree
Hide file tree
Showing 16 changed files with 5,943 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_size = 2
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
node_modules
10 changes: 10 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": [
"@nuxtjs/eslint-config-typescript"
],
"rules": {
"@typescript-eslint/no-unused-vars": [
"off"
]
}
}
25 changes: 25 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
publish-npm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
scope: '@zellart'
- run: yarn install --frozen-lockfile
- run: yarn dev:prepare
- run: yarn prepack
- run: yarn publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTH_TOKEN}}
56 changes: 56 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Dependencies
node_modules

# Logs
*.log*

# Temp directories
.temp
.tmp
.cache

# Yarn
**/.yarn/cache
**/.yarn/*state*

# Generated dirs
dist

# Nuxt
.nuxt
.output
.vercel_build_output
.build-*
.env
.netlify

# Env
.env

# Testing
reports
coverage
*.lcov
.nyc_output

# VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Intellij idea
*.iml
.idea

# OSX
.DS_Store
.AppleDouble
.LSOverride
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
2 changes: 2 additions & 0 deletions .nuxtrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports.autoImport=false
typescript.includeWorkspace=true
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Nuxt Module

## Development

- Run `npm run dev:prepare` to generate type stubs.
- Use `npm run dev` to start [playground](./playground) in development mode.
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@zellart/nuxt-basic-auth",
"version": "1.0.0",
"license": "MIT",
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/lennartzellmer/nuxt-basic-auth.git"
},
"exports": {
".": {
"import": "./dist/module.mjs",
"require": "./dist/module.cjs"
}
},
"main": "./dist/module.cjs",
"types": "./dist/types.d.ts",
"files": [
"dist"
],
"scripts": {
"prepack": "nuxt-module-build",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
},
"dependencies": {
"@nuxt/kit": "^3.0.0",
"defu": "^6.1.1"
},
"devDependencies": {
"@nuxt/module-builder": "^0.2.1",
"@nuxt/schema": "^3.0.0",
"@nuxtjs/eslint-config-typescript": "^12.0.0",
"eslint": "^8.29.0",
"nuxt": "^3.0.0"
}
}
8 changes: 8 additions & 0 deletions playground/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<div>
Nuxt module playground!
</div>
</template>

<script setup>
</script>
13 changes: 13 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { defineNuxtConfig } from 'nuxt/config'
import MyModule from '..'

export default defineNuxtConfig({
modules: [MyModule],
basicAuth: {
password: 'password',
username: 'username',
realm: 'defaultRealm',
excludedRoutes: ['/health'],
enableOnDev: false
}
})
4 changes: 4 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"name": "playground"
}
7 changes: 7 additions & 0 deletions playground/server/routes/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineEventHandler } from 'h3'

export default defineEventHandler(() => {
return {
state: 'healthy'
}
})
57 changes: 57 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { defineNuxtModule, addServerHandler, createResolver } from '@nuxt/kit'
import { defu } from 'defu'

export interface ModuleOptions {
username?: string
password?: string
realm: string
whitelistedRoutes: string[]
enabled: boolean
}

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'nuxt-basic-auth',
configKey: 'basicAuth'
},
defaults: {
username: process.env.BASIC_AUTH_USERNAME,
password: process.env.BASIC_AUTH_PASSWORD,
realm: process.env.BASIC_AUTH_REALM || 'default',
whitelistedRoutes: [],
enabled: true
},
setup (options, nuxt) {
// Check if enabled
if (!options.enabled) {
return
}
// Check if config is present
if (!options.username) {
throw new Error('BasicAuth username not set. Either use runtimeConfig or \'.env\' with BASIC_AUTH_USERNAME')
}
if (!options.password) {
throw new Error('BasicAuth password not set. Either use runtimeConfig or \'.env\' with BASIC_AUTH_PASSWORD')
}

// Copy module config in server runtimeConfig
nuxt.options.runtimeConfig.basicAuth = defu(nuxt.options.runtimeConfig.basicAuth, options)

// Transpile runtime
const { resolve } = createResolver(import.meta.url)
const runtimeDir = resolve('./runtime')
nuxt.options.build.transpile.push(runtimeDir)

addServerHandler({
middleware: true,
handler: resolve(runtimeDir, 'server/middleware/serverHandler')
})
}
})

declare module '@nuxt/schema' {
interface RuntimeConfig
{
basicAuth: ModuleOptions
}
}
32 changes: 32 additions & 0 deletions src/runtime/server/middleware/serverHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { defineEventHandler } from 'h3'
import { useRuntimeConfig } from '#imports'

export default defineEventHandler((event) => {
const {
username,
password,
realm,
whitelistedRoutes
} = useRuntimeConfig().basicAuth

// Allow access on whitelisted routes
if (whitelistedRoutes.includes(event.node.req.url || '')) {
return
}

// Check authentication
let authenticated = false
const base64Credentials = event.node.req.headers?.authorization?.split(' ')?.[1]
if (base64Credentials) {
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii')
const [credentialsUsername, credentialsPassword] = credentials.split(':')
authenticated = credentialsUsername === username && credentialsPassword === password
}

// Reject authentication
if (!authenticated) {
event.node.res.statusCode = 401
event.node.res.setHeader('WWW-Authenticate', `Basic realm="${realm}"`)
event.node.res.end('Unauthorized')
}
})
4 changes: 4 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./playground/.nuxt/tsconfig.json",
"include": ["./src"]
}
Loading

0 comments on commit d563f90

Please sign in to comment.