Created
July 25, 2019 18:20
-
-
Save msurguy/ba2c423e52b5b0ac44d4152b7254125f to your computer and use it in GitHub Desktop.
Nuxt.js Basic Auth
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
const { createMiddleware } = require('./basic-auth') | |
export default function (moduleOptions) { | |
const options = Object.assign({}, this.options.basic, moduleOptions || {}) | |
const { name, pass, enabled } = options | |
if (!(name && pass)) { | |
console.log('name or pass not found. Skip registration of authentication server.') | |
return false | |
} | |
if (enabled === false) { | |
console.log('Enabled flag is false. Skip registration of authentication server.') | |
return false | |
} | |
const middleware = createMiddleware(options) | |
this.addServerMiddleware(middleware) | |
console.log('Register basic auth module to server middleware') | |
return true | |
} |
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
const parseBasicAuth = require('basic-auth') | |
const matchCredential = (requirement, input) => | |
Object.entries(requirement).every(([key, value]) => value === input[key]) | |
const auth = ({ name, pass }, req) => { | |
if (!(name && pass && req)) { | |
throw new TypeError('Missing argument "name" or "pass" or request object.') | |
} | |
const credential = parseBasicAuth(req) | |
return !!(credential && matchCredential({ name, pass }, credential)) | |
} | |
const createBasicMessage = (options = {}) => | |
`Basic realm="${options.message || 'Please enter username and password'}"` | |
export function createMiddleware (options) { | |
return (req, res, next) => { | |
try { | |
let enabled = true | |
let match = options.match | |
if (typeof match === 'function') { | |
enabled = match(req) | |
} else if (match instanceof RegExp) { | |
enabled = match.test(req.url) | |
} else if (typeof match === 'string') { | |
enabled = match === req.url | |
} | |
if (!enabled || auth(options, req)) { | |
return next() | |
} | |
} catch (e) { | |
// | |
} | |
res.statusCode = 401 | |
res.setHeader('WWW-Authenticate', createBasicMessage(options)) | |
return res.end() | |
} | |
} |
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
export default { | |
'@/basic-auth-module' | |
} | |
basic: { | |
name: 'username', | |
pass: 'password', | |
enabled: true // require boolean value(nullable) | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment