小编Ale*_*pez的帖子

如何在动态路由中使用 getStaticPaths 和 getStaticProps 获取下一个身份验证会话?

我正在尝试在 NextJS 应用程序中实现动态路由。我需要来自 next-auth 的令牌来通过对 API 的 axios 请求从 getReport 和 getReports (在 /reports.js 中)获取数据,但我不知道如何获取它。当我使用 [itemId].js 中硬编码的令牌尝试它时,它起作用了,因此路由工作正常(我认为)。

由于 getStaticPaths 和 getStaticProps 在服务器上运行,我尝试使用 getSession() ,但我不确定它是否不起作用或者我没有正确使用它,但我永远无法获取会话。使用 getSession() 对我来说是更好的选择。

我也尝试将其保存在 localStorage 中,但 getStaticProps 无法访问 localStorage。

我正在考虑使用 Redux,但我真的想避免它。

这是我的代码:

/pages/user/reports/[reportId]/[itemId].js

import { useSession } from "next-auth/react";
import { useLayoutEffect } from "react";
import Loading from "../../../../components/Loading";
import Layout from "../../../../components/user/layout/Layout";
import { getAllReportsIds, getReportItem } from "../../../../components/user/reports";

const token = 'here is currently the token'

export default function Report({ reportItem }) {
  return ( …
Run Code Online (Sandbox Code Playgroud)

javascript routes reactjs next.js next-auth

6
推荐指数
1
解决办法
5686
查看次数

标签 统计

javascript ×1

next-auth ×1

next.js ×1

reactjs ×1

routes ×1