简体   繁体   English

如何在 ReactJS 中添加角色?

[英]How to add roles in ReactJS?

I want to add roles to admin when login so admin can create new user.我想在登录时向管理员添加角色,以便管理员可以创建新用户。 I dont know how to add the roles and make it works.我不知道如何添加角色并使其工作。 Here's my code:这是我的代码:

Login.js登录.js

 class Login extends Component { constructor(props) { super(props); this.state = { username: "", password: "", } } onChangeInput = e => { this.setState({ [e.target.name]: e.target.value }) } onLogin = async () => { const { username, password } = this.state const exist = this.props.listUsers.find(user => user.username === username) if (exist) { this.props.changeLogIn() } else if(username === 'admin' && password === "123") { const roles = "ADMIN"; this.props.changeLogIn() } } render() { if (this.props.statusLogin){ return <Redirect to="/about" /> } return ( <div className="login"> <form className="login-form" method="POST"> <div className="container-log"> <h1 className="judul">Login</h1> <RowInput value={this.state.username} label="Username" placeholder="Username" type="text" name="username" onChange={this.onChangeInput}/> <RowInput value={this.state.password} label="Password" placeholder="Password" type="password" name="password" onChange={this.onChangeInput}/> <Button onClickInput={this.onLogin}>Masuk</Button> </div> </form> </div> ); } }

Body.js正文.js

 showPage = () => { const { changeLogIn, statusLogin } = this.props return ( <Switch> <Route path="/about" children={(props) => <About {...props} statusLogin={statusLogin} listUsers={this.state.userData} />} /> <Route path="/login"> <Login changeLogIn={changeLogIn} listUsers={this.state.userData} statusLogin={statusLogin} /> </Route> <Route path="/register"> <Register listUsers={this.state.userData} tambahUser={this.addUsers} /> </Route> </Switch> ) }

App.js应用程序.js

 class App extends Component { constructor(props) { super(props); this.state = { page: "login", isLoggedIn: false } } changeLogIn = () => { this.setState(oldState => ({ isLoggedIn: !oldState.isLoggedIn })) }

Yes, I know this code wouldn't work (esp Login.js), but again, I dont know how to fix it.是的,我知道这段代码不起作用(尤其是 Login.js),但同样,我不知道如何修复它。 Like, somewhat I have the solutions on my head, but I can't code it.就像,我脑子里有一些解决方案,但我无法编码。 Thank you before!先谢谢了!

Admin , user roles is something that we should do it in the backend but if you are trying to do it in front end then , There should be one super admin (he an add new admins or user ) , admins (he can add users)管理员,用户角色是我们应该在后端做的事情,但是如果您尝试在前端做,那么应该有一个超级管理员(他是添加新管理员或用户),管理员(他可以添加用户)

store the login details in cookie or localstorage , and redirect the user if he is not authorized , I can code if you ask me something specific将登录详细信息存储在 cookie 或 localstorage 中,如果用户未经授权,则重定向用户,如果您问我一些具体问题,我可以编码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM