React Private Route Redirect not working with Asp.net core
up vote
0
down vote
favorite
I have created one website with Asp.Net Core and typescript/webpack/react in vs 2017.
Basically, I have 3 pages: Home Index Page, Login Page and Dashboard in my website. Home page is the main page to render react app.
If user logs in, the Dashboard component will be rendered and if not then the login component will be rendered in Home Index view.
Below is my main app that list out routes:
export class App extends React.Component<{}, {}> {
constructor(props) {
super(props);
}
public render() {
return (
<div >
<Route exact path="/" component={Login} />
<Route exact path="/login" component={Login} />
<Layout>
<PrivateRoute exact path="/dashboard" component={Dashboard} />
</Layout>
</div>
);
}
}
and PrivateRoute is like:
export const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
localStorage.getItem('user') ?
<Component {...props} /> :
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}} />
)}/>
)
This is working like if not logged in, then it goes back to login component (inside Home Index) url. But issue is, if I just refresh that page from browser (localhost:1234/login), then it tries to find this /login
in MVC Routing so it gives me error like below:
If I remove Redirect from PrivateRoute then it works properly. So I think the issue is with MVC routing takes place for redirect.
reactjs asp.net-core webpack react-router
New contributor
add a comment |
up vote
0
down vote
favorite
I have created one website with Asp.Net Core and typescript/webpack/react in vs 2017.
Basically, I have 3 pages: Home Index Page, Login Page and Dashboard in my website. Home page is the main page to render react app.
If user logs in, the Dashboard component will be rendered and if not then the login component will be rendered in Home Index view.
Below is my main app that list out routes:
export class App extends React.Component<{}, {}> {
constructor(props) {
super(props);
}
public render() {
return (
<div >
<Route exact path="/" component={Login} />
<Route exact path="/login" component={Login} />
<Layout>
<PrivateRoute exact path="/dashboard" component={Dashboard} />
</Layout>
</div>
);
}
}
and PrivateRoute is like:
export const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
localStorage.getItem('user') ?
<Component {...props} /> :
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}} />
)}/>
)
This is working like if not logged in, then it goes back to login component (inside Home Index) url. But issue is, if I just refresh that page from browser (localhost:1234/login), then it tries to find this /login
in MVC Routing so it gives me error like below:
If I remove Redirect from PrivateRoute then it works properly. So I think the issue is with MVC routing takes place for redirect.
reactjs asp.net-core webpack react-router
New contributor
Share us yourstartup.cs
. Is there any demo to reproduce your issue? I try to make a test withPrivateRoute
, fail to reproduce your issue, it does not look for theIndex
page.
– Tao Zhou
Nov 22 at 6:42
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have created one website with Asp.Net Core and typescript/webpack/react in vs 2017.
Basically, I have 3 pages: Home Index Page, Login Page and Dashboard in my website. Home page is the main page to render react app.
If user logs in, the Dashboard component will be rendered and if not then the login component will be rendered in Home Index view.
Below is my main app that list out routes:
export class App extends React.Component<{}, {}> {
constructor(props) {
super(props);
}
public render() {
return (
<div >
<Route exact path="/" component={Login} />
<Route exact path="/login" component={Login} />
<Layout>
<PrivateRoute exact path="/dashboard" component={Dashboard} />
</Layout>
</div>
);
}
}
and PrivateRoute is like:
export const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
localStorage.getItem('user') ?
<Component {...props} /> :
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}} />
)}/>
)
This is working like if not logged in, then it goes back to login component (inside Home Index) url. But issue is, if I just refresh that page from browser (localhost:1234/login), then it tries to find this /login
in MVC Routing so it gives me error like below:
If I remove Redirect from PrivateRoute then it works properly. So I think the issue is with MVC routing takes place for redirect.
reactjs asp.net-core webpack react-router
New contributor
I have created one website with Asp.Net Core and typescript/webpack/react in vs 2017.
Basically, I have 3 pages: Home Index Page, Login Page and Dashboard in my website. Home page is the main page to render react app.
If user logs in, the Dashboard component will be rendered and if not then the login component will be rendered in Home Index view.
Below is my main app that list out routes:
export class App extends React.Component<{}, {}> {
constructor(props) {
super(props);
}
public render() {
return (
<div >
<Route exact path="/" component={Login} />
<Route exact path="/login" component={Login} />
<Layout>
<PrivateRoute exact path="/dashboard" component={Dashboard} />
</Layout>
</div>
);
}
}
and PrivateRoute is like:
export const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={props => (
localStorage.getItem('user') ?
<Component {...props} /> :
<Redirect to={{
pathname: '/login',
state: { from: props.location }
}} />
)}/>
)
This is working like if not logged in, then it goes back to login component (inside Home Index) url. But issue is, if I just refresh that page from browser (localhost:1234/login), then it tries to find this /login
in MVC Routing so it gives me error like below:
If I remove Redirect from PrivateRoute then it works properly. So I think the issue is with MVC routing takes place for redirect.
reactjs asp.net-core webpack react-router
reactjs asp.net-core webpack react-router
New contributor
New contributor
edited Nov 22 at 5:09
Tân Nguyễn
1
1
New contributor
asked Nov 21 at 19:52
ldev
11
11
New contributor
New contributor
Share us yourstartup.cs
. Is there any demo to reproduce your issue? I try to make a test withPrivateRoute
, fail to reproduce your issue, it does not look for theIndex
page.
– Tao Zhou
Nov 22 at 6:42
add a comment |
Share us yourstartup.cs
. Is there any demo to reproduce your issue? I try to make a test withPrivateRoute
, fail to reproduce your issue, it does not look for theIndex
page.
– Tao Zhou
Nov 22 at 6:42
Share us your
startup.cs
. Is there any demo to reproduce your issue? I try to make a test with PrivateRoute
, fail to reproduce your issue, it does not look for the Index
page.– Tao Zhou
Nov 22 at 6:42
Share us your
startup.cs
. Is there any demo to reproduce your issue? I try to make a test with PrivateRoute
, fail to reproduce your issue, it does not look for the Index
page.– Tao Zhou
Nov 22 at 6:42
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
ldev is a new contributor. Be nice, and check out our Code of Conduct.
ldev is a new contributor. Be nice, and check out our Code of Conduct.
ldev is a new contributor. Be nice, and check out our Code of Conduct.
ldev is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419578%2freact-private-route-redirect-not-working-with-asp-net-core%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Share us your
startup.cs
. Is there any demo to reproduce your issue? I try to make a test withPrivateRoute
, fail to reproduce your issue, it does not look for theIndex
page.– Tao Zhou
Nov 22 at 6:42