Next.js Discord

Discord Forum

Access to cookies of client side using server side component

Unanswered
Highlander posted this in #help-forum
Open in Discord
HighlanderOP
I have a server side component need to access to cookies of client side how can i do that ?

11 Replies

import { cookies } from 'next/headers'
@Highlander I have a server side component need to access to cookies of client side how can i do that ?
you can do that, by using the cookies function ([read more](https://nextjs.org/docs/app/api-reference/functions/cookies)):
import { cookies } from 'next/headers'
 
export default async function Page() {
  const cookieStore = await cookies()
  const theme = cookieStore.get('theme')?.value
  return '...'
}
@Highlander But if i used it i can’t access to client the component is in server side
yes, that's only available on the server
HighlanderOP
The is send request from the server with token to check the authorization
@Highlander The is send request from the server with token to check the authorization
what do you want to do? Building an auth system? Yea, then write functions that check your auth
HighlanderOP
I use jwt the component on server side i can’t send request from server side with token of client side to other backend i’m not use next as backend
@Highlander I use jwt the component on server side i can’t send request from server side with token of client side to other backend i’m not use next as backend
yea, then you can check the auth directly on your server like:
const cookieStore = await cookies()
  const someToken = cookieStore.get('token')?.value
  const authRequest = await fetch("https://yourexternal.backend/api/...", {
// add your token as cookies
// ...
});

  const data = await authRequest.json();
  if(!data.isAuthorized)
    return '...'

  return '...'
@Highlander solved?
HighlanderOP
Nope
I’ve changed the idea