javascript - 使用 auth0 进行身份验证
问题描述
我正在为我的 reactjs 应用程序实施 auth0 授权
我已经创建了我的 auth0 帐户,并在我的 Auth.js 文件中传递了参数
我的 Index.js 和 main.js 代码如下。
我面临的问题是我无法获得this.props.auth
价值。它在按钮中的 onClick 事件中引发错误以调用 Auth0:
TypeError:无法读取未定义的属性“登录”。
import auth0 from 'auth0-js';
export default class Auth {
auth0 = new auth0.WebAuth({
domain:"xxxxx.auth0.com",
clientID:"xxxxxxxxxxxx",
redirectUri:"http://localhost:3000/Callback",
audience:"https://xxxx.auth0.com/userinfo",
responseType:"token id_token",
scope:"openid"
});
constructor(){
this.login = this.login.bind(this);
}
login () {
this.auth0.authorize();
}
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { BrowserRouter as Router } from "react-router-dom";
import Auth from './Auth';
const auth= new Auth();
let state = {};
window.setState=(changes)=>{
state = Object.assign({},state,changes);
};
/* eslint no-restricted-globals : 0 */
let initialState = {
name :"user1",
location :location.pathname.replace(/^\/?|\/$/g,""),
auth
};
ReactDOM.render(
<Router {...state}>
<App />
</Router>,
document.getElementById("root")
);
window.setState(initialState);
serviceWorker.unregister();
main.js
import React, { Component } from "react";
export default class Main extends Component{
render(){
return(
<div>
<div>
<h1> Please login here user </h1>
<button onClick={this.props.auth.login}>Login</button>
</div>
</div>
)
}
}
解决方案
我没有看到您将该道具传递给 Main 组件。
你应该做的是
import Auth from './Auth';
const auth= new Auth();
export default class Main extends Component{
render(){
return(
<div>
<div>
<h1> Please login here user </h1>
<button onClick={auth.login}>Login</button>
</div>
</div>
)
}
}
推荐阅读
- google-bigquery - Google Ads BigQuery 数据传输 - 自今年年初以来,转化统计信息表中缺少一些子帐户
- sql - 如何组合 2 个查询或使用连接创建相同的查询
- parcel - 导入非捆绑资源
- java - Visual Studio Code 中是否有将您的 java 项目转换为可执行 jar 文件的命令?
- java - 尝试在 JSP 中发送电子邮件时出现 SMTPSendFailedException
- swift - swift:通用风格的查找功能
- substrate - Substrate 3.0“on_runtime_upgrade”功能不起作用
- javascript - 在页面加载时运行外部 javascript(“驻留在”一个跨度)
- sql-server - 带有连字符的 Python 摄取表
- laravel - laravel 8 中的认证路由