小编Wou*_*een的帖子

使用 Azure AD 的 NextAuth 刷新令牌

由于访问令牌的默认有效时间为 1 小时,因此我尝试让刷新令牌在我的应用程序中工作。我已经被这个问题困扰了几个星期,而且似乎无法解决它。我已经验证了我的refreshAccessToken(accessToken)函数可以工作(其中accessToken有一个带有过期令牌、刷新令牌和其他一些东西的对象)。

我将问题定位到了该async session()函数。虽然在async jwt()函数中调用了refreshAccessToken,但该async session()函数仍然会导致错误,因为参数token未定义。这会导致错误cannot read property accessToken from undefined(因为token.accessToken位于第一行)。我怎样才能解决这个问题,并以某种方式让函数async session()等待访问令牌刷新,然后将其与所有其他信息(组、用户名等)一起发送到客户端?

/**
 * All requests to /api/auth/* (signIn, callback, signOut, etc.) will automatically be handled by NextAuth.js.
 */

import NextAuth from "next-auth"
import AzureAD from "next-auth/providers/azure-ad";

async function refreshAccessToken(accessToken) {
  try {
    const url = "https://login.microsoftonline.com/02cd5db4-6c31-4cb1-881d-2c79631437e8/oauth2/v2.0/token"
    await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/x-www-form-urlencoded",
      },
      body: `grant_type=refresh_token`
      + …
Run Code Online (Sandbox Code Playgroud)

azure azure-active-directory next.js next-auth

5
推荐指数
1
解决办法
2493
查看次数