question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

calling getToken from GetServerSideProps

See original GitHub issue

Question šŸ’¬

Context

  • I have configured NextAuth to add the account.access_token to my jwt token on login/signup.
  • I want to pass the token to my data API (separate environment) in GetServerSideProps to get my hands on the data I need to render the page.
  • I’ve spent a large amount of time trawling the forums, trying to find the answer myself before posting on here.

Problem?

So my problem is that getToken() expects a req param of NextRequest | NextApiRequest, but inside GetServerSideProps the context.req prop has a type of IncomingMessage & { cookies: NextApiRequestCookies }, so understandably it throws the toys when I try and make the call to getToken using it.

Existing Workarounds

To get around this issue, my current bag of options are:

  • Call getToken with context.req as any - this works fine, but casting to any tears at my soul.
  • Add account.access_token to the session (in addition to the token) so I can get hold of it via getSession(). This works, but I dislike conceding to add the access token in to more places than it absolutely needs to be.
  • Call the decode method from next-auth/jwt manually and extract the token.

Question

How should I get hold of the decoded JWT token in GetServerSideProps when using Typescript? I don’t believe for a second that I have exposed any sort of flaw in the NextAuth code, and I doubt I’m the first person to want to do this, so there’s gotta be something obvious I’m missing here.

How to reproduce ā˜•ļø

import { GetServerSideProps } from 'next';
import { getToken } from 'next-auth/jwt';

const SomeComponent = (props: { someData: SomeData }) => (
  <div>{props.someData}</div>
);

export const getServerSideProps: GetServerSideProps = async context => {
  const token = await getToken({
    req: context.req,
    secret: SECRET_VALUE,
  });

  const someData = await getSomeData(token?.accessCode);

  return {
    props: {
      someData,
    },
  };
};

export default SomeComponent;

Contributing šŸ™ŒšŸ½

Yes, I am willing to help answer this question in a PR

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
balazsorban44commented, May 31, 2022

Please, there is no need to be dramatic, let’s use welcoming language as reflected in our Code of Conduct: https://github.com/nextauthjs/next-auth/blob/main/CODE_OF_CONDUCT.md#our-standards

The source code is publicly available and you marked ā€œYes, I am willing to help answer this question in a PRā€.

This seems to be an easy fix, check out the source code here:

https://github.com/nextauthjs/next-auth/blob/e203801f30e5b34d85bef617a1266c1b073bc8e5/packages/next-auth/src/jwt/index.ts#L40-L41

~Feel free to open a PR.~ šŸ‘ šŸ’š

UPDATE: Done in #4659

0reactions
mjthcommented, May 27, 2022

+1 on this issue. Looks like misconfigured types.

Packages: next: 12.1.6 next-auth: 4.1.4

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using getToken in getServerSideProps() Ā· Issue #720 - GitHub
I got it working by parsing the cookie manually using next-cookies and modifying the req object, then passing this to getToken():. importĀ ...
Read more >
Next-Auth: Cannot get Access Token with getToken() inside ...
always returns undefined inside the next-js API Middleware which is called by await axios. get() inside getServerSideProps() , is this normal?
Read more >
How to add a Bearer token to `getServerSideProps` api request
I get the token from ` @auth0/nextjs-auth0 by using an API route /api/token : The api route works fine on the client side...
Read more >
5b. Session verification in getServerSideProps - SuperTokens
1) We use getSession in getServerSideProps​​ The only difference is the way the props are returned (see comments in the code). An example...
Read more >
Securing pages and API routes - NextAuth.js
If data on a page is fetched using calls to secure API routes - i.e. routes which use getSession() or getToken() to access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found