Next.js Discord

Discord Forum

[next-auth] Get token for Github REST API

Answered
Puli posted this in #help-forum
Open in Discord
PuliOP
I want to implement user login and access some user data via the [Github REST API](https://docs.github.com/en/rest?apiVersion=2022-11-28) (eg. repository, issues).

Then I successfully implemented a login feature, but the Github REST API requires a token to access its endpoints. I checked the session using getServerSession(authOptions) and obtained some information, but not the token (as shown below).
"session": {
  "user": {
    "name": "UserName",
    "email": "xxxxxxxx@gmail.com",
    "image": "https://xxxxxx.xxxxxx/xxxxx"
  }
}


I tried some solutions on StackOverflow but none of them worked for me.
Is there somebody who can help me? Thansk.
Answered by Ray
https://next-auth.js.org/configuration/callbacks#session-callback
I think you need to add the token to the session object by the session callback
View full answer

7 Replies

Answer
@Ray https://next-auth.js.org/configuration/callbacks#session-callback I think you need to add the token to the session object by the session callback
PuliOP
I tried to add a console log statement to this callback function, then I got undefined 🤔
@Puli I tried to add a console log statement to this callback function, then I got undefined 🤔
ok add another callback function
async jwt({ token, user, account, profile, isNewUser }) {
    if (account) {
      token.accessToken = account.access_token
    }
    return token
  }
@Ray ok add another callback function ts async jwt({ token, user, account, profile, isNewUser }) { if (account) { token.accessToken = account.access_token } return token }
PuliOP
It works!
However, is there a better alternative than using session as any in Typescript?
@Ray yes check this out https://next-auth.js.org/getting-started/typescript
PuliOP
Thanks very much!! I really appreciate your help!!!! 😇