App router giving me pages router error?
Answered
Schweizer Laufhund posted this in #help-forum
Schweizer LaufhundOP
× You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/I'm using app router though.. text code below
Answered by Ray
you could create a server action with this and import it to your client component
// action.ts
'use server'
export async function getSession() {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return {
user: null,
session: null,
};
}
return lucia.validateSession(sessionId);
}20 Replies
Schweizer LaufhundOP
the offending function is
//login user
export const validateRequest = cache(
async (): Promise<
{ user: User; session: Session } | { user: null; session: null }
> => {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return {
user: null,
session: null,
};
}
const result = await lucia.validateSession(sessionId);
// next.js throws when you attempt to set cookie when rendering page
try {
if (result.session && result.session.fresh) {
const sessionCookie = lucia.createSessionCookie(result.session.id);
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
}
} catch {}
return result;
}
);I believe the import cookies is causing it
│ import { cookies } from "next/headers";@Ray where do you use `validateRequest`?
Schweizer LaufhundOP
yes its supposed to be used in server components only but i made a custom hook calling
validateRequest and imported it to my client . idk if i did that properly though@Schweizer Laufhund yes its supposed to be used in server components only but i made a custom hook calling validateRequest and imported it to my client . idk if i did that properly though
this is not going to work
1.
2.
1.
cookies() cannot be used in client component2.
cookies().set() doesn't work in page componentyou should handle it in middleware
but since you are using prisma, it may not work in middleware
@Ray this is not going to work
1. `cookies()` cannot be used in client component
2. `cookies().set()` doesn't work in page component
Schweizer LaufhundOP
im trying to call
validateRequest to authenticate my users session so i can display the user to the client component, is there any workaround to this@Ray how do you call validateRequest now?
Schweizer LaufhundOP
import { validateRequest } from "../actions/auth";
const getUser = async () => {
const { user } = await validateRequest();
return user;
}
export default getUser; I just import this function to my client componentyou could create a server action with this and import it to your client component
// action.ts
'use server'
export async function getSession() {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return {
user: null,
session: null,
};
}
return lucia.validateSession(sessionId);
}Answer
@Ray you could create a server action with this and import it to your client component
ts
// action.ts
'use server'
export async function getSession() {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return {
user: null,
session: null,
};
}
return lucia.validateSession(sessionId);
}
Schweizer LaufhundOP
oh making getUser a server action seemed to have solved it? im not sure why though
Schweizer LaufhundOP
what changed though? i thought the code was still all being executed on the server before. im still new to nextjs13
so it can be executed on client
Schweizer LaufhundOP
oh gotcha so before the code could only run on
the server