gpt4 book ai didi

react-router-dom - 在 react-admin 中添加无需身份验证的自定义页面

转载 作者:行者123 更新时间:2023-12-01 01:43:46 26 4
gpt4 key购买 nike

我想在我的 react-admin 中添加一个注册页面网络门户。由于react-admin不提供注册页面,我创建了一个自定义页面并将其添加到自定义路由中:

customRoutes.js

import React from 'react';
import { Route } from 'react-router-dom';
import SignupForm from './signupForm';

export default [
<Route path="/signup" component={SignupForm} noLayout/>,
];

问题是我只能在 /signup 打开页面当用户已经登录时。否则我会自动重定向到 /login路线。

如何禁用自定义路由的身份验证?是否有一些属性 <Route>接受或与 dataProvider.js 有关?

编辑 :

添加我的 signupForm.js的代表代码:
import React from 'react';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import { fetchUtils } from 'react-admin';
import { ApiUrl } from './config';

class SignupForm extends React.Component {
constructor() {
super();
this.state = {
fields: {
username: '',
password: ''
}
}


handleChange = name => event => {
let fields = this.state.fields;
fields[name] = event.target.value;

this.setState({
fields: fields,
});
};

handleSubmit = (event) => {
// Prevent default
event.preventDefault();

if (this.handleValidation()) {
let body = JSON.parse(JSON.stringify(this.state.fields));

let url = ApiUrl + '/api/user/create';
let options = {}
options.headers = new Headers({ Accept: 'application/json' });
options.method = 'POST'
options.body = JSON.stringify(body);
fetchUtils.fetchJson(url, options)
.then(data => {
alert(data.json.message);
this.props.history.push('/login')
})
.catch((err, ...rest) => {
console.log(err.status, err.message);
alert(err.message);
});
}
}

render() {
const { classes } = this.props;

const actions = [
<Button
type="submit"
label="Submit"
color='primary'
variant="flat"
>Submit</Button>,
];

return (
<Dialog
open={true}
style={{
textAlign: "center",
}}
onClose={this.handleClose}
classes={{ paper: classes.dialogPaper }}
>
<DialogTitle>Create an Account</DialogTitle>
<form className={classes.container} noValidate autoComplete="off" onSubmit={this.handleSubmit}>
<TextField
required
id="username"
label="Username"
value={this.state.fields["username"]}
onChange={this.handleChange('username')}
/>
<br />
<TextField
required
id="password"
label="Password"
value={this.state.fields["password"]}
onChange={this.handleChange('password')}
type="password"
/>

<div style={{ textAlign: 'right', padding: 40 }}>
{actions}
</div>
</form>
</Dialog>
);
}
}

export default withStyles(styles)(SignupForm);

最佳答案

问题是根据 /signup 的请求, react-admin调用authProvider类型为 AUTH_GET_PERMISSIONS其代码是:

if (type === AUTH_GET_PERMISSIONS) {
const role = localStorage.getItem('role');
return role ? Promise.resolve(role) : Promise.reject();
}

由于用户没有登录所以 localStorage.role从未初始化。

将其更改为:
if (type === AUTH_GET_PERMISSIONS) {
const role = localStorage.getItem('role');
return role ? Promise.resolve(role) : Promise.resolve('guest');
}

关于react-router-dom - 在 react-admin 中添加无需身份验证的自定义页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53470403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com