0

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>
        );
    }
}

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.