Skip to content

Commit f075802

Browse files
committed
Allow for skipping auth
1 parent 91758bd commit f075802

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const scrud = {
2222
'PUT/': 'update',
2323
'DELETE/': 'delete'
2424
}
25-
const hasBody = ['create', 'update']
25+
const hasBody = {create: true, update: true}
2626
const wlSign = [
2727
'algorithm',
2828
'expiresIn',
@@ -111,6 +111,11 @@ function register (name, opts = {}) {
111111
if (!name) return Promise.reject(new Error(`no name specified in register`))
112112
return new Promise((resolve, reject) => {
113113
let r = resources[name] = Object.assign(opts, {name})
114+
if (Array.isArray(r.skipAuth)) {
115+
let skippers = {}
116+
r.skipAuth.forEach((a) => { skippers[a] = true })
117+
r.skipAuth = skippers
118+
}
114119
return resolve(r)
115120
})
116121
}
@@ -150,13 +155,17 @@ function handleRequest (req, res) {
150155
req.once('error', (err) => sendErr(res, err))
151156
let handler = (resource[action] || handlers[action])
152157
let jwt = (headers.authorization || '').replace(/^Bearer\s/, '')
153-
authenticate(jwt).then((authData) => {
154-
req.auth = req.params.auth = authData
155-
if (hasBody.indexOf(action) === -1) return handler(req, res, resource.name)
158+
let callHandler = () => {
159+
if (!hasBody[action]) return handler(req, res, resource.name)
156160
bodyParse(req).then((body) => {
157161
req.params = Object.assign(body, req.params)
158162
return handler(req, res, resource.name)
159163
})
164+
}
165+
if (resource.skipAuth && resource.skipAuth[action]) return callHandler()
166+
authenticate(jwt).then((authData) => {
167+
req.auth = req.params.auth = authData
168+
return callHandler()
160169
}).catch((err) => fourOhOne(res, err))
161170
}
162171

0 commit comments

Comments
 (0)