2.E.4: Instagram Auth
Learning Objectives
Know how to decompose complex components into multiple smaller, more manageable components
Understand where to put authentication in a UX flow to lure users in and give them as much reason to login as possible
Understand how to use Firebase Authentication
Know how to read documentation to apply a new technology
Introduction
We will build on the previous exercise to incorporate authentication and user information on all posts, likes and comments.
Setup
Start with the code we wrote in the previous exercise in our forked and cloned copy of the Rocket Academy Instagram starter repo
Set up Firebase Authentication in our local
firebase.jsxas per the official Firebase documentationUnder "Add and initialize the Authentication SDK", we will need to import
getAuthand export a named export with the Auth object, likeexport const auth = getAuth(firebaseApp);Skip "(Optional) Prototype and test with Firebase Local Emulator Suite" and everything below it for now; that content will be covered in the next step.
Enable Email/Password sign-in in the Firebase console (Step 3 in the linked docs)
Once in the Auth section of our app in the Firebase console, click "Get started" button
Choose the Email/Password sign-in method from the menu
Enable Email/Password
Practice safe sharing, create implement your .env so that you do not share your Firebase credentials online when pushing to GitHub.
Base: Users must login to post, posts have author identity
Refactor app into multiple components each in separate files for maintainability.
Now that our app is starting to become complex (100+ lines of news feed code alone in
App.jsx), we may want to consider refactoringAppinto multiple components for maintainability before adding new functionality such as auth.In our reference solution we separate
App.jsx,Composer.jsx(the form to create new posts) andNewsFeed.jsxfiles for their respective components and put them in a componentsfolder insrcthat contains the component.jsxfiles and their relevant.cssfiles.This will require some re-wiring of relative imports to files such as
src/firebase.jsxand in files such assrc/main.jsx.
Separate composer state and logic into a
Composercomponent inComposer.jsxand news feed state and logic into aNewsFeedcomponent inNewsFeed.jsx. Import bothComposerandNewsFeedinApp.jsxto render them in theAppcomponent as before. Revise React docs for composing components for a refresher.If you customised your UI in previous exercises, feel free to decompose your components however makes sense for your UI.
Update our user flow such that a user needs to log in to post. They should still see the news feed (without composer) before logging in (to lure them in). But when they are not logged in, we display a button to "Create Account or Sign In" instead of the composer.
Consider storing
loggedInUseras state inAppcomponent and using theonAuthStateChangedlistener inApp'scomponentDidMountto keeploggedInUserupdated.Appcan useloggedInUserto determine whether to render the "Create Account or Sign In" button or the composer.Consider also storing
shouldRenderAuthFormboolean state inAppcomponent and creating a class methodtoggleAuthForminAppthat togglesshouldRenderAuthForm. When an unauthenticated user clicks "Create Account or Sign In" button,Appcan calltoggleAuthFormand render the auth form instead of the news feed. Once the user authenticates, auth form logic can calltoggleAuthFormagain forAppto render composer and news feed instead of auth form.Create a new
AuthFormcomponent incomponents/AuthForm.jsxfor the auth form and import it where relevant inApp.You may find the email HTML input type and password HTML input type helpful for email validation and hiding passwords in the password field
All posts should now render the author's identity as part of the post.
Comfortable: Identity in navbar
Display the logged-in user's identity in a navbar at the top of the app. Consider using React Bootstrap's
Navbarcomponent for this.
More Comfortable: Identity for likes and comments
If you haven't already, implement like and/or comment functionality for posts from Comfortable and More Comfortable in the Instagram Posts exercise
Likes should now be associated with a user's identity, and a user can only like a post at most once
Comments should show the author's identity like posts
Submission
Submit a pull request to the main branch of Rocket's Instagram repo and share your PR link in your section Slack channel.
If you would like to deploy your app to the internet, follow Vitejs GitHub Pages deployment instructions here.
Reference Solution
Here is reference code for this exercise. You can do better!
To play with the solution, clone it, run npm i and npm start. We did not host a reference deployment for this solution because we will build on this repo in the following exercises and will host a reference deployment for the final one.
Sample Implementation Firebase Auth Legacy (Slightly different code)
Please checkout the finished code in this repository, ensure that you're on the auth branch. If you want to test out the application on your machine you will need to have registered an Application on Firebase with Realtime Database, Storage and Authentication activated. Use the sample.env within the application to create an .env file with your Firebase credentials. With this in mind if you want to run the application on your machine you will need to install the dependencies with the command npm install after the installation you can then run the application with the command npm run dev. Then open a browser of your choice and navigate to http://localhost:5173.
Last updated