如何解决如何为 next-auth 提取 Heroku postgres 凭据?
我正在尝试在 Heroku 上使用 postgres 实例和 Next-Auth.js Heroku 的文档指出,不应将凭据硬编码到应用程序中;所以,我正在尝试使用 Heroku 的 api 来获取所需的 url。我的问题 - 我认为 - 是当我尝试异步运行 axios 请求时,返回语句的值没有分配给 database
对象的 options
属性。我究竟做错了什么?非常感谢!
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(),});
解决方法
您的 getCredentials
是一个异步函数,这意味着它返回一个承诺。因此,您需要为它await
。
database: await getCredentials()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。