Firebase React Authentication Tutorial For Beginners - Private Route With Hooks

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 พ.ค. 2019
  • 📘New Course: Build full-stack React Typescript applications tsreact.maksimivanov.com/s/ytd
    Follow me on Odysee: odysee.com/$/invite/@stepbyst...
    New video about Firebase Database + React: • How to use Firebase Fi... Sometimes you just need to make a fast prototype and you don’t want to mess with back end, authentication, authorization and all of that. Here is where Google’s firebase can help you. In this tutorial I’ll show you how to connect your react app with Firebase authentication module.
    It is Javascript tutorial for beginners. We are using React with hooks, React Router, React Router Dom and Firebase.
    We’ll use Firebase Authentication module to handle login/sign up and React router to manage routing.
    🐦 Twitter: / ivanov_dev
    I write about React and TypeScript there
    Code for this tutorial: github.com/satansdeer/react-f...
    Subscribe to my emails and get free React testing checklist: eepurl.com/c_8tzP
    Join my Discord server: / discord

ความคิดเห็น • 269

  • @stevenjchang
    @stevenjchang 4 ปีที่แล้ว +42

    Hi Maksim. There is a bug in this code. After watching the video after this, I realized you fixed it before the next video, but you did not mention it anywhere in either videos.
    It's in Auth.js. In between the two videos (because you start that video with a recap), it appears you wrote the pending/loading logic. Without this logic, private route will hit the too early, while the check for currentUser is still pending.
    But thanks for the video, very helpful series!

    • @satansdeer1
      @satansdeer1  4 ปีที่แล้ว +7

      Thanks for the comment, i've pinned it so others can also see it. Later ill update the code in the attached repo.

    • @nazliton5306
      @nazliton5306 3 ปีที่แล้ว +1

      Can you expand further on how to fix this bug ?

    • @alexanderdeimos1687
      @alexanderdeimos1687 3 ปีที่แล้ว +1

      @@johnparsons4434 I found a bug and try to fix it using async/await. Like await onAuthStateChanged(); setLoading(false), but that's not working. When i use example from repo - its done. Can you please explain how it works?

    • @nazliton5306
      @nazliton5306 3 ปีที่แล้ว +1

      @@johnparsons4434 Thanks for that, fixed a bug I was working around for weeks :)

    • @AlanJudson
      @AlanJudson 3 ปีที่แล้ว

      Thanks Maksim! I have been using PrivateRoutes with this, and I've put in the pending/loading logic, but every time I click on a navigation link, it says it's loading, so there's always a little blip that looks like a page refresh (but isn't). I'm wondering if there's a work-around, or a way to only get it to reload for redirects. Anyone have any thoughts?

  • @eaglebirdiepar
    @eaglebirdiepar 5 ปีที่แล้ว +211

    Theon you’re a good man

  • @StevenBoutcher
    @StevenBoutcher ปีที่แล้ว +4

    Holy f***ing f***, man, you're a lifesaver!! I've been bouncing around the internet for months and somehow missed your video, but yours is the first one that worked for me! I just had to refactor a couple of things so they worked for React Router v6 & Google auth (Firebase). If you haven't yet, doing another video with that combo would be amazing. Thanks so much for taking the time to post this, it saved me so much frustration

    • @chrisc.4144
      @chrisc.4144 ปีที่แล้ว +1

      What changes did you make? As soon as I wrap my routes in an AuthProvider, everything disappears when the app runs. Idk how many different tutorials and code examples I’ve used to try and fix this but nothing is working. Ended up going with Auth0 because their AuthProvider works right out of the box

  • @kavinduvindikasomadasa352
    @kavinduvindikasomadasa352 4 ปีที่แล้ว

    without going through all the way upto the end in video...I was able to figure out how to work with firebase auth...thank you for this great video...!

  • @useyourname24
    @useyourname24 4 ปีที่แล้ว

    Great tutorial. I integrated this into a little side app and all seems good to go. Now I'm just curious how to handle multiple protected routes. In any case, this was an excellent guide. Subscribed.

  • @TotallyNotZoid
    @TotallyNotZoid 4 ปีที่แล้ว

    Thank you so much man, quick and straight to the point. Worked like a charm man!

  • @ChrisGWGreen
    @ChrisGWGreen 3 ปีที่แล้ว

    Thanks Maksim, went down the react-firebase-auth-provider route but it felt clunky. Your solution feels a lot more lightweight. Thanks so much.

  • @paullukic1135
    @paullukic1135 4 ปีที่แล้ว +30

    Most useful tutorial I have found on the internet about firebase auth, thanks a lot for sharing.

  • @igorkirey1642
    @igorkirey1642 3 ปีที่แล้ว

    Very nice! Many thanks for this tutorial. I got head-ache trying to understand everything from scratch, but using your code and vide - super great!

  • @wrutzreaction7199
    @wrutzreaction7199 4 ปีที่แล้ว +1

    amazing tutorial ,this is the best interms of authentication using react and firebase...this video helped me a lot

  • @paulm9400
    @paulm9400 5 ปีที่แล้ว +5

    Thank you. Clear explanation with new version of React router 5.0 and react 16.8(withHooks)

  • @NateFreight
    @NateFreight 3 ปีที่แล้ว

    Super easy to follow. Implemented it in my own project. Thanks dude

  • @modeezy2341
    @modeezy2341 2 ปีที่แล้ว

    This is exactly what I needed for my project! Thank you!

  • @kallilfreire972
    @kallilfreire972 3 ปีที่แล้ว

    Finally a good and functional tutorial! Thanks, bro

  • @anvarsaidov8964
    @anvarsaidov8964 2 ปีที่แล้ว

    Great tutorial, liked (fat like) and subscribed, keep it up bro, watching you from Uzbekistan

  • @chinmayphanasgaonkar
    @chinmayphanasgaonkar 3 ปีที่แล้ว

    i saw many tutorials but this was the best

  • @hypergraphjs9739
    @hypergraphjs9739 5 ปีที่แล้ว +4

    This is very useful. Thanks for the video!

  • @chris78945
    @chris78945 4 ปีที่แล้ว +2

    Excellent tutorial! If you ever decide to do a remake, I'll love to see how to manage idToken and timeout in the authentication process.

  • @kwadwoagyapon-ntra2176
    @kwadwoagyapon-ntra2176 4 ปีที่แล้ว

    Awesome tutorial. It undid a lot of the damage a previous tutorial did to my knowledge on this subject.

  • @TheSanchezxd
    @TheSanchezxd 4 ปีที่แล้ว

    thanks dude the solution was pretty simple it is that I was stuck maybe because of the quarentine

  • @adrian_franczak
    @adrian_franczak 4 ปีที่แล้ว +2

    On login page i found this error :/
    "Uncaught Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops."

  • @ilug8571
    @ilug8571 4 ปีที่แล้ว

    I have never learn so much from a video, and AWESOMEEE minecraft stickers 🚀💯

  • @reydez84
    @reydez84 2 ปีที่แล้ว

    damn, this helped me so much bro, better than the 1hr long ones lmaoooo

  • @mdyamin007
    @mdyamin007 3 ปีที่แล้ว

    Simple and easy explanations among all the videos. Thanks a lot ♥

  • @vittoriomorellini1939
    @vittoriomorellini1939 2 ปีที่แล้ว

    Simply explanation that explain perfectly Firebase. This service simplify the user manage

  • @baileyritchie5711
    @baileyritchie5711 4 ปีที่แล้ว +1

    What is the best way to go about private pages for users with different access points? I am trying to create an admin only portal for my site, and the setup in this tutorial simply lets anyone access if they are logged in. I am struggling to get currentUser from context, because it always appears null. Is the better way to do this through setting up the admin access values in Auth (through context) instead of in my login file?

  • @irzoltan
    @irzoltan 4 ปีที่แล้ว +4

    Love it! - Also, don't forget to `yarn build` after you update the `.env` file.

    • @rayywaii_
      @rayywaii_ 3 ปีที่แล้ว +1

      Thanks man, your reply helped me

  • @paradoxalJohn
    @paradoxalJohn ปีที่แล้ว

    I have installed firebase, created a .env and a base file just like you did here. However, it will not recognize 'auth()' as a property of type 'FirebaseApp' (I'm using TypeScript). Can't seem to solve this.

  • @gwapster13
    @gwapster13 4 ปีที่แล้ว +2

    Great tutorial. Thanks.

  • @AlexCastillo-dy6db
    @AlexCastillo-dy6db 4 ปีที่แล้ว

    Thanks for the video. What is happening in your useEffect hook? At 3:30, onAuthStateChanged takes a cb as an argument, and you're passing a reference to setState. Why does this work without calling setState and passing an argument to it?

  • @xurify
    @xurify 4 ปีที่แล้ว

    You're a god, how are you not more popular

  • @elmehdiettalab4756
    @elmehdiettalab4756 4 ปีที่แล้ว

    Straight to the point!! Thanks man

  • @johanochoa396
    @johanochoa396 3 ปีที่แล้ว

    Amazing tutorial. Thank you so much!

  • @TheBaws76
    @TheBaws76 4 ปีที่แล้ว +1

    Very nice tutorial. But i can't seem to grasp why you use the Callback Hook... In what instance, would the handle login function be called without you wanting it to be? It seems to me it will only be called when the login form is submitted which is what we want. So I am not sure why we add this with the history dependency, it seems unecessary. Could someone explain what I am missing here
    ?

  • @MsKodus
    @MsKodus 4 ปีที่แล้ว

    That was really helpfull man! Thanks a lot for sharing this amazing content.

  • @Gabriel-cd5jx
    @Gabriel-cd5jx 4 ปีที่แล้ว

    Thank you so much sir. Your video helped me a lot!

  • @MateusDvk
    @MateusDvk 4 ปีที่แล้ว

    Amazing Tutorial!! Thanks for the vid

  • @hincha013
    @hincha013 4 ปีที่แล้ว

    Thank you bro, amazing tuto !

  • @thedeveloper4207
    @thedeveloper4207 4 ปีที่แล้ว +1

    Amazing Video....Please don't stop making videos because I have pressed the bell button too😄

    • @satansdeer1
      @satansdeer1  4 ปีที่แล้ว

      Wow, thank for support!

  • @cloudeater9571
    @cloudeater9571 3 ปีที่แล้ว

    The dot env is only for development and testing I presume? As the key would be exposed as part of the javascript bundle I’d imagine. Would you recommend just generating a new key once things get to production, or is there some other method you’d recommend? I have never used firebase before

  • @taguma
    @taguma 4 ปีที่แล้ว

    Very nice video, thanks for the tutorial!

  • @mf2810
    @mf2810 4 ปีที่แล้ว

    Very clear, great tutorial - thank you - please keep making more :)

  • @user-dd1pi5ff1l
    @user-dd1pi5ff1l 4 ปีที่แล้ว

    Why we using withRouter if our Login and SignUp pages are already in and have access to history?

  • @asharashfaque
    @asharashfaque 4 ปีที่แล้ว

    This is just what i needed. Thanks.

  • @AlanJudson
    @AlanJudson 3 ปีที่แล้ว

    Thank you so much, Maksim.

  • @metalejo
    @metalejo 4 ปีที่แล้ว

    thank you for this great explanation!

  • @hussainsalih3520
    @hussainsalih3520 2 ปีที่แล้ว

    great tutorial keep moving :)

  • @nilmendes7710
    @nilmendes7710 4 ปีที่แล้ว

    Excellent tutorial.
    Good work!

  • @user-sh2rz4px7d
    @user-sh2rz4px7d 4 ปีที่แล้ว

    So capaciously and understandably! Thanks

    • @satansdeer1
      @satansdeer1  4 ปีที่แล้ว

      Яков Гущин спасибо)

  • @mgominasian9206
    @mgominasian9206 4 ปีที่แล้ว

    why history push and also redirect the history.push is not working in the login component so it is getting rendered again but but the other check it will redirect it with the component why?

  • @alehsiarheyeu8255
    @alehsiarheyeu8255 4 ปีที่แล้ว +1

    Спасибо огромное!👍
    С большой радостью открыл для себя твой канал!🔥
    Продолжай в том же духе!🚀

  • @Melvin420x12
    @Melvin420x12 3 ปีที่แล้ว

    I do not understand why you're using `useCallback` here and what the `withRouter()` is for... I tried the same setup with `useHistory()` to get access to the history object and just replace `useCallback` with an async function. And it acts exactly the same. Could you please explain why you choose to use `useCallback` and `withRouter`? Thanks!

  • @yendrembamrabinchandra476
    @yendrembamrabinchandra476 3 ปีที่แล้ว

    I've installed firebase version 7.19.1. When I use auth feature, there are lot of token requests sent. Please tell me how to fix this.

  • @geovanejunior3154
    @geovanejunior3154 3 ปีที่แล้ว +1

    Theon Greyjoy is a great teacher.

  • @dmitruz1900
    @dmitruz1900 2 ปีที่แล้ว

    I have an error Uncaught TypeError: (0 , _firebase__WEBPACK_IMPORTED_MODULE_1__.useAuth) is not a function

  • @danielgreenfield8970
    @danielgreenfield8970 4 ปีที่แล้ว

    Extremely useful tutorial, thank you! How could I access the currentUser uid in the child components? Is there a way to pass currentUser into the PrivateRoute props? Or maybe a better way to get currentUser.uid?

  • @andreikudriavtsev7402
    @andreikudriavtsev7402 3 ปีที่แล้ว

    Why you use useCallback in onSubmit event? I don't see any React.memo exports in your code

  • @dmitruz1900
    @dmitruz1900 2 ปีที่แล้ว

    export 'Redirect' (imported as 'Redirect') was not found in 'react-router' (possible exports: MemoryRouter, Navigate, Outlet, Route, Router,

  • @user-jp7ss3mn9f
    @user-jp7ss3mn9f 2 ปีที่แล้ว

    I keep getting this error "TypeError: _base_js__WEBPACK_IMPORTED_MODULE_1__.default.auth is not a function"
    1, I found that my firebase config obj looks different from his code, but It's auto generated by firebase so I'm assuming that I have nothing to do with it...
    2, My code is 120% correct with no typo as I cloned from his repo and copied the entire src file
    Does anyone know how to fix this?

  • @bensonluis9846
    @bensonluis9846 3 ปีที่แล้ว

    Hi, my /login and /signup does not show anything. It's just white. When I try to re enter the path, it shows the h1 and inputs briefly then become white screen again. Pls heeeelp thanks

  • @adammo6661
    @adammo6661 3 ปีที่แล้ว

    Great tutorial. However I wonder if there is an easy way to redirect user to his dashboard ('/') when he's logged in without showing login screen for split second?
    I implemented it and when logged user enters localhost:3000/login in address bar it shows him login page for like 0.5s and then redirects to '/'. How can I bypass showing that login page?

  • @davidsnyder518
    @davidsnyder518 4 ปีที่แล้ว

    how do I store the authentication in local storage so the login status persists after logout?

  • @RogerioOrioli
    @RogerioOrioli 4 ปีที่แล้ว

    perfect and using hooks , congrats

  • @ayaanmomin8333
    @ayaanmomin8333 4 ปีที่แล้ว

    How can I connect auth with firestore? In the sense, how can I make or access a document inside a collection called users, and then access the user data?

  • @apayrus
    @apayrus 3 ปีที่แล้ว +1

    Спасибо, Максим, очень интересно объясняешь. И материал урока подготовил хорошо. 👍

  • @jokovBG
    @jokovBG 2 ปีที่แล้ว

    Hahahaha I subscribed because of the loading... moment :D

  •  3 ปีที่แล้ว

    Thanks man! Saved the day

  • @antdx316
    @antdx316 ปีที่แล้ว

    now how do you add the others like Google? also how do you make it where each user has where they left off?

  • @omercak2746
    @omercak2746 4 ปีที่แล้ว

    Awesome tutorial, thank you ^^

  • @alexanderpunkovsky437
    @alexanderpunkovsky437 4 ปีที่แล้ว

    The best tutorial!

  • @wjrobinson11
    @wjrobinson11 3 ปีที่แล้ว

    What if you do have a back end api and want to authenticate requests to it? Would Firebase no longer be a viable solution?

  • @taqikhan5418
    @taqikhan5418 3 ปีที่แล้ว

    where did you get the code of .env file from?

  • @jeremyi
    @jeremyi 4 ปีที่แล้ว +1

    I can't seem to solve this error:
    Error: Invariant failed: You should not use outside a
    The withRouterLogin IS called inside a I tried with your code from Github and got the same result.
    Thank you for your help!

  • @shehzadmalik7926
    @shehzadmalik7926 4 ปีที่แล้ว

    really great lecture... i love it...thank u sir

  • @Elator11777
    @Elator11777 3 ปีที่แล้ว

    I wish I knew about your channel when I started to learn JS, that was about the time you made this video! But at that time, I didn't know. If Had, my knowledge would be better now.

    • @satansdeer1
      @satansdeer1  3 ปีที่แล้ว

      What resources did you use to learn JS as a beginner?

    • @Elator11777
      @Elator11777 3 ปีที่แล้ว

      @@satansdeer1
      FreeCodeCamp
      CodeAcademy
      PluralSight
      Those were my favourite because you get to write code and run it right on their platforms. I also watched a couple of courses, read several articles and did a lot of katas on codewars. Im still learning but with emphasis on react and related libraries.

  • @mariomarasovic6342
    @mariomarasovic6342 3 ปีที่แล้ว

    Error: There is no user record corresponding to this identifier. The user may have been deleted. I can't figure out this error please help

  • @PriyanshuKumar-wu4wk
    @PriyanshuKumar-wu4wk 3 ปีที่แล้ว +1

    I have checked many times my "Auth.js" and "PrivateRoute.js" all the things , are same as video, but still getting
    this error : "Cannot destructure property 'currentUser' of 'Object(...)(...)' as it is undefined."
    I have already applied the previous mentioned changes in Auth.js of adding "Pending state", but still the error won't go away.

    • @Zenyrus
      @Zenyrus 2 ปีที่แล้ว

      Had the same problem, check your AuthProvider did u set that up properly

  • @energyimpulsedel7124
    @energyimpulsedel7124 ปีที่แล้ว +1

    Doesn`t work PrivateRoute 2023 in must be only component

  • @abhishekpatel7624
    @abhishekpatel7624 3 ปีที่แล้ว

    Thanks man... Actually I'm stuck last two days.
    How to set env variables and hide firebase key in react I'm trying all the stackoverflow & documentations steps but it's not finally your video recommended by the TH-cam randomly and just for the learning i click and saw whole video after video i changed into project and wolla its perfectly working 🙂✨

  • @bimalgrg8951
    @bimalgrg8951 3 ปีที่แล้ว

    Thank you.

  • @simonkalu
    @simonkalu 4 ปีที่แล้ว

    Interesting tutorial. Thanks, buddy. But I am stuck with firestore authorization. Do a tutorial on how to load the sidebar menu from the firestore based on user role. This is very important

  • @sndhigh
    @sndhigh 4 ปีที่แล้ว

    Maksim, this is a great tutorial but I am having an issue all of a sudden. i"m getting "TypeError: Cannot read property 'currentUser' of null" from PrivateRoute.jsx:6. I'm banging my head against the wall trying to figure it out. Any suggestions or insights?

    • @satansdeer1
      @satansdeer1  4 ปีที่แล้ว

      Maybe the context is missing, did you wrap your whole app into AuthProvider?

  • @AndreyPiterov
    @AndreyPiterov 4 ปีที่แล้ว

    Thanks a lot for tut

  • @atmospheric_b
    @atmospheric_b 4 ปีที่แล้ว

    that login route flashing on every public route is not good. I already broke my brain to figure out how to do it more elegant. Like showing loading spinner but the problem is that users can stuck on that spinner component forever if they are not been logged in.

  • @TheWizardOfTech
    @TheWizardOfTech 2 ปีที่แล้ว

    where can i find the .env file

  • @user-zp4yg8qi2j
    @user-zp4yg8qi2j 5 ปีที่แล้ว +1

    amazing :)

  • @deepL0
    @deepL0 ปีที่แล้ว

    1:36 Hahaha I laughed a lot for you waiting for no reason with a serious facial expression

  • @LegendarySkyHero
    @LegendarySkyHero 3 ปีที่แล้ว

    What about authentication with Google, Apple, Github?

  • @senangcoding4747
    @senangcoding4747 3 ปีที่แล้ว

    thanks bro

  • @plastikkman
    @plastikkman 3 ปีที่แล้ว

    Hi there, wouldn't it be a security risk storing the firebase key in your code?

    • @plastikkman
      @plastikkman 3 ปีที่แล้ว

      If someone was able to overcome this limitation please let me know how! 😄

  • @Dev_Everything
    @Dev_Everything 2 ปีที่แล้ว

    Can you PLEASE do a react, node, passport local with a mysql or just in memory array?

  • @AlanJudson
    @AlanJudson 3 ปีที่แล้ว

    If I wanted to set another value when someone logs in, for example, currentOrganizationId, where would I set that and can I use the same Context. I've written this code, but I don't know at what point to set the currentOrganizationId. gist.github.com/alanjudson/dfc1321c6a99ae637f0914781ac1c4b5

  • @tattah96
    @tattah96 4 ปีที่แล้ว

    Hi Maksim, how do I dynamically render the user data from firebase clientside? So if a user logs in, how do I render that users profile with his name etc

    • @gubmx20
      @gubmx20 3 ปีที่แล้ว

      Hi @fattah do you can use the UUID from the user and create a document in firestore relating the UUID with this document.

  • @emday4prez
    @emday4prez 3 ปีที่แล้ว +3

    According to the update log of the firebase SDK (version 8.0.0), the way to import firebase SDK should be` import firebase from 'firebase/app' `instead of ` import * as firebase from 'firebase/app'`

  • @tsorgil8475
    @tsorgil8475 4 ปีที่แล้ว

    wow very thanks

  • @VoidDeanmon
    @VoidDeanmon 4 ปีที่แล้ว

    Hi great video! I'm making an app with users and admins do you think you could make a video explaining how to make private routes for the admin and user?

  • @trongnhanle7658
    @trongnhanle7658 2 ปีที่แล้ว

    how to make authorization with react and firebase , bro ?

  • @oojiitech32
    @oojiitech32 4 ปีที่แล้ว

    Hi Maksim! Is there a way to do role specific private routes using custom claims?

    • @satansdeer1
      @satansdeer1  4 ปีที่แล้ว

      sure, youll just need a more complicated context provider that will check against the role instead of the simple boolean check

  • @Sam-xv5ko
    @Sam-xv5ko 4 ปีที่แล้ว

    I loved your tutorial... anyway did anyone told you before that you look like Alfie Allen (Theon Grejoy) from Game of Thrones?? I paused your video and watched your face for like 10 minutes... I was thinking why this face is so familiar.. :-D

  • @MariusMathisenVLOG
    @MariusMathisenVLOG 5 ปีที่แล้ว +1

    When "logged in" the route seems to jump to /login for 1 sec. It's seems to be becouse of PrivateRoute component where it interpets currentUser as null first, and load again to find the current user. The results is that /login flashes for 1 sec, then route to Home-component. Why is that?

    • @radonjamarinkovic
      @radonjamarinkovic 4 ปีที่แล้ว +1

      It happens to me also but in other case. If user is logged in, every time i press refresh button route jumps to login page for 1 sec.

    • @hristoenev6733
      @hristoenev6733 4 ปีที่แล้ว +1

      That is because the render happens before the response is received from Firebase. Sadly this tutorial doesn't handle these cases.

    • @luismiguelascencio4065
      @luismiguelascencio4065 4 ปีที่แล้ว +1

      I have this same issue, could someone help me out? how can I wait to trigger the render after we check the status of current user?

    • @zcto414
      @zcto414 4 ปีที่แล้ว

      Same problem here as well. @Maksim Ivanov, could you please help us here? Do you have any suggestions on how to solve this issue?