PricingBlog

http-only cookie Supabase - How to get a specific key

  • matt_apollodev-1418572804873519154

    Matthieu B.

    5 months ago

    TL;DR : How to get the cookies.user.user_metadata.usertype ?

    __


    I'm trying to do something that I thought would be simple (in javascript at least) but I'm struggling with it.

    I'm using the supabase template, and the cookie get saved in the http-only cookie.

    I'm trying to retreive the value for user_type

    And the structure is like so :
    {
    "access_token": "xx",
    "user": {
    "user_metadata": {
    "user_type": "ORGANISATION"
    }
    }
    }


    Attached the screenshot of what I've tried to attempt.
    Any idea how to solve that ?
    1418572805276176496-Screen_Shot_2025-09-19_at_2.09.30_PM.png
  • jaycmpb-1418592222378725526

    Jay Campbell

    5 months ago

    Cookies are stored as strings so arrays and objects lose their shape if you try to save them directly.

    You can stringify the object before saving it as a cookie then when you go to call for it, use JSON parse to turn it back into an object THEN you can do the Get.

    Someone correct this if incorrect.
  • martinf4630-1418629465369612350

    MartinF

    5 months ago

    You can't access http-only cookies in the browser so you won't be able to get this data. You can get those values directly from the jwt before it is stored, but that would involve editing the package. Alternatively you could call an edge function and call getClaims() which would return all claims from the JWT. This is most secure and fast. I do it on every page load instead of getUser()
  • matt_apollodev-1418986220117299321

    Matthieu B.

    5 months ago

    Okay, thanks guys, I'll try
  • Tod-1418986222147207228

    Tod

    5 months ago

    Great job @Matthieu B.! Your contribution to the Nordcraft Community just made you advance to Community Level 6! 🌲