#I never worked on this particular

1 messages · Page 1 of 1 (latest)

covert sparrow
#
//frontend side method
    const postTweetFF = async () => {
        const res = await axios
            .post('/api/firebase_functions/tweetv2', // made a file in /api to redirect the request but didn't work either
                { uid: auth.currentUser.uid },
                { 'Content-type': 'application/json' }
            )
        console.log('res: ', res);
        console.log('res.data: ', res.data);
    }

//---------------------------------------------------------------------------------------------------------------
//api route in /pages/api/firebase_functions/tweetv2
import axios from "axios";

export default async function handler(req, res) {
    try {
        const data = await axios
            // .post('http://localhost:5001/****/us-central1/tweetv2',
            .post('https://us-central1-****.cloudfunctions.net/tweetv2',
                { uid: req.body.uid })
        res.status(200).JSON(data)
    } catch (error) {
        console.log('error: ', error);
        return res.status(error.status || 500).end(error.message)
    }
}

//------------------------------------------------------------------------------------------------
//firebase function that exists at http://localhost:5001/****/us-central1/tweetv2 and https://us-central1-****.cloudfunctions.net/tweetv2
exports.tweetv2 = functions.https.onRequest(async (req, res) => {
    const uid = req.body
    console.log('uid: ', uid);

    const tempDB = admin.firestore().doc(`users/${uid}`)
    const someData = (await tempDB.get()).data()

    const userClient = new TwitterApi({
        appKey: process.env.TWITTER_API_KEY_CONSUMER_KEY,
        appSecret: process.env.TWITTER_API_KEY_SECRET_CONSUMER_SECRET,
        accessToken: someData.twitter_oauthAccessToken,
        accessSecret: someData.twitter_oauthTokenSecret
    })

    const { data } = await userClient.v2.tweet(`sample tweet at ${(new Date()).toLocaleString("en-US", { timeZone: "America/Los_Angeles" })}`)
    res.send(data)
})
#

that's where i'm at, the functions work indepdently until i try to start posting from localhost3000 to the prod server or localhost5001

covert sparrow
#

@raven solstice @ancient cargo

raven solstice
#

whats the issue with the api route?

#

btw res.JSON(data) is not valid, you probably meant .json

covert sparrow
# raven solstice whats the issue with the api route?

this is my request

        const res = await axios
            .post('/api/rewrite/tweetv2', // using rewrites() in next.config.js but didn't work
                { uid: auth.currentUser.uid },
                { 'Content-type': 'application/json' }
            )

this is my exports in next.config.js with the rewrite from /api/rewrite to http://localhost:5001/****/us-central1/

const moduleExports = {
  // Your existing module.exports
  reactStrictMode: true,
  swcMinify: true,
  pageExtensions: ['mdx', 'md', 'jsx', 'js', 'tsx', 'ts', 'json'],
  images: {
    domains: ['pbs.twimg.com'],
  },
  experimental: {
    images: {
      allowFutureImage: true,
    },
  },
  async rewrites() {
    return [
      {
        source: '/api/rewrite/:slug*',
        destination: 'http://localhost:5001/****/us-central1/:slug*',
        // destination: 'https://us-central1-****.cloudfunctions.net/:slug*',
      },
    ]
  },
  async headers() {
    return [
      {
        source: "/api/rewrite/:slug*",
        // source: "/api/firebase_functions/:path*",
        headers: [
          { key: "Access-Control-Allow-Credentials", value: "true" },
          { key: "Access-Control-Allow-Origin", value: "*" },
          { key: "Access-Control-Allow-Methods", value: "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
          { key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" },
        ]
      }
    ]
  }
};
module.exports = withSentryConfig(moduleExports, sentryWebpackPluginOptions);
covert sparrow
raven solstice
#

you need to choose, you either do a proxy with rewrites or you create an api route to proxy the request

covert sparrow
raven solstice
#

I suggest you to try with the rewrite only

#

you dont need this headers field

#

the rewrites would be only ```
async rewrites() {
return [
{
source: '/api/rewrite/:slug*',
destination: 'YOUR_DESTINATION_HOST/***/us-central1/:slug',
},
]
},