next.js 在下一次登入回呼时存取'req'

t2a7ltrp  于 2022-11-23  发布在  其他
关注(0)|答案(1)|浏览(145)

是否可以在登录回调期间访问req参数?我需要在登录尝试期间检查并记录用户的ip。
我已经遵循了高级初始化过程,但是,我似乎无法在登录回调期间访问会话。
从文档:

callbacks: {
  async signIn({ user, account, profile, email, credentials }) {
    const isAllowedToSignIn = true
    if (isAllowedToSignIn) {
      return true
    } else {
      // Return false to display a default error message
      return false
      // Or you can return a URL to redirect to:
      // return '/unauthorized'
    }
  }
}

我已经添加了这样的ip

export default async function auth(req: NextApiRequest, res: NextApiResponse) {
  
  const ip= req.socket.remoteAddress

  return await NextAuth(req, res, {
    ...
    callbacks: {
      session({ session}) {

        session.ip= ip
        return session
      }
    }
  })
}
00jrzges

00jrzges1#

req参数未传递给signIn回调。这是signIn的类型脚本签名。来自npm存储库:

signIn: (params: {
    user: User | AdapterUser;
    account: A | null;
    /**
     * If OAuth provider is used, it contains the full
     * OAuth profile returned by your provider.
     */
    profile?: P;
    /**
     * If Email provider is used, on the first call, it contains a
     * `verificationRequest: true` property to indicate it is being triggered in the verification request flow.
     * When the callback is invoked after a user has clicked on a sign in link,
     * this property will not be present. You can check for the `verificationRequest` property
     * to avoid sending emails to addresses or domains on a blocklist or to only explicitly generate them
     * for email address in an allow list.
     */
    email?: {
        verificationRequest?: boolean;
    };
    /** If Credentials provider is used, it contains the user credentials */
    credentials?: Record<string, CredentialInput>;
}) => Awaitable<string | boolean>;

相关问题