Accessing Github REST API with Next-auth token

Looking to enable user login and retrieve specific user data using the Github REST API, such as repositories and issues.

After successfully implementing the login feature, I encountered an obstacle with the Github REST API that requires a token for endpoint access. Despite checking the session with getServerSession(authOptions) and retrieving some information, the token was not included (refer to snippet below).

"session": {
  "user": {
    "name": "UserName",
    "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="84fcfcfcfcfcfcfcfcc4e3e9e5ede8aae7ebe9">[email protected]</a>",
    "image": "https://xxxxxx.xxxxxx/xxxxx"
  }
}

I've attempted various solutions from StackOverflow without success.
Is there anyone out there who can assist me? Thanks in advance.

Answer №1

With the assistance of someone in the next.js Discord server, I was able to find a solution.
Check out the documentation here: https://next-auth.js.org/configuration/callbacks#sign-in-callback

export const authOptions : NextAuthOptions = {
  adapter: ...,
  session: {
    strategy: "jwt", // include this
  },
  providers: [...],
  // add two callback functions below
  callbacks: {
    async session({ session, token, user }) {
      session.accessToken = token.accessToken as string;
      return session;
    },
    async jwt({ token, user, account, profile }) {
      if (account) {
        token.accessToken = account.access_token;
      }
      return token;
    }
  },
};

If you are using TypeScript, remember to add this code snippet to "types/next-auth.d".
Refer to the docs here: https://next-auth.js.org/getting-started/typescript

import NextAuth, { DefaultSession } from "next-auth";

declare module "next-auth" {
  interface Session {
    accessToken: string;
    expires: string;
    user: {
      address: string
    } & DefaultSession["user"];
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling with rendering object in Next.js, receiving an error stating "map is not a function."

Currently, I am attempting to display data fetched from STRAPI using Graphql and Next.js. Fortunately, my event Adapter is functioning perfectly. However, when trying to showcase this data on the UI, an error occurs stating event.map is not a function. Whi ...

Angular: Tailoring the Context Menu

Currently, I am utilizing a helpful tutorial to implement a custom context menu feature. The main issue I am facing is that when a user interacts with the list items, I want the correct index of each item to be displayed. However, at the moment, clicking o ...

Typescript type for selecting only string[] keys in an object

I am looking for a specific type: interface Filter<P extends object> { label: string; options: FilterOption[]; key: StringArrayKeyOf<P>; } The definition of StringArrayKeyOf is as follows: type StringArrayKeyOf<T extends object> = ...

How can I implement Data Caching in Next.js 13 (App Router) by accessing the DataBase directly instead of using fetch?

The Next.js 13 documentation, available at https://nextjs.org/docs/app/building-your-application/caching#data-cache, mentions the possibility of utilizing data-cache in fetch within a server component to cache data. I'm interested in achieving a simi ...

Tips on effectively rendering child components conditionally in React

My components currently consist of an AddBookPanel containing the AddBookForm. I am looking to implement a feature where the form is displayed upon clicking the 'AddBookButton', and hidden when the 'x' button (image within AddBookForm c ...

The function that was provided as a parameter is not functioning as expected

I have a WsConnector class responsible for connecting to my backend application and subscribing to a WebSocket topic. export class WsConnector { private stompClient: any; connect(onMessage: (msg) => void) { this.stompClient = Stomp.over ...

"What is the best way to determine the data type of an object retrieved from an API in TypeScript

Hey there, I'm currently developing a web application using Angular 2 and I'm focusing on implementing an exception handling mechanism. To achieve this, I've created a model that mirrors the object structure I will receive from the server (E ...

Encountering an uncaughtException: Error stating that the module '....nextserverapphomelibworker.js' cannot be located while attempting to utilize pino.transport in Next.js

I recently set up a Next.js project with typescript using create-next-app. Opting for Pino as the logging library, recommended by Next.js, seemed like the logical choice. Initially, when I utilized Pino without incorporating its transport functionality, e ...

Step-by-step guide on building an API to push form input data into a MongoDB document as a subdocument

Is it possible to achieve this using the fetch API? Which method should I use, POST or PUT? Please provide a response. Can someone please give me the complete API code to push orderSchema as a subdocument in userSchema? I have searched through numerous lin ...

Confused about the functionality of the map feature in Angular

I am new to Angular and currently working on an application that utilizes the typeahead functionality. I have set up a HTTP call through my Express backend on GCP to fetch search results from the TMDB database based on a search keyword. this.http .ge ...

Best practices for receiving messages from SQS in Node.js

I am exploring how to implement SQS in a similar manner as RabbitMQ or Kafka, where we usually set up a listener. However, after going through their documentation, I couldn't find any instructions on how to set up a listener for SQS: Currently, I am ...

"TypeScript function returning a boolean value upon completion of a resolved promise

When working on a promise that returns a boolean in TypeScript, I encountered an error message that says: A 'get' accessor must return a value. The code snippet causing the issue is as follows: get tokenValid(): boolean { // Check if curre ...

Error: Cannot access Angular 5 Title service at this time

When attempting to change the page title using BrowserModule, I encountered an issue. I added the BrowserModule and Title in the application module as shown here: https://angular.io/guide/set-document-title However, in a child module where I tried to add ...

Securing your Next.js App with Express JWT Authentication

After setting up a /api/login endpoint to handle user login functionality, I created a login.js page to send form data to the login API. The login API authenticates user credentials stored in the database, generates a token, and saves it to a cookie. awai ...

I am a beginner in the world of Typescript/Angular, and I have encountered a compiling error TS2339. It seems that even when the condition *ngIf="x.length > 0;" should be false, the

I'm currently enrolled in a Typescript/Angular course where I am learning about the implementation of "*ngIf". During one of the lessons, the instructor provided an example using an empty array to demonstrate how it fails the condition and results in ...

How can I access keys and values from an Observable within an Angular template?

Attempting to display all keys and values from an Observable obtained through Angular Firebase Firestore Collection. This is how I establish a connection to the collection and retrieve an Observable. The function is called subsequently. verOrden : any; ...

Would you recommend incorporating SWR for refreshing prefetched data alongside SSR in a nextJS application?

Consider the webpage below: export default ({ SSRtasks }) => { const [ tasks, setTasks ] = useState(SSRtasks) const { data: freshTasks, mutate } = useSwr('/api/tasks') useEffect(() => freshTasks && setTasks(freshTasks), [ fres ...

Pause the React rendering process to initiate a redirection

Currently, I am developing a React application using Next.js and facing a specific challenge that requires a solution: There are certain pages in my application that should only be accessible once a particular context has been established. To achieve this ...

Having trouble locating the name 'it' in Jest TypeScript

After setting up Jest in React + TypeScript, I encountered an error when trying to run a test using the command npm test. The error message displayed was: Cannot find name 'it'. Do you need to install type definitions for a test runner? Try ` ...

How to attach a local directory to a Docker container

I am currently working on containerizing my Next.js app for production purposes and have encountered an issue with the setup. Here is how my `docker-compose.yml` file is set up: version: '3.9' services: app: container_name: webapp buil ...