如何获取Heroku Postgres凭据以进行下一次身份验证? [英] How to pull in Heroku postgres credentials for next-auth?

查看:67
本文介绍了如何获取Heroku Postgres凭据以进行下一次身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

您的 getCredentials 是一个异步函数,表示它返回了promise.因此,您需要等待.

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

database: await getCredentials()

这篇关于如何获取Heroku Postgres凭据以进行下一次身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆