Mediv0
Mediv0
NNuxt
Created by Mediv0 on 10/18/2024 in #❓・help
Problem getting data from Pinia
Not yet I just moved the shared logic to my main layout
6 replies
NNuxt
Created by Mediv0 on 10/18/2024 in #❓・help
Problem getting data from Pinia
i'll check it out thanks
6 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
I fixed the issue by using middleware combined with a timestamp to track the JWT token's expiry date. In my case, the token was set to expire after 20 minutes. In the middleware, I check if the token has expired, and if so, I handle the refresh, pass the updated cookies, and continue with the request. also have a custom useAuth composable with a useState inside to keep track of cookie expiry on the client side. Additionally, I use a Redis instance on the backend to cache both the JWT and the refresh token for one minute. This ensures that if any subsequent requests fail to get the cookie from the frontend, I can return it directly from the cache. instead of generating new one ( in my case i revoke previous refresh tokens ) simplfied version
import { setHeader } from "h3";
import { useAuth } from "~/composable/useAuth";

export default defineNuxtRouteMiddleware(async () => {
const { isRefreshNeeded, refreshCSR, refreshSSR, setCookieCSR } = useAuth();

if (import.meta.server) {
if (isRefreshNeeded()) {
try {
const { cookies, exp } = await refreshSSR(useRequestHeaders());

cookies.push(`exp=${exp}; Path=/`);
setHeader(useRequestEvent()!, "set-cookie", cookies);

return true;
}
catch {
return navigateTo("/auth/login");
}
}
}

if (import.meta.client) {
if (isRefreshNeeded()) {
try {
await refreshCSR(useRequestHeaders());
setCookieCSR();
return true;
}
catch {
return navigateTo("/auth/login");
}
}
}
});
import { setHeader } from "h3";
import { useAuth } from "~/composable/useAuth";

export default defineNuxtRouteMiddleware(async () => {
const { isRefreshNeeded, refreshCSR, refreshSSR, setCookieCSR } = useAuth();

if (import.meta.server) {
if (isRefreshNeeded()) {
try {
const { cookies, exp } = await refreshSSR(useRequestHeaders());

cookies.push(`exp=${exp}; Path=/`);
setHeader(useRequestEvent()!, "set-cookie", cookies);

return true;
}
catch {
return navigateTo("/auth/login");
}
}
}

if (import.meta.client) {
if (isRefreshNeeded()) {
try {
await refreshCSR(useRequestHeaders());
setCookieCSR();
return true;
}
catch {
return navigateTo("/auth/login");
}
}
}
});
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
you are trying to refresh an access token right ?
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
as far as i know subsequent requests are isolated you cannot alter with their headers i guess there was a issue for this for example
fetch("/refresh") // gets new set cookie
fetch("/a_call_that_needs_cookie") // this doesn't have access to cookies generated by /refresh
fetch("/refresh") // gets new set cookie
fetch("/a_call_that_needs_cookie") // this doesn't have access to cookies generated by /refresh
second call will run with the old cookies https://github.com/nuxt/nuxt/issues/13096#issuecomment-1397311005
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
your request is in event object you should be able to see it if you log
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
oh ye these composables wont work inside the server handlers to get cookies using h3
defineEventHandler(async (event) => {
const name = getCookie(event, "name");

// do something...
}),
defineEventHandler(async (event) => {
const name = getCookie(event, "name");

// do something...
}),
there is another way to actually write cookies back something like
defineEventHandler(async (event) => {
setCookie(event, "name", "value", { maxAge: 60 * 60 * 24 * 7 });
}),
defineEventHandler(async (event) => {
setCookie(event, "name", "value", { maxAge: 60 * 60 * 24 * 7 });
}),
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
try this appraoch inside event handlers
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
can i see the code by server context u mean the server middleware ?
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
there is also setResponseHeaders
export default defineEventHandler((event) => {
setResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
export default defineEventHandler((event) => {
setResponseHeaders(event, {
"content-type": "text/html",
"cache-control": "no-cache",
});
});
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
use useRequestHeaders to get access to request cookies
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
on ssr context and server middleware you could write header back to a request(event) using setHeader import { setHeader } from "h3"; setHeader(useRequestEvent()!, "set-cookie", cookies);
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
did you use useRequestHeaders ?
27 replies
NNuxt
Created by Braňo on 10/17/2024 in #❓・help
Missing cookies in request from server
Show some codes
27 replies
NNuxt
Created by felix on 10/14/2024 in #❓・help
Auth cookie being refreshed during SSR
It's really frustrating because this issue could easily be resolved if they allow to preserve the headers for subsequent requests.
6 replies
NNuxt
Created by felix on 10/14/2024 in #❓・help
Auth cookie being refreshed during SSR
and in my custom useFetch if i ecounter a 401 i just redirect to the login page because at this point if you hit 401 it means something is wrong
6 replies
NNuxt
Created by felix on 10/14/2024 in #❓・help
Auth cookie being refreshed during SSR
simplfied version
import { setHeader } from "h3";
import { useAuth } from "~/composable/useAuth";

export default defineNuxtRouteMiddleware(async () => {
const { isRefreshNeeded, refreshCSR, refreshSSR, setCookieCSR } = useAuth();

if (import.meta.server) {
if (isRefreshNeeded()) {
try {
const { cookies, exp } = await refreshSSR(useRequestHeaders());

cookies.push(`exp=${exp}; Path=/`);
setHeader(useRequestEvent()!, "set-cookie", cookies);

return true;
}
catch {
return navigateTo("/auth/login");
}
}
}

if (import.meta.client) {
if (isRefreshNeeded()) {
try {
await refreshCSR(useRequestHeaders());
setCookieCSR();
return true;
}
catch {
return navigateTo("/auth/login");
}
}
}
});
import { setHeader } from "h3";
import { useAuth } from "~/composable/useAuth";

export default defineNuxtRouteMiddleware(async () => {
const { isRefreshNeeded, refreshCSR, refreshSSR, setCookieCSR } = useAuth();

if (import.meta.server) {
if (isRefreshNeeded()) {
try {
const { cookies, exp } = await refreshSSR(useRequestHeaders());

cookies.push(`exp=${exp}; Path=/`);
setHeader(useRequestEvent()!, "set-cookie", cookies);

return true;
}
catch {
return navigateTo("/auth/login");
}
}
}

if (import.meta.client) {
if (isRefreshNeeded()) {
try {
await refreshCSR(useRequestHeaders());
setCookieCSR();
return true;
}
catch {
return navigateTo("/auth/login");
}
}
}
});
6 replies
NNuxt
Created by felix on 10/14/2024 in #❓・help
Auth cookie being refreshed during SSR
I've been stuck on this issue for five days and tried every possible solution. While my approach might not be the best, it works. I fixed the issue by using middleware combined with a timestamp to track the JWT token's expiry date. In my case, the token was set to expire after 20 minutes. In the middleware, I check if the token has expired, and if so, I handle the refresh, pass the updated cookies, and continue with the request. also have a custom useAuth composable with a useState inside to keep track of cookie expiry on the client side. Additionally, I use a Redis instance on the backend to cache both the JWT and the refresh token for one minute. This ensures that if any subsequent requests fail to get the cookie from the frontend, I can return it directly from the cache. instead of generating new one ( in my case i revoke previous refresh tokens )
6 replies
NNuxt
Created by Mediv0 on 10/10/2024 in #❓・help
What is the best way to use pinia with useAsyncData
nice i'll check it out thank you
5 replies