Next.js server Action cookes error
Answered
HEDI posted this in #help-forum
HEDIOP
Hi, please help me.
My codes
session.service.ts
My codes
session.service.ts
'use server';
import { jwtVerify, SignJWT } from 'jose';
import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import CryptoJS from 'crypto-js';
export const getSession = async (): Promise<any> => {
if ((await cookies()).has('session')) {
const session = (await cookies()).get('session')?.value;
if (!session) return null;
return await decryptSession(session);
}
return null;
};
export const setSession = async (data: any) => {
(await cookies()).set('session', await encryptSession(data), {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
maxAge: 60 * 60 * 24 * 7,
});
return null;
};
export const deleteSession = async () => {
return (await cookies()).delete('session');
};
'use server';
import { redirect } from 'next/navigation';
import { GetRequest, isError } from '../service/api.service';
import { sleep } from '../service/function.service';
import { deleteSession, getSession } from '../service/session.service';
export const getGuilds = async () => {
const session = await getSession();
try {
const { data: res } = await GetRequest('/users/@me/guilds', session.access_token);
return { data: res };
} catch (error) {
const err = await isError(error);
console.log(err);
if (err === 'You are being rate limited.') {
await sleep(1000);
getGuilds();
}
if (err === 'jwt expired') {
redirect('/dashboard');
}
return { error: err };
}
};
export const verifyAccess = async (id: string) => {
const session = await getSession();
try {
const { data: res } = await GetRequest(`/guilds/${id}/verify`, session.access_token);
return true;
} catch (error) {
await deleteSession();
return false;
}
};
Answered by HEDI
WHYYY is working????????????????
'use server';
import { verifyAccess } from '@/lib/actions/VerifyGuildAction';
export default async function appLayout({
children,
params,
}: Readonly<{
children: React.ReactNode;
params: any;
}>) {
const test = async () => {
const id = (await params).id;
await verifyAccess(id);
};
test();
return children;
}
5 Replies
HEDIOP
/dashboard/[id]/layout.tsx
This error:
"I think the problem is clear: I’m using a server action, as it works on the logout button in another action, and it works perfectly there. But here, for some reason, it throws an error, and I just can't get it to work. Can someone please help me because it's really frustrating that I’m doing everything as I should, yet it throws an error when it shouldn't. I'm using Next.js 15 with App Router.
'use server';
import { verifyAccess } from '@/lib/actions/GuildsAction';
export default async function appLayout({
children,
params,
}: Readonly<{
children: React.ReactNode;
params: any;
}>) {
const access = await verifyAccess((await params).id);
return children;
}
This error:
[ Server ] Error: Cookies can only be modified in a Server Action or Route Handler. Read more: https://nextjs.org/docs/app/api-reference/functions/cookies#options
"I think the problem is clear: I’m using a server action, as it works on the logout button in another action, and it works perfectly there. But here, for some reason, it throws an error, and I just can't get it to work. Can someone please help me because it's really frustrating that I’m doing everything as I should, yet it throws an error when it shouldn't. I'm using Next.js 15 with App Router.
HEDIOP
WHYYY is working????????????????
'use server';
import { verifyAccess } from '@/lib/actions/VerifyGuildAction';
export default async function appLayout({
children,
params,
}: Readonly<{
children: React.ReactNode;
params: any;
}>) {
const test = async () => {
const id = (await params).id;
await verifyAccess(id);
};
test();
return children;
}
Answer
Northeast Congo Lion
'use server';
import { verifyAccess } from '@/lib/actions/GuildsAction';
import { redirect } from 'next/navigation';
export default async function appLayout({ children, params }) {
const hasAccess = await verifyAccess(params?.id);
if (!hasAccess) {
redirect('/dashboard');
return null;
}
return children;
}
import { verifyAccess } from '@/lib/actions/GuildsAction';
import { redirect } from 'next/navigation';
export default async function appLayout({ children, params }) {
const hasAccess = await verifyAccess(params?.id);
if (!hasAccess) {
redirect('/dashboard');
return null;
}
return children;
}
export const verifyAccess = async (id: string) => {
const session = await getSession();
if (!session) {
return false; // Session does not exist
}
try {
await GetRequest(
return true;
} catch {
await deleteSession(); // Ensure deleteSession is executed in Server Action
return false;
}
};
const session = await getSession();
if (!session) {
return false; // Session does not exist
}
try {
await GetRequest(
/guilds/${id}/verify
, session.access_token);return true;
} catch {
await deleteSession(); // Ensure deleteSession is executed in Server Action
return false;
}
};
Try this