LearnTeachCode / pair-partners

A web app that helps people find study partners for virtual pair programming and collaboration
http://learnteachcode.org/pair-partners/
38 stars 16 forks source link

For Rick: Add Login with GitHub feature using Firebase Authentication #44

Closed LearningNerd closed 7 years ago

LearningNerd commented 7 years ago

So far we've set up a small but functional Firebase app and practiced using some of the Firebase functions for accessing and displaying data. Next, let's set up a user login system using Firebase Authentication and the GitHub API!

📚 Prerequisites: First complete "Challenge: Practice displaying Firebase data"

✔️ To complete this challenge: follow each step below, check off the checkboxes to track your progress (so we can see your progress too!), and then submit your solution as a pull request.

If you have any questions, post a comment at the bottom of this page or ask us in our Slack chatroom!

1. Set up for local development and testing with a local server

Why we need to use a local server

From now on, when testing our app locally, we'll need to run it from a local server every single time! Why's that? Because when users log in to our app through GitHub, they'll be redirected from GitHub's website back to our app -- and for that, we need to provide GitHub with a URL to our app!

The problem is, as you may have noticed, we've previously been running our app directly from our computer's file system. When you open a local HTML file, the URL starts with file:// -- so we're using the file URI scheme instead of HTTP, and that means the URL to our web app points directly to a file on our computer, not to a website on the internet!

GitHub won't allow us to register our app as a link to a file on our hard drive -- not to mention the security issues involved in trying to do that! Luckily, GitHub is totally OK with us providing a URL like http://localhost:8080 -- so, problem solved! (This is a big topic of course, with much more to learn about here! But for now, we'll focus on just getting our app to work.)

2. Create buttons and event listeners for login and logout

✔️ SOLUTION for Part 2: https://gist.github.com/LearningNerd/26e3996aebf800225145c33e512c0d07

3. Register a new GitHub OAuth application

📑 Official documentation: Firebase Authentication Using GitHub with JavaScript and Registering OAuth Apps on GitHub

OAuth is an open protocol that lets you give certain apps permission to interact with other apps that you use, without needing to enter your password for all of them! We're going to use OAuth to let users log into our web app using their existing GitHub account, so that way our users never need to type their password into our app, and we don't need to store their passwords in our database at all!

When we register our OAuth app with GitHub, we get a unique Client ID and Client Secret. The Client ID is public, sort of like a username but for our application, and the Client Secret is like our password -- never, ever share or publish your Client Secret! (Except when we put it into our Firebase console, which will keep it securely stored on Firebase's server.)

4. Set up GitHub authentication in the Firebase console

📑 Official documentation: Firebase Authentication Using GitHub with JavaScript

Congrats, your app is now all configured to use both the GitHub API and Firebase Authentication! Now all we need to do is write some code to implement the user login feature.

5. Add Firebase Authentication functions to your JavaScript

🏆 The goal: Once the user has successfully logged in with GitHub, display a message on the page showing their GitHub username and profile photo! Then when the user logs out, remove their information and display the message "Not currently logged in."

📑 Official documentation: Firebase Authentication Using GitHub with JavaScript (the section titled "Handle the sign-in flow with the Firebase SDK") and Manage Users in Firebase

The Firebase signInWithPopup method will open another website in a pop-up window, asking the user to grant access to our app. It takes a Firebase Auth provider object as its input, so in this case, we're using the GitHub login provider object that we created earlier. That's how it knows to open GitHub's website! (We can also use other providers like Facebook or Google if we want to.)

If you like, you can switch out the signInWithPopup method with the signInWithRedirect method instead, which redirects the user to a new page in the same browser window instead of opening up a pop-up window. (This works better on mobile devices!)

The Firebase signOut method will sign out the current user, regardless of how they signed in (whether we had them log in with GitHub or Facebook or something else).

The Firebase onAuthStateChanged method is an event listener that is triggered whenever the user's authentication state has changed -- either they've just logged in, or they've just logged out. Its callback function receives a Firebase User object as its input (here we named the parameter user), which contains a bunch of information about the user!

💡 Remember to commit early and commit often! A good rule of thumb is to make a commit with Git after every 15 minutes of coding and at the end of every work session.

✔️ Solutions will be posted later. Try it out by yourself first, and submit a pull request to get feedback on your code! Remember you can post a pull request at any time, not just when your code is all finished!

6. Access Firebase user profile information

First take a look through the official documentation for the Firebase User object to get a sense of what information Firebase stores about a user by default.

7. Display the user's profile information on the web page

For the next step, we're going to use a new DOM element property called innerHTML! It's very similar to the textContent property, but the main difference is that innerHTML will render HTML elements in your string as actual HTML elements on the page!

You should see that message with the literal HTML tags show up as code on your web page! That's because the textContent property does not render HTML.

✔️ Solutions will be posted later. Try it out by yourself first, and submit a pull request to get feedback on your code! Remember you can post a pull request at any time, not just when your code is all finished!

8. Test the app with your local server

Now we're finally ready to see if the app works! Let's try logging in with GitHub!

🏆 Congratulations, you built your first web app with a working user login system! This is the first step towards turning all your wildest ideas into web applications that could potentially have millions of users!

9. Create a pull request to share your code with us

You know the drill! If you need to review the detailed instructions, see the bottom of the previous challenge ("Practice displaying Firebase data"). Here's a less detailed, quick recap:

Congrats, you've completed this challenge!

✔️ Don't forget to check off all the checkboxes in these instructions, so the rest of our team can easily see your progress!

👉 More challenges to come, so stay tuned!

LearningNerd commented 7 years ago

Great job!!! Closing this issue now that you're done 🏁