sheleoni / icecream-kana-game

A game for learning Japanese characters in the form of collecting ice-cream scoops. 🍦
https://icecream.sheleoni.com/
1 stars 0 forks source link

Store each user's ice-cream collection in protected routes #3

Closed sheleoni closed 1 year ago

sheleoni commented 1 year ago

for now, give each user a unique ice-cream flavor (e.g. a string, like 'chocolate', 'strawberry', etc) users can view their own collection (not others') when they are signed in at http://localhost:3000/user/collection

stuff to consider

viewing each users' owned ice-cream

QA

sheleoni commented 1 year ago

problem: const session = await getServerSession(); returns null when called in api/user/userCollection. solved by: adding headers: headers() as a parameter when fetching api/user/userCollection.

 const response = await fetch('http://localhost:3000/api/user/userCollection', {
            method: 'GET',
            headers: headers() // add this
        });

reason for why this solves the issue: ref: https://github.com/nextauthjs/next-auth/issues/7423#issuecomment-1531064680 My understanding is:

  1. You must have cookies to check if a user is authenticated.
  2. Fetching from a server-side component does not pass cookie to the api/user/userCollection.
  3. So, you must pass cookie when are fetching from a server-side component.

Solved by 9fed7a5

sheleoni commented 1 year ago

Problem: fetch failed in production mode Try: https://github.com/vercel/next.js/issues/44062#issuecomment-1445183293

None of the above has worked. Found this on stack overflow: https://stackoverflow.com/questions/76905386/typeerror-fetch-failed-during-next-js-project-build Option 1: fetch data directly from component without using /api route handler (?) Option 2: Ignore this fetch error on local build. It may work on deployment. Let's try.

↑ Didn't work.

Now reading: https://medium.com/@kaloyan_17221/fix-vercel-next-js-fetch-failed-from-undici-polyfill-8c66346c9c2f

search keyword:

sheleoni commented 1 year ago

OMG FINALLY SOLVED!!! PROBLEM WITH HEADER

        const response = await fetch('http://localhost:3000/api/user/userCollection', {
            method: 'GET',
            // headers: headers() ◀️ delete this
        });

Steps to debug: 1) log the headers()

image

2) read the error

error mesage header
sheleoni commented 1 year ago

OMG FINALLY SOLVED!!! PROBLEM WITH HEADER

But here comes a new issue occurring at http://localhost:3000/user/collection

After I delete headers(), I won't be able to retrieve user ice-cream flavors anymore:

image

Only with headers() am I able to retrieve use ice-cream flavour data. However, it breaks in production.

image

Breaking in production:

image
sheleoni commented 1 year ago

Update: Maybe api routes aren't available at build time!

Ref: https://stackoverflow.com/questions/76591751/next-js-13-app-router-build-error-when-i-try-fetch-api-route-in-server-compone

sheleoni commented 1 year ago

Ref: https://stackoverflow.com/a/76569284 maybe fetching from own app's api endpoint won't work. maybe really try fetching the data within the component itself without using route handlers?