write access token in jwt callback with TypeScript #4735
-
I'd like to extract the access token from my authentication session to pass in the header of other API calls in an application. The docs suggest doing this by writing the access token to an additional field on the callbacks: {
async jwt({ token, account }) {
// Persist the OAuth access_token to the token right after signin
if (account) {
token.accessToken = account.access_token
}
return token
}
} Confusingly the Has anyone else dealt with this problem and found a solution? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
JWT does not have accessToken field as you said. You would need to use module augmentation as per documentation if you are using typescript. I believe the code you posted works fine for plain JavaScript. Doc link: https://next-auth.js.org/getting-started/typescript#module-augmentation |
Beta Was this translation helpful? Give feedback.
-
If any of this is about my account accesses, please make sure to contact me
privately Immediately before you do anything ! thanks ! Code of conduct
please!
Thanks,
Justin
ᐧ
…On Mon, Jun 20, 2022 at 4:41 PM Kyle Duffy ***@***.***> wrote:
I'd like to extract the access token from my authentication session to
pass in the header of other API calls in an application. The docs
<https://next-auth.js.org/configuration/callbacks#jwt-callback> suggest
doing this by writing the access token to an additional field on the token
object in the jwt callback when the user signs in:
callbacks: {
async jwt({ token, account }) {
// Persist the OAuth access_token to the token right after signin
if (account) {
token.accessToken = account.access_token
}
return token
}}
Confusingly the token object is a JWT type which doesn't have an
accessToken field, producing an unknown type that breaks any downstream
type checks. Unfortunately I can't find a workaround with a union or
extending type because the jwt callback is expected to produce a JWT.
Has anyone else dealt with this problem and found a solution?
—
Reply to this email directly, view it on GitHub
<#4735>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AYXI3IVDZGYXK6XOK5EVPGLVQDJQ3ANCNFSM5ZKBLV2A>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
JWT does not have accessToken field as you said. You would need to use module augmentation as per documentation if you are using typescript. I believe the code you posted works fine for plain JavaScript.
Doc link: https://next-auth.js.org/getting-started/typescript#module-augmentation
Here's what I do to log the callbacks for learning the flow