Next.js Discord

Discord Forum

Cookie Set in API Endpoint Response Not Appearing in Browser Developer Tools

Unanswered
Chalcid wasp posted this in #help-forum
Open in Discord
Chalcid waspOP
Hello, I have an api that returns data from the database. Whenever the access token is expired, I obtain a new token using the refresh token. I tried to return this token by returning a cookie as part of the api response. However, when I check the developer tool in chrome and firefox, the old access token does not seem to get updated. Help me fix this problem. Attached below is my code:

import { queryIncomingEvents } from '@/app/api/_utils/event';
import { NextRequest } from 'next/server';
import handleEndpointAuth from '@/app/api/_utils/handle_endpoint_auth';
import { AttributeValue } from '@aws-sdk/client-dynamodb';
import { errorBody } from '@/app/api/_utils/status_handler';

export const GET = async (request: NextRequest) => {
    try {
        const newToken = await handleEndpointAuth(request);

        const event_id = request.nextUrl?.searchParams.get('event_id') ?? null;
        const start_date =
            request.nextUrl?.searchParams.get('event_id') ?? null;
        let lastEvaluatedKey = null;

        if (typeof event_id === 'string' || typeof start_date === 'string') {
            lastEvaluatedKey = { event_id: event_id, start_date: start_date };
        }

        const response = await queryIncomingEvents(
            'EventDetails',
            lastEvaluatedKey as unknown as Record<string, AttributeValue>,
        );

        if (typeof newToken === 'object') {
response.cookies.delete('access_token');
            response.cookies.set({
                name: 'access_token',
                value: newToken.access_token,
                httpOnly: true,
                secure: true,
                path: '/',
                maxAge: newToken.expires_in,
            });
        }

        return response;
    } catch (error) {
        return errorBody(
            parseInt((error as any).status),
            (await (error as any).json()).message as string,
        );
    }
};

export const dynamic = 'force-dynamic';

0 Replies