尝试使用auth 0 token使用axios.create创建新示例时出现异步/等待问题,

d7v8vwbk  于 2023-10-18  发布在  iOS
关注(0)|答案(1)|浏览(132)

未及时解析承载令牌:
Bearer [object Promise]

import axios from 'axios'
import { Auth0Client } from "@auth0/auth0-spa-js";
import { auth0Config } from 'src/config';
import { paths } from 'src/paths';

const baseURL = 'http://localhost:3001'

const auth0 = new Auth0Client({
  domain: auth0Config.issuer_base_url!,
  clientId: auth0Config.client_id!,
  cacheLocation: 'localstorage',
  authorizationParams: {
    audience: auth0Config.audience,
    redirect_uri: auth0Config.base_url + paths.auth.auth0.callback,
  },
});

async function getToken() {
  return await auth0.getTokenSilently()
}

const token = getToken()

export const instance = axios.create({
  baseURL,
  headers: {
    Authorization: `Bearer ${token}`,
  }
});

我试过将示例封装在一个函数中,但这也不起作用,因为示例没有可用的方法,如.get,.push等。

ippsafx7

ippsafx71#

我认为问题在于你的getToken是一个await函数,应该用await来调用,所以consttoken = getToken()

const token = await getToken()

相关问题