1

サーバーに投稿すると、認証機能に与える情報に関係なく、trueが返されます。私の予感は、同期的に、つまり本質的に非同期で何かをしようとしているということですが、それを修正する方法がわかりません。

auth = (username, api_key, device) ->
  hashed_key = hash.sha256(username + api_key + device, salt) 
  winston.debug('Checking auth for ' + username)
  redis_client.get hashed_key, (err, data) ->
    if data == username
        true

# Main Handler for posting data for a device.
server.post "/:customer/:site/:device", create = (req, res, next) ->
    message = JSON.parse(req.body)
    winston.info(server.name + ': Recieved event from ' + req.params.device)
    authenticated = auth(message.username, message.api_key, message.device)
    winston.debug('****' + authenticated)
    if authenticated == true 
        winston.debug('Auth passed, got a valid user/device/api combination: ' + message.username)
        redis_client.publish('device_events', req.body)
        return next()
    else
        winston.debug('Auth failed, cant find device ' + message.device + ' for ' + message.username)
        return next(restify.NotAuthorizedError)
4

1 に答える 1

1

何かが非同期であることを知っている (または予感している) 場合は、後で何をすべきかをコールバック関数として渡す必要があります。サーバーのポスト機能がどのように機能しているかはわかりませんが、Node HTTP のリクエストのような場合は、次のようにする必要があります。

get = (location, callback, retriever, filterer, formatter)->
  decoratedCallback = (data)->
    callback formatter.applyFormat filterer.applyFilter data
  retriever.retrieve location, decoratedCallback

module.exports = get
于 2012-04-30T07:14:44.483 に答える