calling getToken from GetServerSideProps
See original GitHub issueQuestion š¬
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
withcontext.req as any
- this works fine, but casting toany
tears at my soul. - Add
account.access_token
to the session (in addition to the token) so I can get hold of it viagetSession()
. 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 fromnext-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:
- Created a year ago
- Reactions:2
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
+1 on this issue. Looks like misconfigured types.
Packages: next: 12.1.6 next-auth: 4.1.4