问题
I'm trying to pass the role to user so that It can check if LoggedIn user is "Admin' or "mod" or only user. how can I pass role to user so that I can check user role after loggedIn? I'm using jwt token from backend for get/post. here is my code
class App extends Component {
constructor(props) {
super(props);
this.logOut = this.logOut.bind(this);
this.state = {
showModeratorBoard: false,
showAdminBoard: false,
currentUser: undefined
};
}
componentDidMount() {
const user = AuthService.getCurrentUser();
if (user) {
const { roles = '' } = user;
this.setState({
currentUser: AuthService.getCurrentUser(),
showModeratorBoard: roles.includes("admin"),
showAdminBoard: roles.includes("member")
});
}
}
logOut() {
AuthService.logout();
}
render() {
const { currentUser, showModeratorBoard, showAdminBoard } = this.state;
return (
<Router>
<div>
<nav className="navbar navbar-expand navbar-dark bg-dark">
<Link to={"/"} className="navbar-brand">
bezKoder
</Link>
<div className="navbar-nav mr-auto">
<li className="nav-item">
<Link to={"/home"} className="nav-link">
Home
</Link>
</li>
{showModeratorBoard && (
<li className="nav-item">
<Link to={"/mod"} className="nav-link">
Moderator Board
</Link>
</li>
)}
{showAdminBoard && (
<li className="nav-item">
<Link to={"/admin"} className="nav-link">
Admin Board
</Link>
</li>
)}
{currentUser && (
<li className="nav-item">
<Link to={"/user"} className="nav-link">
User
</Link>
</li>
)}
</div>
{currentUser ? (
<div className="navbar-nav ml-auto">
<li className="nav-item">
<Link to={"/profile"} className="nav-link">
{currentUser.username}
</Link>
</li>
<li className="nav-item">
<a href="/login" className="nav-link" onClick={this.logOut}>
LogOut
</a>
</li>
</div>
) : (
<div className="navbar-nav ml-auto">
<li className="nav-item">
<Link to={"/login"} className="nav-link">
Login
</Link>
</li>
<li className="nav-item">
<Link to={"/register"} className="nav-link">
Sign Up
</Link>
</li>
</div>
)}
</nav>
<div className="container mt-3">
<Switch>
<Route exact path={["/", "/home"]} component={Home} />
<Route exact path="/login" component={Login} />
<Route exact path="/register" component={Register} />
<Route exact path="/profile" component={Profile} />
<Route path="/user" component={BoardUser} />
<Route path="/mod" component={BoardModerator} />
<Route path="/admin" component={BoardAdmin} />
</Switch>
</div>
</div>
</Router>
);
}
}
export default App;
this the class where I have used Axios to get jwt from the backend:
import axios from "axios";
const API_URL = "https://localhost:44329/api/";
const Api="https://localhost:44329/api/users/";
class AuthService {
login(username, password) {
console.log(username);
return axios
.post(API_URL + "login", {
username,
password
})
.then(response => {
if (response.data.token) {
localStorage.setItem("user", JSON.stringify(response.data));
console.log(localStorage.getItem("user"));
}
});
}
logout() {
localStorage.removeItem("user");
}
register(username, email, password) {
return axios.post(Api + "register", {
username,
email,
password
});
}
getCurrentUser() {
return JSON.parse(localStorage.getItem('user'));;
}
}
export default new AuthService();
thank You!
来源:https://stackoverflow.com/questions/61252129/unable-define-role-in-reactjs-single-page-application