freeCodeCamp/curriculum/challenges/chinese/06-information-security-and.../advanced-node-and-express/implementation-of-social-au...

3.2 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
589a8eb3f9fc0f352b528e72 Implementation of Social Authentication III 2 社会认证的实施III

Description

提醒一下,这个项目是基于Glitch的以下入门项目构建的,或者是从GitHub克隆的。策略的最后一部分是处理从Github返回的配置文件。我们需要加载用户数据库对象如果存在或创建一个如果不存在并填充配置文件中的字段然后返回用户的对象。 Github在每个配置文件中为我们提供了一个唯一的ID ,我们可以使用它来搜索(已经实现)用户序列化。下面是您可以在项目中使用的示例实现 - 它位于作为新策略的第二个参数的函数内,就在console.log(profile);目前是:
 db.collection 'socialusers'。findAndModify
    {idprofile.id}
    {}
    {$ setOnInsert{
        idprofile.id
        nameprofile.displayName || 'John Doe'
        照片profile.photos [0] .value || ”
        电子邮件profile.emails [0] .value || '没有公开电子邮件'
        created_onnew Date
        providerprofile.provider || “
    } $设置:{
        last_login新日期
    } $ INC {
        login_count1
    }}
    {upserttruenewtrue}
    错误doc=> {
        return cbnulldoc.value;
    }
; 
使用findAndModify它允许您搜索对象并对其进行更新如果对象不存在则将其置换并在每次回调函数中接收新对象。在这个例子中我们总是将last_login设置为now我们总是将login_count增加1并且只有当我们插入一个新对象新用户我们才会填充大部分字段。需要注意的是使用默认值。有时返回的个人资料不会填写所有信息或者用户会选择保留私密信息;所以在这种情况下我们必须处理它以防止错误。你现在应该可以登录你的应用了 - 试试吧!当您认为自己已经做对时,请提交您的页面。如果你正在运行到错误,您可以检查出的这个小项目的完成代码的例子在这里

Instructions

Tests

tests:
  - text: Github策略设置完成
    testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /GitHubStrategy[^]*db.collection/gi, "Strategy should use now use the database to search for the user"); assert.match(data, /GitHubStrategy[^]*socialusers/gi, "Strategy should use "socialusers" as db collection"); assert.match(data, /GitHubStrategy[^]*return cb/gi, "Strategy should return the callback function "cb""); }, xhr => { throw new Error(xhr.statusText); })'

Challenge Seed

Solution

// solution required