'Error: Cannot set headers after they are sent to the client when using passport-oauth2 with pkce

Iam not sure where i called res twise in my code,iam trying to implement passport client for twitter oauth2 with pkce, any help finding out would be appreciated. thanks in advance


    import * as passportOAuth2 from 'passport-oauth2';
    import { TwitterProfile } from './types';
    
    export interface AuthorizationParams {
      response_type: string;
      client_id: string;
      redirect_uri: string;
      scope: string[];
      state: string;
      code_challenge: string;
      code_challenge_method: string;
    }
    
    export interface TokenParams {
      code?:string;
      grant_type: string;
      client_id:string;
      redirect_uri:string;
      code_verifier:string;
    }
    
    export interface TwitterOptions extends passportOAuth2.StrategyOptionsWithRequest {
      code_challenge: string;
    }
    
    export class Strategy extends passportOAuth2.Strategy {
      constructor(options: TwitterOptions, verify: passportOAuth2.VerifyFunctionWithRequest) {
        super(options, verify);
        this.name = 'twitter';
        this._oauth2.setAccessTokenName('access_token');
        this._oauth2.useAuthorizationHeaderforGET(true);
        this.tokenParams(
          {
          grant_type:'authorization_code',
          client_id: options.clientID,
          redirect_uri: options.callbackURL,
          code_verifier:'challenge'}
          );
        this.authorizationParams(
          {
            response_type:'code',
            client_id: options.clientID,
            redirect_uri: options.callbackURL,
            scope:['offline.access','users.read'],
            state:'state',
            code_challenge:'challenge',
            code_challenge_method:'plain'
          }
        );
      }  
      
      authorizationParams(options:AuthorizationParams){
        return options;
      };
      tokenParams(options:TokenParams){
        return options;
      }
    
      userProfile = (
        accessToken: string,
        done: (err?: Error | null, profile?: any) => void,
      ) => {
        this._oauth2.get(
          `${process.env.TWITTER_OAUT2_PROFILE_URI}`,
          accessToken,
          (err, result: string) => {
            if (err) {
              return done(new Error(JSON.stringify(err)));
            }
            try {
              const profile: TwitterProfile = JSON.parse(result);
              return done(null, profile);
            } catch (e) {
              return done(e);
            }
          }
        );
      }
    }


    import { PassportStrategy } from "@nestjs/passport";
    import { Injectable } from "@nestjs/common";
    import { TwitterProfile } from "../passport/twitter/types";
    import { Strategy } from "../passport/twitter";
    
    @Injectable()
    
    export class TwitterStrategy extends PassportStrategy(Strategy, 'twitter'){
        constructor(){
            super({
                    authorizationURL:process.env.TWITTER_OAUTH2_AUTHORIZATION_URI,
                    clientID: process.env.TWITTER_OAUTH2_CLIENT_ID,
                    clientSecret: process.env.TWITTER_OAUTH2_CLIENT_SECRET,
                    callbackURL: process.env.TWITTER_OAUTH2_REDIRECT_LOCAL,
                    tokenURL:process.env.TWITTER_OAUT2_TOKEN_URI,
                    scope: ['offline.access', 'users.read'],
                    passReqToCallback : true,
                    pkce: 'plain',
                    state:'state',                
                    customHeaders: {
                            Authorization:
                                "Basic " +
                                Buffer.from(`${process.env.TWITTER_OAUTH2_CLIENT_ID}:${process.env.TWITTER_OAUTH2_CLIENT_SECRET}`).toString('base64')
                        }
                }, (req: Request, accessToken:string, refreshToken:string, profile:TwitterProfile, done:any)=>{
                    return done(null, profile);
                }
            );
        }

Error: Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:371:5) at ServerResponse.setHeader (node:_http_outgoing:576:11) at ServerResponse.header (node_modules\express\lib\response.js:776:10) at ServerResponse.json (node_modules\express\lib\response.js:264:10) at ExpressAdapter.reply (node_modules@nestjs\platform-express\adapters\express-adapter.js:42:62) at ExceptionsHandler.handleUnknownError (node_modules@nestjs\core\exceptions\base-exception-filter.js:38:24) at ExceptionsHandler.catch (node_modules@nestjs\core\exceptions\base-exception-filter.js:17:25) at ExceptionsHandler.next (node_modules@nestjs\core\exceptions\exceptions-handler.js:16:20) at node_modules@nestjs\core\router\router-proxy.js:24:35 at Layer.handle_error (node_modules\express\lib\router\layer.js:71:5)

this error appears only once , for the subsequent attmpts , i am not seeing any error as well not flowing forward.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source