#Next.js server Action cookes error

1 messages · Page 1 of 1 (latest)

prisma wren
#

Hi, please help me.
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;
  }
};

fiery spruceBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

prisma wren
#

/dashboard/[id]/layout.tsx

'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.

prisma wren
#

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;
}
pale owlBOT
# prisma wren WHYYY is working???????????????? ```ts 'use server'; import { verifyAccess } fr...
Please add more information to your question

Your question currently does not have sufficient information for people to be able to help. Please add more information to help us help you, for example: relevant code snippets, a reproduction repository, and/or more detailed error messages. See more info on how to ask a good question in https://discord.com/channels/752553802359505017/1138338531983491154 and #welcome message.

fiery spruceBOT
light vine
#

'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;
}

#

export const verifyAccess = async (id: string) => {
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