我不知道如何在fetcher中的API调用中添加不记名令牌头来接收数据,因为我在后端检查JWT令牌……
抓取器代码:
const fetcher = async () => {
const response = await fetch(api); //I need to add bearer token headers to this api request or else it's 401
const data = await response.json();
const props = {
data: data
};
return props;
};这是数据获取代码
const {data, error} = useSWR("data", fetcher);
if (error) return <div>oops... {error.message}</div>;
if (data === undefined) return <div>Loading...</div>;
console.log(accessToken);
return <div>{data}</div>;我在一篇文章中发现了一段代码,其中说我需要添加以下内容:
const {accessToken} = getAccessToken({
scopes: ["read:shows"], // I don't know what is this scope I just pasted it...
});但是accessToken返回的错误是它只能在服务器端被检索到...
发布于 2021-11-12 20:04:46
您可以使用任何授权流来获取持有者令牌
发布于 2021-11-18 14:22:02
安装auth0SDK,如果您想要解码jwt,还需要安装jwt decode lib。
import { getSession, } from '@auth0/nextjs-auth0';
const jwt = require('jsonwebtoken');
...
//all user data here, with tokens
const session = getSession(req,res)
//bearer token here
const token = session.idToken
//simple token here
const tokenId = session.accessToken,此外,在这个auth0SDK中,您可以使用很多钩子/函数!
https://stackoverflow.com/questions/69926666
复制相似问题