Sails.js Waterlock /auth/register causes error 500

一笑奈何 提交于 2019-12-07 11:29:00

问题


In trying to make Waterlock not to create new user on login.

When I set createOnNotFound to false and try to use http://localhost:1337/auth/register?email=a@s.d&password=12345678 to register a new user. I've got 500 error:

error: Sending 500 ("Server Error") response: TypeError: undefined is not a function
at Object.module.exports (D:\temp\sails-waterlock\node_modules\waterlock\lib\controllers\actions\register.js:25:44)
at bound (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\lodash\dist\lodash.js:729:21)
at routeTargetFnWrapper (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:179:5)
at callbacks (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:164:37)
at param (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:138:11)
at param (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:135:11)
at pass (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:145:5)
at nextRoute (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:100:7)
at callbacks (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:167:11)
at C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:187:7
at alwaysAllow (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\lib\hooks\policies\index.js:207:11)
at routeTargetFnWrapper (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\lib\router\bind.js:179:5)
at callbacks (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:164:37)
at param (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:138:11)
at param (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:135:11)
at pass (C:\Users\sandres\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\router\index.js:145:5) [TypeError: undefined is not a function]

The register module, where the error happens is a part of waterlock library.

Here goes the code of the register module as it is now:

'use strict';

/**
 * login action
 *
 * tries to find if we have an auth method to handle this type of login
 * request.
 *
 * GET /auth/login
 */
module.exports = function(req, res){
   var params = waterlock._utils.allParams(req);

  // If there is only 1 chosen auth method just assume it
  if(waterlock._utils.countTopLevel(waterlock.methods) === 1){
    params.type = waterlock._utils.accessObjectLikeArray(0, waterlock.methods).authType;
  }

  if(typeof params.type === 'undefined'){
    return res.badRequest('You must specify a type parameter.');
  }

  if(waterlock.methods.hasOwnProperty(params.type)){
    // call the login function of the correct auth type
    waterlock.methods[params.type].actions.register(req, res);
  }else{
    return res.badRequest('unknown/invalid authentication type');
  }
};

Sails v 0.11, Waterlock v 0.1.0

How can I register user now?

UPDATE:

This happens due to register action is not yet implemented in waterlock-local-auth module I use for authentication. See this PR for details.

So the updated question is: how to work this around until the implementation will be done?


回答1:


I've solved this with custom registration action in /controllers/AuthController.js:

module.exports = require('waterlock').waterlocked({

    register: function(req, res) {
        var params = req.params.all(),
                        def = waterlock.Auth.definition,
                        criteria = { },
                        scopeKey = def.email !== undefined ? 'email' : 'username';

        var attr = {
            password: params.password
        }
        attr[scopeKey] = params[scopeKey];
        criteria[scopeKey] = attr[scopeKey];

        waterlock.engine.findAuth(criteria, function(err, user) {
            if (user)
                return res.badRequest("User already exists");
            else
                waterlock.engine.findOrCreateAuth(criteria, attr, function(err, user) {
                    if (err)
                        return res.badRequest(err);
                    delete user.password;
                    return res.ok(user);
                });
        });
    }

});

This is exactely what I need.

Meanwhile Wayne-o, one of Waterlock contributors, said that he will finish his implementation of registration in waterlock-local-auth.



来源:https://stackoverflow.com/questions/29944905/sails-js-waterlock-auth-register-causes-error-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!