gun - gundb中的私有(private)写入和公共(public)读取

标签 gun gundb

我想创建一个 microblog每个人都可以阅读所有帖子,但只有所有者可以删除或编辑帖子。在 gundb无海人人可以编辑或删除帖子,在sea( gun.user())我必须共享公钥,在海上如何获取所有用户的帖子并在时间轴中显示帖子?

我怎么能用 gundb 创建这个?

最佳答案

我一直在 gun 中寻找有关数据隐私问题的答案,这是我的答案:

  • 变量的导入和定义
  • <script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gun/sea.js"></script>
    
    var gun = Gun()
    
  • 创建微博第一作者
  • gun.user().create('firstMicroblogAuthor', 'somePassword')
    gun.user().auth('firstMicroblogAuthor', 'somePassword')
    
    
  • 创建帖子并获取作者
  • var post = {
      title: 'First post',
      text: 'Hello world!'
    }
    
    var author = gun.get('~@firstMicroblogAuthor') // There should be the same `username` in Step 2
    
  • 保存帖子
  • gun
      .user()
      .get('posts')
      .set(post) // At this step, we saved the post in a user schedule, which by default is only writable by the user
      .once(function() {
        this.get('author').put(author) // In this step, we link our post with the author (with our user)
        gun.get('posts').set(this) // At this step, we save the post with the author installed in the main graph
      })
    
  • 检查我们的帖子是否受到其他用户编辑的保护:
  • gun.user().leave()
    
    gun.user().create('secondMicroblogAuthor', 'somePassword')
    gun.user().auth('secondMicroblogAuthor', 'somePassword')
    
    gun
      .get('posts') // Read posts from public graph
      .once(function() {
        this.get('text').put('Goodbye world!') // In this case, we will get an error, because this post was protected
      })
    

    关于gun - gundb中的私有(private)写入和公共(public)读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044288/

    相关文章:

    firebase - GunDB 存储和 super 对等选项

    reactjs - 将 Gun 与 Redux 一起用于 React 的规范方法/示例

    node.js - 调用gundb.once回调并显示错误消息: Error: No ACK received yet

    javascript - gun.not() 方法不适用于嵌套节点

    javascript - 无法从根级别删除节点

    javascript - 如何使用gundb存储图像/视频文件?

    javascript - Gun 如何在有时断开的 P2P 网络中管理用户

    node.js - 如何将子目录路径传递到 GunDB S3 存储驱动程序,以便可以与非 Gun 数据共享相同的 S3 存储桶?

    javascript - 嵌套对象未更新