markdown Feathersjs挂钩根据用户的电子邮件地址获取Gravatar URL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Feathersjs挂钩根据用户的电子邮件地址获取Gravatar URL相关的知识,希望对你有一定的参考价值。

These hooks executed in order, by assumption users and profiles are separated.

(after)Create User-->(before)Create Profiles

After Create User Hook `create-profile.js`

```nodejs
module.exports = function (options = {}) {
  return async context => {
    async function patchProfile (userId, data) {
      return await context.service.patch(userId, data);
    }
    const userId = context.result._id,
      email = context.result.email,
      profiles = context.app.service('profiles'),
      data = { userId: userId },
      params = { email: email };
      
    await profiles.create(data,params).then((result) => {
          const data = { profileId: result._id };
          patchProfile(userId, data);
      });
      
    return context;
  };
};
```

Before creating profile hook `gravatar-url.js`
This hook require `crypto` for hashing `npm i --save crypto`

```javascript
const crypto = require('crypto'),
  gravatarUrl = 'https://s.gravatar.com/avatar',
  query = 's=64';

// eslint-disable-next-line no-unused-vars
module.exports = function (options = {}) {
  return async context => {
    const { email } = context.params, 
      hash = crypto.createHash('md5').update(email).digest('hex');
    context.data.avatarUrl = `${gravatarUrl}/${hash}?${query}`;
    
    return context;
  };
};
```

以上是关于markdown Feathersjs挂钩根据用户的电子邮件地址获取Gravatar URL的主要内容,如果未能解决你的问题,请参考以下文章

text 持久性用户认证di vue dengan vuex pada后端feathersjs

在 feathersjs 中使用 JWT 进行身份验证和会话处理

Feathersjs - 如何创建自定义身份验证

markdown Git挂钩提交中的自动引用问题

feathersjs 授权自定义问题

FeathersJS 为集合创建复合索引