#Session

1 messages · Page 1 of 1 (latest)

shrewd marlinBOT
#

Hi @viscid fulcrum. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To create a session with cookies in Next.js, you can use the cookies function to read and write cookies. Here's a step-by-step guide on how to do this:

  1. In your server action or route handler, import the cookies function from next/headers.
import { cookies } from 'next/headers';
  1. To read cookies from the incoming request, use the get method.
const cookieStore = cookies();
const session = cookieStore.get('session');
  1. To set a cookie for the outgoing response, use the set method. This method is only available in a Server Action (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions) or Route Handler (https://nextjs.org/docs/app/building-your-application/routing/router-handlers).
cookies().set('session', 'your-session-value');
// or
cookies().set('session', 'your-session-value', { secure: true });
// or
cookies().set({
  name: 'session',
  value: 'your-session-value',
  httpOnly: true,
  path: '/',
});
  1. To delete a cookie, use the delete method.
cookies().delete('session');

Keep in mind that the information provided is based on the available extracts. For more details and examples, refer to the following sources:

Please note that Next.js 13 is not mentioned in the provided extracts. The information above is based on the available documentation and may not be specific to Next.js 13.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@warm rose kapa.ai is still learning and improving, please let us how it did by reacting below

#

Let me check the knowledge base for you. One moment! :books:

#

It seems like you're setting the cookies in the response headers, but you're not using the cookies function from next/headers to set them. To set cookies properly, you should use the cookies function as shown in the provided extracts.

Here's an updated version of your code with the correct usage of the cookies function:

import { NextRequest, NextResponse } from "next/server";
import { PrismaClient } from '@prisma/client'
import { ID } from "@/utils/SnowFlake";
import crypto from 'crypto';
import bcrypt from 'bcrypt';
import { cookies } from "next/headers";
const prisma = new PrismaClient()

const snowflake = new ID(1);
export async function POST(req: NextRequest, res: NextResponse) {
    // ... (rest of the code)

    if (user) {
        console.log(`New user created: ${user.username}#${user.tag}`)
        
        const responseCookies = cookies();
        responseCookies.set('user', JSON.stringify(user), { path: '/', maxAge: 3600 });
        responseCookies.set('token', user.token, { path: '/', maxAge: 3600 });

        return new Response(JSON.stringify(user), { status: 200, statusText: "success" });
    } else {
        return new Response("error_creating_user", { status: 200, statusText: "error_creating_user" });
    }
}

// ... (rest of the code)

In this updated version, we're using the cookies function to set the cookies for the outgoing response. This should ensure that the cookies are set correctly and can be accessed in other files.

For more information on working with cookies in Next.js, refer to the following sources: