Next 14
Answered
(-.-) posted this in #help-forum
(-.-)OP
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.
27 Replies
why do you need the auth token client side? bit insecure no?
(-.-)OP
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
Get request to where. from where
from client to external? from client to server? from server to external?
(-.-)OP
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
pages dir or app dir
(-.-)OP
app dir
why are you not fetching in a server component
(-.-)OP
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
that makes it a server action and is another way to do it.
(-.-)OP
In the future I will refactor this page to be server components
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
(-.-)OP
Got it, you gave me an idea, thanks
sweet. mark whichever message helped as the answer so it closes this post
(-.-)OP
Would this solve it for a while?
(-.-)OP
only until I refactor everything
does this make my function server-side?
makes it basically a self contained api route yep. the docs explain server actions in detail
(-.-)OP
even though it is of the GET type? I saw that actions are used for mutations (POST)
the POST function is to call the server action. it can make a GET request inside it
(-.-)OP
cool, thanks
Answer