New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
calling getToken from GetServerSideProps #4609
Comments
Meh, I guess I'll just bury my Happy to explore implementing a fix if I get a nod from one of the RepoLords though. |
Yes, I am doing something similar right now. Since I extend the token with additional data I am using a custom import { IncomingMessage } from 'http';
import { NextApiRequest } from 'next';
import { JWT, getToken as getAuthToken } from 'next-auth/jwt';
import { NextApiRequestCookies } from 'next/dist/server/api-utils'; // ugly import path!
import { NextRequest } from 'next/server';
interface ExtendedJWT extends JWT {
foo: string | null;
}
type GetServerSidePropsRequest = IncomingMessage & { cookies: NextApiRequestCookies };
export type Request = NextRequest | NextApiRequest | GetServerSidePropsRequest;
export const getToken = async (request: Request): Promise<ExtendedJWT | undefined> => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const token = await getAuthToken({ req: request as any });
if (!token) {
return;
}
if ('foo' in token) {
return {
...token,
foo: (typeof token.foo === 'string') ? token.foo : null,
};
}
}; I would love to see a PR that fixes this typing and allows me to remove the |
+1 on this issue. Looks like misconfigured types. Packages: |
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: next-auth/packages/next-auth/src/jwt/index.ts Lines 40 to 41 in e203801
UPDATE: Done in #4659 |
Question 馃挰
Context
account.access_token
to my jwt token on login/signup.GetServerSideProps
to get my hands on the data I need to render the page.Problem?
So my problem is that
getToken()
expects areq
param ofNextRequest | NextApiRequest
, but insideGetServerSideProps
thecontext.req
prop has a type ofIncomingMessage & { cookies: NextApiRequestCookies }
, so understandably it throws the toys when I try and make the call togetToken
using it.Existing Workarounds
To get around this issue, my current bag of options are:
getToken
withcontext.req as any
- this works fine, but casting toany
tears at my soul.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.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 鈽曪笍
Contributing 馃檶馃徑
Yes, I am willing to help answer this question in a PR
The text was updated successfully, but these errors were encountered: