Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

asynchronous - How to pull in Heroku postgres credentials for next-auth?

I'm trying to use a postgres instance on Heroku with Next-Auth.js Heroku's documentation notes that the credentials shouldn't be hardcoded into the application; so, I'm trying to use Heroku's api to pull in the needed url. My issue - I think - is when I try to run the axios request asynchronously, the value of the return statement isn't being assigned to the database property of the options object. What am I doing wrong? Many thanks!

import NextAuth from "next-auth";
import Providers from "next-auth/providers";
const axios = require("axios");

// Heroku api key and postgres instance
const herokuApiKey = PROCESS.ENV.API_KEY;
const herokuPostgres = PROCESS.ENV.POSTGRES_INSTANCE;

// Connection to Heroku API
const herokuApi = axios.create({
  baseURL: `https://api.heroku.com`,
  headers: {
    Authorization: `Bearer ${herokuApiKey}`,
    Accept: "application/vnd.heroku+json; version=3",
  },
});

// Async function to get database string
const getCredentials = async () => {
  const response = await herokuApi.get(`addons/${herokuPostgres}/config`);
  const pgConnStr = response.data[0].value; // Logging this value displays the needed string
  return pgConnStr; 
};

export default async (req, res) => NextAuth(req, res, {
  providers: [
    Providers.Email({
      server: process.env.EMAIL_SERVER,
      from: process.env.EMAIL_FROM,
    }),
  ],
  database: getCredentials(),
});

question from:https://stackoverflow.com/questions/65890362/how-to-pull-in-heroku-postgres-credentials-for-next-auth

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your getCredentials is an async function, meaning it returns a promise. As such you'll need to await for it.

database: await getCredentials()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...