Reputation: 7656
I am converting from C# views to using react. I want to render some html depending on whether the user is authenticated or not, in C# this was as easy as this:
<ul class="outer pull-right">
@if (User.Identity.IsAuthenticated)
{
<li>
<form action="logout" id="logout-form">
<input type="submit" value="Logout" />
</form>
</li>
}
</ul>
What is the correct way to do this in react.js? Pseudocode For example:
export default class Nav extends React.Component {
getCurrentUser(){
//Use ajax to get current user from server?
}
render(){
return (
<ul class="outer pull-right">
@if (User.Identity.IsAuthenticated)
{
<li>
<form action="logout" id="logout-form">
<input type="submit" value="Logout" />
</form>
</li>
}
</ul>
);
}
}
Upvotes: 0
Views: 882
Reputation: 3902
If you're going to use c# sessions in your react application, you'll need an API call. For obvious reasons, you'll be using react-router. And for that, people generally adopt this method to authenticate user.
Agreed, that it is not that easy to authenticate the user. But once you've set up the mechanism, you won't need to look back again.
Good luck!
Upvotes: 1