Unable to retrieve property from NextRequest

Currently, I am attempting to utilize MiddleWare in conjunction with Next.js's Middleware and JWT.

Upon logging cookies and the typeof cookies variable, this is what I see on my console:

{
  token: 'token='myToken'; Path=/'
}

object

Below is the code that I am working with:

import { NextRequest, NextResponse } from "next/server";
import { verify } from "jsonwebtoken";

const SECRET = process.env.SECRET;

export function middleware(req, res) {
  const cookies = req.cookies;
  
  const url = req.url;
  
  if (url.includes("/admin")) {
      console.log(cookies)
      console.log(typeof cookies)
    
  }

  return NextResponse.next();
}

However, when attempting to access the token property using cookies.token, it returns undefined.

Any insights into what might be causing this issue?

Answer №1

Humorous Response:

All I have to do is employ the get method in order to obtain the desired property

const snacks = req.cookies.get('token');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to retrieve data from response using promise in Angular 2?

I am struggling to extract the desired data from the response. Despite trying various methods, I can't seem to achieve the expected outcome. facebookLogin(): void { this.fb.login() .then((res: LoginResponse) => { this.acce ...

Filter the JSON using jq to select one attribute that matches another

Here's the JSON input: { "data": { "zone": [ { "record": [ { "Line": 1, "raw": "; cPanel first:11.25.0-CURRENT_46156 (update_time):1708598274 Cpanel::ZoneFile::VERSION:1.3 hostname:server2.somethin ...

In Chrome, the `Jquery $('input[name=""]').change(function will not be triggered unless there is an unhandled error following it

Here is a script that I've created to make certain inputs dependent on the selection of a radio button. The 'parent' input in this case is determined by the radio button choice. Upon document load, the script initially hides parent divs of ...

Having trouble clicking a button using Python and Selenium that triggers the `openWindow` function

I am attempting to interact with a button using Python Selenium WebDriver (Chrome). Here is the HTML code of the button: <button type="button" class="button blue" onclick="openWindow(LINK_HERE, 'idpage6')">Like</button> (I had to re ...

Improving the efficiency of to_json for deeply nested objects

Imagine a standard relational structure: Answer has multiple comments. Comments are associated with a user and have multiple likers. answers.to_json(:include => {:comments => {:include => :user, :likers}}) When the to_json method is called, dat ...

Maintaining the image's aspect ratio using the Next Image component

Currently, I am using Next's Image component to display a logo. Although the image itself is 1843x550 px, I have specified the width and height properties to adjust it to fit within a space of 83x24 px. However, due to a slightly different aspect rati ...

Unable to show information in Next.js Strapi integration

I have set out to create a basic task management website as a way to delve into the world of full stack development. My tech stack includes Next.js and Strapi, but I've hit a roadblock. Despite my best efforts, I can't seem to get the server data ...

Error: jquery unexpectedly encountered a token 'if'

I've successfully created an autocomplete suggestion box, but I'm facing an issue when using if and else along with console.log(). An error is displayed in my console saying Uncaught SyntaxError: Unexpected token if, and I'm not sure why. Ho ...

Integrate a PHP variable into an HTML/JavaScript statement that was previously transformed from PHP

Incorporated within this PHP variable is a mix of HTML and JavaScript code. My task is to enhance it by adding another PHP variable. Specifically, I have a PHP variable denoted as $user->user_email. How can I seamlessly integrate this variable into th ...

The expiration time and date for Express Session are being inaccurately configured

I'm experiencing an issue with my express session configuration. I have set the maxAge to be 1 hour from the current time. app.use( session({ secret: 'ASecretValue', saveUninitialized: false, resave: false, cookie: { secure ...

Refresh the page without reloading to update the value of a dynamically created object

Maybe this question seems silly (but remember, there are no stupid questions).. but here it goes. Let me explain what I'm working on: 1) The user logs into a page and the first thing that happens is that a list of objects from a MySQL database is fet ...

The issue of jQuery AJAX functionality not functioning properly when using Internet Explorer

I have encountered an issue that many others have faced on various platforms, but none of the suggested solutions seem to resolve it for me. My problem lies within a jQuery AJAX call to an ASHX handler. While the code functions smoothly in Firefox and Chr ...

Error encountered while utilizing a custom third-party component within an Angular project during static analysis

Currently, I am utilizing an Angular (2+) datepicker component (link to Github) in two separate Angular projects: Angular CLI v1.0.0-beta.30, Angular v2.3 Angular CLI v1.0.0, Angular v4.0 The first project works flawlessly both during development with n ...

What is the issue with this asynchronous function?

async getListOfFiles(){ if(this.service.wd == '') { await getBasic((this.service.wd)); } else { await getBasic(('/'+this.service.wd)); } this.files = await JSON.parse(localStorage.getItem('FILENAMES')); var ...

The Restful API is receiving a request with an empty object

I am facing an issue with my MVC application where the API call is passing a null object. Despite debugging the code and confirming that the data fits the model before calling the API, the object appears to be null during the API call. Interestingly, all o ...

What is the best way to have an image trail another in JavaScript coding for my game?

Hey, can someone help me create a zombie game where a zombie image moves towards a player image (zs.png) at a specific speed and decreases the player's health upon contact? This game is designed for the 3ds browser, so it must be coded in JavaScript ...

What is the preferred method for initiating a call from a JSP to a servlet using an href link?

Currently, I am successfully using href to call a servlet URL. However, I would like to add parameters and receive a response from this request. Is it possible to achieve this? I attempted an AJAX call but encountered a CORS issue when trying to call an ex ...

I need help determining the starting date and ending date of the week based on a given date

I am looking to determine the starting date (Monday) and ending date of a specified date using Javascript. For instance, if my date is 2015-11-20, then the starting date would be 2015-11-16 and the ending date would be 2015-11-21. ...

Transfer the cropped image to the database through AJAX on the client side and PHP on the server side

I am attempting to upload an image to a Database using JavaScript on the client-side and PHP on the server-side. The first step is selecting an image from the gallery. After zooming and cropping the image, it should be passed to the database. The issue ...

Use AJAX request to download file and handle errors in case the file is not found or cannot be downloaded

Trying to implement an ajax request for downloading a file. Here's the code snippet: const downloadFile = (element) => { $.ajax({ url: element.id, type: 'GET', success: (result) => { window.lo ...