Next.js Discord

Discord Forum

Next 14

Answered
(-.-) posted this in #help-forum
Open in Discord
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

why do you need the auth token client side? bit insecure no?
@DirtyCajunRice | AppDir why do you need the auth token client side? bit insecure no?
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
from client to external? from client to server? from server to external?
export const get = async () => { const { data } = await axios.get(url, { headers: { Authorization:Bearer ${myToken}} }); return data; };
my function
I'm using react query
pages dir or app dir
app dir
why are you not fetching in a server component
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
@DirtyCajunRice | AppDir why are you not fetching in a server component
I'm migrating an application, this would just be a test to present, there's still a lot left to refactor
In the future I will refactor this page to be server components
@(-.-) I'm migrating an application, this would just be a test to present, there's still a lot left to refactor
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
Got it, you gave me an idea, thanks
@(-.-) Got it, you gave me an idea, thanks
sweet. mark whichever message helped as the answer so it closes this post
only until I refactor everything
does this make my function server-side?
@(-.-) does this make my function server-side?
makes it basically a self contained api route yep. the docs explain server actions in detail
even though it is of the GET type? I saw that actions are used for mutations (POST)
@(-.-) 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
cool, thanks
Answer