ruby-on-rails - 使用 Auth0 对令牌进行身份验证后如何检索用户信息
问题描述
我在用户登录网站时使用 Auth0 对用户进行身份验证,我使用 rails 作为后端,而对于前端,我使用 React。我可以从前端获取令牌并将其发送到后端以对用户进行身份验证,但我还无法提取用户信息。这是我正在使用向我的后端发出请求的代码
const callProtectedApi = async () => {
try {
const token = await getAccessTokenSilently();
console.log(token)
const response = await axios.get('http://localhost:5000/private', {
headers: {
authorization: `Bearer ${token}`,
},
});
console.log(response.data);
} catch (error) {
console.log(error.messsage);
} };
这是我在rails后端创建的方法
class PrivateController < ActionController::API
def index
require 'faraday'
url = 'https://dev-v88tfgqc.us.auth0.com/userinfo'
access_token = request.headers['authorization'].split(' ').last
puts access_token
response = Faraday.get(url, { 'authorization' => 'Bearer' + access_token })
p response.status
p response.body
response = Faraday.get(url,
{ 'authorization' => 'Bearer' + access_token })
end
end
我可以在 Rails 中的服务器中打印令牌,但我无法从用户那里检索信息,因为我得到代码 401 并且未经授权,但这就是我没有得到的,因为我能够打印令牌,所以为什么我没有被授权?
解决方案
推荐阅读
- c# - 带有 id 的 ASP.NET WebAPI 2 版本控制 POST
- c# - 在 C# .NET Core 中检查文件权限
- xcode - 如何快速访问其他类中的变量?
- mongodb - mongodb - 集合的唯一字段覆盖文档
- python - Python Selenium:如何设置基于 DIV 的编辑器的文本?
- html - 如何修改包含在角度变量中并以 html 显示的字符串
- azure-data-factory - 长时间排队的复制活动
- c++ - 从照片中计算 3D 对象位置
- loops - 在嵌套的 for 循环中遍历特定的名称组合
- reactjs - 如何在打字稿中构建函数回调?