Typescript error: Type 'undefined' cannot be used as an index type.ts(2538) #2886
-
I was following along https://spacejelly.dev/posts/how-to-make-twitter-api-requests-with-nextauth-js-session-tokens/#step-1-accessing-twitter-oauth-tokens-in-a-nextauthjs-session & found this chunk of code written in JS: callbacks: {
async jwt({ token, user, account = {}, profile, isNewUser }) {
if (account.provider && !token[account.provider]) {
token[account.provider] = {}
}
if (account.access_token) {
token[account.provider].accessToken = account.access_token
}
if (account.refresh_token) {
token[account.provider].refreshToken = account.refresh_token
}
return token
},
}, On the 2nd & 3rd if statements, I get an error on
I've tried all general methods to fix it using TS like What's the solution for this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
just simplify to one if statement with This should work: async jwt({ token, account }) {
if (account) {
token[account.provider] = {
accessToken: account.access_token,
refreshToken: account.refresh_token,
}
}
return token
} Furthermore, to make TS happy, check out the docs at: https://next-auth.js.org/getting-started/typescript#submodules declare module "next-auth/jwt" {
interface JWT {
[key: string]: {
accessToken: string
refreshToken: string
}
}
} |
Beta Was this translation helpful? Give feedback.
just simplify to one if statement with
if(account)
This should work:
Furthermore, to make TS happy, check out the docs at: https://next-auth.js.org/getting-started/typescript#submodules