WDI-SEA / project-4-issues

Open an issue to receive help on project 4 issues
0 stars 0 forks source link

401 Unauthorized in app.js #24

Closed isaac8069 closed 2 years ago

isaac8069 commented 2 years ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

MERN

What's the problem you're trying to solve?

authorization using token. When I had it working it seemed to be in an infinite loop. I also modified Bearer${user.token} to Bearer${user}. Doesn't seem to like any of the many ways I have attempted authorization.

Post any code you think might be relevant (one fenced block per file)

const [apartments, setApartments] = useState([])
    useEffect(() => {
        getApartments()
      })

     const getApartments = () => {
          fetch(`${apiUrl}/apartments`, {
              method: 'GET',
              headers: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${user}`
              }
          })

              .then(res => res.json())
              .then(foundApartments => {
                  setApartments(foundApartments.apartments)
              },[])
              .catch(err => console.log(err))
      }

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

401 (Unauthorized)

What is your best guess as to the source of the problem?

token isn't being passed in correctly

What things have you already tried to solve the problem?

various code, saving authorization to a variable and passing it into the getApartments route

TaylorDarneille commented 2 years ago

When I had it working it seemed to be in an infinite loop <---- what did that code look like?

TaylorDarneille commented 2 years ago

I think it'll be easier to fix the infinite loop than the complete lack of user

isaac8069 commented 2 years ago

I've modified it many times after the infinite loop in an ad hoc way. Hard to explain, but I believe my code is the same. I can't recall how I got it to pull in. I like saved in VSCode, then I refreshed the page on the client, then I hit undo in VSCode and it showed up on the client but in the console.log it kept going up to like 7000+ renders until I hit save again in VSCode to stop the loop

TaylorDarneille commented 2 years ago

Sure - that's a bummer we don't have that code. Passing the data is one task, fixing an infinite loop is another task. Was hoping we could just do the latter. I'll hop into your room.

isaac8069 commented 2 years ago

Infinite loop and get route resolved!