Files
freeCodeCamp/curriculum/challenges/english/06-quality-assurance/advanced-node-and-express/implementation-of-social-authentication-ii.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.9 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
589a69f5f9fc0f352b528e71 Implementation of Social Authentication II 2 301557

Description

The last part of setting up your GitHub authentication is to create the strategy itself. For this, you will need to add the dependency of 'passport-github' to your project and require it in your auth.js as GithubStrategy like const GitHubStrategy = require('passport-github').Strategy;. Do not forget to require and configure dotenv to use your environment variables.

To set up the GitHub strategy, you have to tell passport to use an instantiated GitHubStrategy, which accepts 2 arguments: An object (containing clientID, clientSecret, and callbackURL) and a function to be called when a user is successfully authenticated which we will determine if the user is new and what fields to save initially in the user's database object. This is common across many strategies but some may require more information as outlined in that specific strategy's github README; for example, Google requires a scope as well which determines what kind of information your request is asking returned and asks the user to approve such access. The current strategy we are implementing has its usage outlined here, but we're going through it all right here on freeCodeCamp!

Here's how your new strategy should look at this point:

passport.use(new GitHubStrategy({
  clientID: process.env.GITHUB_CLIENT_ID,
  clientSecret: process.env.GITHUB_CLIENT_SECRET,
  callbackURL: /*INSERT CALLBACK URL ENTERED INTO GITHUB HERE*/
},
  function(accessToken, refreshToken, profile, cb) {
    console.log(profile);
    //Database logic here with callback containing our user object
  }
));

Your authentication won't be successful yet, and actually throw an error, without the database logic and callback, but it should log to your console your GitHub profile if you 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: passport-github dependency should be added.
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/package.json') .then(data => { var packJson = JSON.parse(data); assert.property(packJson.dependencies, 'passport-github', 'Your project should list "passport-github" as a dependency'); }, xhr => { throw new Error(xhr.statusText); })
  - text: passport-github should be required.
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/auth.js') .then(data => { assert.match(data, /require.*("|')passport-github("|')/gi, 'You should have required passport-github'); }, xhr => { throw new Error(xhr.statusText); })
  - text: GitHub strategy should be setup correctly thus far.
    testString: getUserInput => $.get(getUserInput('url')+ '/_api/auth.js') .then(data => { assert.match(data, /passport\.use.*new GitHubStrategy/gi, 'Passport should use a new GitHubStrategy'); assert.match(data, /callbackURL:\s*("|').*("|')/gi, 'You should have a callbackURL'); assert.match(data, /process.env.GITHUB_CLIENT_SECRET/g, 'You should use process.env.GITHUB_CLIENT_SECRET'); assert.match(data, /process.env.GITHUB_CLIENT_ID/g, 'You should use process.env.GITHUB_CLIENT_ID'); }, 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.
*/