Next.js Discord

Discord Forum

Next 14

Answered
(-.-) posted this in #help-forum
Open in Discord
Avatar
Hello everyone, I'm using Next 14 and I have a question, how do I send the authentication token in all my requests? When logging in I put the token in the cookies, but I can only access it via the server.
Answered by (-.-)
cool, thanks
View full answer

27 Replies

Avatar
why do you need the auth token client side? bit insecure no?
Avatar
I need to make a GET request, and I need to send a token, I have a function in the service folder but I don't know how to access the token to send in the header
Avatar
Get request to where. from where
from client to external? from client to server? from server to external?
Avatar
export const get = async () => { const { data } = await axios.get(url, { headers: { Authorization:Bearer ${myToken}} }); return data; };
my function
client to server
I'm using react query
Avatar
pages dir or app dir
Avatar
app dir
Avatar
why are you not fetching in a server component
Avatar
When I add "use server" at the top of my function, I have access to cookies using next/header, but I don't know if it's correct
I'm migrating an application, this would just be a test to present, there's still a lot left to refactor
Avatar
that makes it a server action and is another way to do it.
Avatar
In the future I will refactor this page to be server components
Avatar
the right way though is just wrapping it with a server component and passing the data as a prop to client if you need client
unless you specifically need react query for something like infinite scroll etc
Avatar
Got it, you gave me an idea, thanks
Avatar
sweet. mark whichever message helped as the answer so it closes this post
Avatar
Would this solve it for a while?
Avatar
yep
Avatar
only until I refactor everything
does this make my function server-side?
Avatar
makes it basically a self contained api route yep. the docs explain server actions in detail
Avatar
even though it is of the GET type? I saw that actions are used for mutations (POST)
Avatar
the POST function is to call the server action. it can make a GET request inside it
Avatar
cool, thanks
Answer