#yes correct! but also my fetch is pretty
1 messages · Page 1 of 1 (latest)
hey hey
Is it a app or page router?
yes you are right, this access token jwt will be passed to everycall to the backend which is nestjs server
app 13.5.6
Cool
What's the dashboard or homepage is here?
and i have created this index.ts to fit more auth calls like update password / register etc no next-auth related. but still auth somehow
in page.tsx right above robots.ts is Home:
export default function Home() {
return (
<main className='flex min-h-screen flex-col items-center justify-between p-24'></main>
);
}
So in your layout
const { data: session } = useSession()
and log the session and check what response you're getting
That's your accesstoken
correct, is set after i login to my nestjs backend
You may can create a middleware and save it and pass for everycall
or
Use the Zustand and store it in a store and use the tanstack query and pass for the everycall
what is your suggestion, someone told me like is not good idea to use it in middleware cuz could slow down because middlewares are executed over every request
Yeah same for me
and so far this it the only middleware that i have
For my application I used the zustand
but zustand and tansack are client stuff?
that mean each component that will execute request need to be marked as client even the ones that executes GET
Nope
The main parent component will be the server and the child can be the cliennt
For an example
If you have the products
the page will be the server
and the component you can make it as the client and do the stuff
liek fetching the data, useeffect and states
but how come that i'm not able to read next-auth in such kind of service
yeah but that moves fetch to the client, and can be done on the server
Yes but here you need to pass the token for every call
and I don't recommend fetching the session token for every component
so you are suggesting uing middleware?
It's better to store it and use it
That's my server componen
and is it possible to read in this updatePasswordApi even if i need to do it for each call feels like better trade of than store it in another place and then use it from there in a client component?
That's my client
it makes sense for container to be client, you have user interaction
Yep
but lets say such is missing and you need to convert it to client just to consume from the store
But your parent is server
but it doesn't really matter cuz parrent have 2 divs? like 95% of the screen is still client?
hmm
Then you need to stick with the server component and need to find a way through the middlewares
looks like very weird, how next do not have scenario for such standard case 😄
i mean if middleware is applied will be executed to each request and need to be very limited, apart from that is hard to chain middlewares as far as i understood
and if i want to read it in a service i'm not really able to do it?
You can follow my strategy bro
hmm will try to find some approach to do it
i feel like should be possible to store the jwt in next auth and then use it in services directly because otherwise become kinda redundant
Okay
if i found something i'll share it with you
so you can clean up a little bit this container and leave just the small pieces as a leafs to the client components
Sure, thanks
hey bro you can use getSession() in the service, that way you can remove everything from the client component and no need to create middleware:
export const updatePasswordApi = async (data: {
oldPassword: string;
newPassword: string;
}) => {
const session = await getSession();
const response = await fetch(baseUrl, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session?.user.accessToken}a`
}
});
if (!response.ok) {
throw new Error(API_ERRORS.UPDATE_PASSWORD);
}
return response.json();
};
yes you need to repeat this on every request, but it's the same if is passed from the component and here is more isolated and applicable