The NextAuth implementation in API routes seems to be causing issues as the unstable_getServerSession and getToken functions consistently

Struggling with utilizing the unstable_getServerSession in a nextJS API route after switching from a custom authentication method. Wondering if I may have misconfigured JWT or missing something obvious.

Referenced the tutorial "Securing API routes": pages/api/get-session-example.js https://next-auth.js.org/tutorials/securing-pages-and-api-routes#securing-api-routes

  • Utilizing the getServerSideProps function to make an Axios call to my next/API endpoint
  • Sending a Bearer token in the header of the next/API call
  • Expecting the API endpoint to authenticate the user using JWT
  • Subsequently fetching Stripe subscriptions based on the stripe_customer_id embedded in the JWT

However, the session and token within the next/API endpoint always return as null

This is the snippet for my getServerSideProps function:


// Code block here...

For the next/API endpoint /api/account/subscriptions:


// Code block here...

Here's the complete [...nextauth].ts file:

Mentioned that the JWT is created by a Rails backend.


// Code block here...

Seeking assistance in resolving this issue which has been persisting for days without resolution. Cross-posting from GitHub to StackOverflow in hopes of getting insights from the community.

Answer №1

handling jwt callback

 return Promise.resolve(token)

dealing with session callback

return Promise.resolve(session)

executing authorize function

if (res.ok && user) {
      return Promise.resolve(user)
  }

updating user in sessions callback

session.user = token.user 

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

mobx: invoking a class method upon data alteration

Is it possible to utilize the Mobx library in order to trigger a class method whenever data changes? For instance, when MyObject assigns a value of 10 to container['item'], can we have the myaction method invoked? class MyElement extends Compone ...

When working with Typescript, it's important to handle errors properly. One common error you might encounter is: Error:(54, 33) TS2686: 'fabric' refers to a UMD global, but the current file is a module

Encountering an Issue: import {Canvas} from "fabric"; Error:(54, 33) TS2686:'fabric' refers to a UMD global, but the current file is a module. Consider adding an import instead. In my Angular project with TypeScript, I am using fabric which ...

Unable to locate the control specified by the path: 'files -> 0 -> postId'

I am in the process of creating a dynamic form with formArray and running into an issue. When I click on the AddItem button, it should create an input field for uploading files. Here is the HTML code snippet I am using: <div class="row m-auto col-md-1 ...

Can you please explain the distinction between the .ts and .tsx file extensions? They are both commonly used for TypeScript files in a React environment, but what is the specific use case for each

Hello, I'm in the process of learning React and as I work on my project, I've noticed files with both the .ts and .tsx extensions. I'm a bit confused about when to use .ts versus .tsx. Any guidance on this topic would be greatly appreciated. ...

What is the reason for the presence of additional mandatory dependencies in the package-lock.json file?

The recent release of React includes breaking changes to the TypeScript typings, causing packages that require "@types/react" with a wildcard version to automatically target this new version and break my project. Initially, I wanted to reach out to projec ...

"Seeking guidance on getting my carousel functionality up and running in Angular 8 - any

I tried implementing a carousel from the Bootstrap 4 documentation, but it is only displaying one image. How can I modify the carousel to show all images? I am new to using Angular. Below is the code I have: <div class=" bg-success text-white py-5 tex ...

Access the plugin object from a Vue.js 2 component using typescript

I devised a plugin object to handle the regular expressions used in my application in a more global manner. Here's an example of how it looks: import Vue from "vue"; Vue.prototype.$regex = { //isEmail function implementation goes here } ...

Leveraging Promise in conjunction with async/await

As I venture into the world of async/await in TypeScript, I find myself pondering a few questions. Specifically, I have been working on a function to extract an ArrayBuffer from a Blob. async function readAsArrayBuffer(blob: Blob): Promise<ArrayBuffer& ...

Instead of retrieving documents, the MongoDB query is returning topology information

I am currently working with MonogDB and NextJS. My goal is to create an endpoint that returns all the users. However, I am encountering a strange response: { "_events": {}, "_eventsCount": 0 } My code for querying the database looks li ...

Angular: Implementing conditional HTTP requests within a loop

Currently, I am facing a challenge where I need to loop through an array of objects, check a specific property of each object, and if it meets certain criteria, make an HTTP request to fetch additional data for that object. The code snippet below represen ...

Angular - Retrieve Excel file with its current filename

Is there a way for me to save an Excel file with its original filename from a service in my Angular app using the file-saver library? Below is my current code: let blob = new Blob([response], {type: 'application/vnd.openxmlformat-officedocument.spre ...

Is the subdirectory set as "/blogs" using firebase?

Can a NextJs app be added as a subdirectory to an existing ReactJs app on firebase hosting? I have the main app already deployed on firebase with its own domain, and I want to include a NextJs app in the path "/blogs" for SEO reasons. Has anyone successful ...

What could be causing the lack of updates in my shared service across all components?

I have implemented an Angular2 app where I am initializing an authentication service called LocalStorage which I want to be accessible across all my components: bootstrap(AppComponent, [ ROUTER_PROVIDERS, LocalStorage ]); The definition of the Lo ...

Error: The specified property 'Body' is not found within the type '{}'

Looking for some assistance here. I've created an http.get method like this: return this.http .get(url) .map((response: Response) => { response = response.json(); // console.log('The http get response', respon ...

MQTT Broker specialized in Typescript

I'm looking to create a MQTT Broker using TypeScript and Angular. I've attempted a few examples, but I keep encountering the following error: Uncaught TypeError: http.createServer is not a function Here's a simple example of what I'm c ...

New Image module featuring multiple SrcSet-Images

I am attempting to convert a traditional picture-tag with various media images into next.js Image components: Original: <picture> <source media="(min-width:740px)" srcSet={block.imageLandscape.url} /> <img src={bloc ...

Sending data to a parent component from a popup window in Angular Material using a button click while the window is still open

How can I retrieve data from an Angular Material Dialog Box and send it to the Parent component? I am able to access data after the dialog box is closed. However, I am wondering if there is a way to retrieve data while the dialog box is still open, especi ...

Is there a more efficient method in Typescript to define the types of an object containing arrays?

Suppose I have an object that contains various roles, each granting a specific set of rights: const allRoles = { user: [ 'right1' ], admin: [ 'right1', 'right2' ], } as const If I want to define the types ...

Utilizing Typescript and sinon to mock the functionalities of jsonwebtoken

Looking for help with mocking the function verify in Typescript's jsonwebtoken library. I've installed typescript, jsonwebtoken, sinon, mocha, and chai along with their corresponding types. However, when trying to stub the function, an error occu ...

Encountering compilation errors during the vue-cli build process

My Vue 2.2.3 application is experiencing difficulties in the build process due to 4 TypeScript-related errors that are perplexing me. This is the error output displayed on the console: Failed to compile with 4 errors ...