如何使用自定义本地策略featherjs身份验证

时间:2019-06-17 19:21:15

标签: reactjs azure-active-directory feathersjs

我正在尝试使用azure广告中的clientIdentifier从React应用进行身份验证(因为我只需要验证用户的身份)。我使用cli生成了身份验证服务。

本地策略如下

"local": {
      "entity": "aers",
      "usernameField": "email",
      "passwordField": "accountIdentifier"
    }

实体看起来像这样

const aers = new Schema({
    firstName: {type: String, required:true},
    lastName: {type: String, required:true},
    accountIdentifier: {type:String, required:true},
    email: {type:String, required:true}
  }, {
    timestamps: true
  });

客户请求看起来像这样

let email = data.account.userName
let accountIdentifier = data.account.accountIdentifier
api.authenticate({
   "strategy":'local',
   email,
   accountIdentifier
}).then(token => {
   console.log(token);
})

我认为我所做的一切都正确(或者至少希望如此),但是身份验证失败并缺少凭据。我对照数据库进行检查,并且尝试登录的用户存在并且数据匹配。但是,当我尝试登录时,会返回错误401无效登录。

有人可以帮助我吗?

编辑:我了解到,在创建服务挂钩之前,需要使用称为hashPassword()的挂钩对“密码”(在我的情况下为clientIdentifier)进行加密。我已经做到了,但是现在保存到数据库的是一条错误消息。 这是错误: 函数(上下文){\ n if(context.type!=='before'){\ n return Promise.reject(new Error({The 'hashPassword' hook should only be used as a 'before' hook.)); \ n} \ n \ n const app = context。 app; \ n const authOptions = app.get('authentication')|| {}; \ n \ n options = merge({passwordField:'password'},authOptions.local,options); \ n \ n debug('Running hashPassword hook with options:',options); \ n \ n const字段= options.passwordField; \ n const hashPw = options.hash || hasher; \ n \ n if(typeof field!=='string'){\ n返回Promise.reject(new Error({You must provide a 'passwordField' in your authentication configuration or pass one explicitly)); \ n} \ n \ n if(typeof hashPw!== 'function'){\ n return Promise.reject(new Error({'hash' must be a function that takes a password and returns Promise that resolves with a hashed password.)); \ n} \ n \ n if(context.data === undefined){\ n debug(hook.data is undefined. Skipping hashPassword hook. ); \ n返回Promise.resolve(context); \ n} \ n \ n const dataIsArray = Array.isArray(context.data); \ n const data = dataIsArray吗? context.data:[context.data]; \ n \ n返回Promise.all(data.map(item => {\ n const password = get(item,field); \ n if(password){\ n return hashPw (password).then(hashedPassword => \ n set(cloneDeep(item),field,hashedPassword)\ n); \ n} \ n \ n返回项目; \ n}))。then(结果=> {\ n context.data = dataIsArray?结果:results [0]; \ n \ n返回上下文; \ n}); \ n}“

0 个答案:

没有答案