W
Wasp-lang3mo ago
Jake

useAuth() usage

I made significant progress on my app using wasp over the weekend. I thought I will ask this before implementing something naive. Here is what i trying to resolve. There are 2 pages. An order page and an upload page. Order page requires authentication, Upload page does not. I have a Navbar component shared by both pages because Navbar is in the Layout page. Navbar uses useAuth() to get user data and controls the rendering of the user dropdown (picture, edit profile, logout) Now when I access the Upload page, I see a browser console error - 401 Unauthorized even though I haven't set authRequired to true. Is this happening because I am using useAuth() incorrectly. Can you please help?
3 Replies
miho
miho3mo ago
The error shouldn't worry you that's just the API saying "hey the user is not logged in" and then this results that the user object is not set in the useAuth hook. Browsers log 401 errors by default so this sholdn't worry you. Or am I missing something, did cause you any issues other than the error message?
Jake
Jake3mo ago
Thanks miho. I am good if this is acceptable. My naive solution was to build 2 Navbars. One for public pages and the other for authenticated ones. Guess I will put it back to one and not worry about the 401.
martinsos
martinsos3mo ago
Yup, that is fine! Server is telling you that user is not authenticated. That said, @miho it may be nicer if we had a method which answer question of "is user logged in" but wouldn't respond with error if not? How weird / standard is that?