Next.js Discord

Discord Forum

cookies on route handler

Unanswered
Gazami crab posted this in #help-forum
Open in Discord
Gazami crabOP
Hello,

So I'm trying to implement an OAuth integration with Airtable.
I want to set cookies in my route handler and then retrieve the cookie that was previously set in another route.

But I'm unable just to set the cookies whereas I followed the instructions from Nextjs: https://nextjs.org/docs/app/building-your-application/routing/route-handlers#dynamic-functions

Let me share my code to set the cookies:

import { NextResponse } from 'next/server'
import { cookies } from 'next/headers';
import crypto from 'crypto'; // Add this import


export async function GET(request, response) {
    try {

        const clientId = process.env.NEXT_PUBLIC_AIRTABLE_CLIENT_ID;
        const redirectUri = process.env.NEXT_PUBLIC_AIRTABLE_REDIRECT_URI;

        const codeVerifier = await generateCodeVerifier();
        const codeChallenge = await generateCodeChallenge(codeVerifier);
        const state = await generateState();

        const airtableURL = ..
        const cookieStore = cookies();
        cookieStore.set('code_verifier', codeVerifier, {
          httpOnly: true,
          sameSite: 'lax',
          path: '/',
        //   maxAge: 60 * 5, // 5 minutes, as the OAuth flow should complete quickly
        });
        cookieStore.set('state', state, {
          httpOnly: true,
          sameSite: 'lax',
          path: '/',
        });



Then trying to get the cookies

import { NextResponse } from 'next/server'
import { createClient } from '@/utils/supabase/server';
import { cookies } from 'next/headers';

export async function GET(request) {

    try {
        const cookieStore = cookies();
        const storedState = cookieStore.get('state');
        const storedCodeVerifier = cookieStore.get('code_verifier');
        
        console.log(storedState, storedCodeVerifier)

....

What's wrong with my code?
Thanks a lot!

0 Replies