Potential Solution for 'Set up the Environment'

The whole solution is not needed of course, but I really think adding to the allowed origins array should be specified somewhere since the tests fail otherwise.

What is your hint or solution suggestion?

Project Setup

IMPORTANT - Allow freeCodeCamp to access the test suite:

In fcctesting.js, add ‘https://www.freecodecamp.org’ to the allowed origins array:

var allowedOrigins = ['https://pricey-hugger.gomix.me', 'http://pricey-hugger.gomix.me', 'https://freecodecamp.com', 'https://beta.freecodecamp.com', 'http://freecodecamp.com', 'http://beta.freecodecamp.com','http://localhost:3000', 'https://localhost:3000', 'https://www.freecodecamp.org']

Update MongoDB in Tools > Terminal to the latest version:

npm install mongodb@latest
refresh

Firstly, create environment variables for a Session Secret string and your database password.

Create a variable for your database URI, concatenating your password from the environment variables, and filling in your database name, and use this in your connection method. Change db to client, since a client instance is now returned, and call the db() method on the client with the name of your database to get the database instance, and assign this to db:

let uri =
  "mongodb+srv://user1:" +
  process.env.PW +
  "@freecodecamp.xxxxx.mongodb.net/xxxxxxxx?retryWrites=true&w=majority";
mongo.connect(uri, (err, client) => {
    if(err) console.log('Database error: ' + err);
    let db = client.db('advancednode')

Your app should now load.

Get some Github Oauth Credentials and store them in your environment variables. Look at this previous challenge for a reminder on how to do this:

Implementation of Social Authentication

Edit the ‘callbackUrl’ in the Github Strategy with your callback URL:

    passport.use(new GitHubStrategy({
        clientID: process.env.GITHUB_CLIENT_ID,
        clientSecret: process.env.GITHUB_CLIENT_SECRET,
        callbackURL: "<YOUR APP URL>/auth/github/callback"
      },

In the callback function of the callback route, re-assign the email field to profile._json.email.

{$setOnInsert:{
  id: profile.id,
  name: profile.displayName || 'Anonymous',
  photo: profile.photos[0].value || '',
  email: profile._json.email || 'No public email',
  created_on: new Date(),
  provider: profile.provider || '',
  chat_messages: 0

Login to the app through GitHub and ensure that there are no errors. If successful, you should have been redirected to ‘/chat’. Also, check that a collection has been created in your database called ‘chatusers’ and there is a document for your GitHub profile.

Challenge Solution

Open the Glitch Terminal in Tools > Terminal, and install socket with the package name ‘socket.io

npm install socket.io
refresh

socket.io

Require socket in server.js as ‘io’ and give it the argument ‘http’ to allow it to be used for express:

let io = require('socket.io')(http)

Call the on() method on io, and set the event name as connection. The second argument is a function that takes in data, in this case, a socket. Just log this for now.

//start socket.io code  
io.on('connection', (socket) => {
  console.log('A user has connected');
})

Finally, in client.js call the io() method, assigning this to the variable socket. The io() method comes from the socket client script tag declared in chat.pug and the comment above just tells Glitch to ignore this.

/* global io*/
let socket = io();

Challenge: Set up the Environment

Link to the challenge:

We can discuss adding this, but hopefully soon, this whole boilerplate will be made redundant:

One thing I would change is Glitch does not recommend using the terminal to install packages. They have a dedicated tool for installing packages.

Also, why do you suggest including the GitHub strategy connection for this new section?

Oh, my bad I didn’t see that a Pull Request was made already!

In terms of the Terminal part, maybe something like:

Add the socket.io package to your project’s dependencies:

"socket.io": "^1.7.4"

The connection strategy part is not necessary, you’re right, I just thought it would be good to get it to a stage where all the passport and database problems stop and it’s possible to actually sign to the app to start implementing the socket features. The challenges will pass without it of course, but the app wouldn’t be functional.