Files
freeCodeCamp/curriculum/challenges/english/06-quality-assurance/advanced-node-and-express/implementation-of-social-authentication-iii.english.md
Shaun Hamilton e3cfe80f88 fix(curriculum): advanced node express changes for new boilerplate (#39080)
* fix: add tests and steps

* add necessary changes

* edit for new boilerplate

* fix: adjust content for boilerplate merge

* add 4 passing 1 failing socketio

* fix: add socketio changes

* fix: update wording and http test

Co-authored-by: Kristofer Koishigawa <scissorsneedfoodtoo@gmail.com>

* fix: replace glitch remix urls with repl.it urls

* integrate steps between lessons 4 and 5

* add mongodb altas link

* edit test to not require db deletion

* correct register routing and formatting

* fix typos and formatting

* fix: typos, standardize spacing, and remove unnecessary hr elements

* fix: add/update links

Add or update Gist solution links at the bottom of each challenge. Also add a missing link/text to the top of one of the challenges.

* fix: remove Repl.it/boilerplate repo links from all but first challenge

* fix: add target='_blank' to links in challenges

* add note about PIP browser issues

* move PIP note to end of instructions

Co-authored-by: Kristofer Koishigawa <scissorsneedfoodtoo@gmail.com>
2020-09-04 08:50:03 -05:00

3.0 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
589a8eb3f9fc0f352b528e72 Implementation of Social Authentication III 2 301558

Description

The final part of the strategy is handling the profile returned from GitHub. We need to load the user's database object if it exists, or create one if it doesn't, and populate the fields from the profile, then return the user's object. GitHub supplies us a unique id within each profile which we can use to search with to serialize the user with (already implemented). Below is an example implementation you can use in your project- it goes within the function that is the second argument for the new strategy, right below the console.log(profile); currently is:

myDataBase.findAndModify(
  {id: profile.id},
  {},
  {$setOnInsert:{
    id: profile.id,
    name: profile.displayName || 'John Doe',
    photo: profile.photos[0].value || '',
    email: Array.isArray(profile.emails) ? profile.emails[0].value : 'No public email',
    created_on: new Date(),
    provider: profile.provider || ''
  },$set:{
    last_login: new Date()
  },$inc:{
    login_count: 1
  }},
  {upsert:true, new: true},
  (err, doc) => {
    return cb(null, doc.value);
  }
);

With a findAndModify, it allows you to search for an object and update it, as well as insert the object if it doesn't exist and receive the new object back each time in our callback function. In this example, we always set the last_login as now, we always increment the login_count by 1, and only when we insert a new object(new user) do we populate the majority of the fields. Something to notice also is the use of default values. Sometimes a profile returned won't have all the information filled out or it will have been chosen by the user to remain private; so in this case we have to handle it to prevent an error.

You should be able to login to your app now- try it!

Submit your page when you think you've got it right. If you're running into errors, you can check out the project completed up to this point here.

Instructions

Tests

tests:
  - text: GitHub strategy setup should be complete.
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/auth.js') .then(data => { assert.match(data, /GitHubStrategy[^]*myDataBase/gi, 'Strategy should use now use the database to search for the user'); assert.match(data, /GitHubStrategy[^]*return cb/gi, 'Strategy should return the callback function "cb"'); }, xhr => { throw new Error(xhr.statusText); })

Challenge Seed

Solution

/**
  Backend challenges don't need solutions, 
  because they would need to be tested against a full working project. 
  Please check our contributing guidelines to learn more.
*/