【问题标题】:How to pull in Heroku postgres credentials for next-auth?如何为下一个身份验证提取 Heroku postgres 凭据?
【发布时间】:2021-04-29 14:07:34
【问题描述】:

我正在尝试在 Heroku 上使用带有 Next-Auth.js 的 postgres 实例 Heroku 的文档指出不应将凭据硬编码到应用程序中;所以,我正在尝试使用 Heroku 的 api 来获取所需的 url。我的问题 - 我认为 - 当我尝试异步运行 axios 请求时,return 语句的值没有分配给 options 对象的 database 属性。我究竟做错了什么?非常感谢!

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(),
});

【问题讨论】:

    标签: asynchronous heroku next.js next-auth


    【解决方案1】:

    您的 getCredentials 是一个异步函数,这意味着它返回一个承诺。因此,您需要await

    database: await getCredentials()
    

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 2022-01-10
      • 1970-01-01
      • 2022-09-28
      • 2018-11-28
      • 2020-07-22
      • 2021-11-29
      • 2019-04-26
      • 2020-01-11
      相关资源
      最近更新 更多