Reputation: 396
Hi I am using react router v4.
My React runs on port 3000, while express server runs on port 8080. The Redirect segment of the code below brings me to localhost:3000/http://localhost:8080/auth/login
.
I have "proxy": "localhost:8080"
in my React's package.json already. It doesn't work.
How can I redirect from localhost:3000
to localhost:8080/auth/login
?
App.js
class App extends Component {
constructor(props) {
super(props);
this.props.fetchProducts();
this.props.fetchUser();
}
render() {
return (
<Switch>
<Route exact path='/cart' render={() => this.props.isLoggedIn ?
<Checkout /> :
<Redirect to='http://localhost:8080/auth/login' />
} />
<Route exact path='/user/account/profile' render={() => <Profile />} />
<Route exact path='/' render={() => <MainPage />} />
</Switch>
);
}
}
Client's package.json
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@material-ui/core": "^3.6.2",
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"react-scripts": "^2.1.3",
"redux": "^4.0.0",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.3.0"
},
"proxy": "http://localhost:8080",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Upvotes: 5
Views: 11376
Reputation: 1007
i think if your express server run in port 8080 than react router doesn't redirect you to "http://localhost:8080/auth/login" because it's an API that will authenticate you username/password credentials.
you can use fetch/axios to call your login api.
i think you are checking if user is already logged in before than redirect to login but i don't think you need to redirect to login page if user is already log in.
Upvotes: 0
Reputation: 396
Since I couldn't use react-router's Redirect to link to /auth/login
, I changed window.location.href
in a method and returned null.
class App extends Component {
constructor(props) {
super(props);
this.props.fetchProducts();
this.props.fetchUser();
}
redirect = () => {
window.location.href = 'http://localhost:8080/auth/login';
// maybe can add spinner while loading
return null;
}
render() {
return (
<Switch>
<Route exact path='/cart' render={() =>
this.props.isLoggedIn ?
<Checkout /> :
this.redirect()
} />
<Route exact path='/user/account/profile' render={() => <Profile />} />
<Route exact path='/' render={() => <MainPage />} />
</Switch>
);
}
}
Upvotes: 1
Reputation: 619
You can just add proxy in your package.json file of reactjs.
"proxy": "http://localhost:8080"
This will connect with nodejs server that is started on port 8080.
You should call api from client side using url
axios.post('/api/auth', {credentials}).then(res => res.data.user)
and the same time you have configure routes in nodejs using routes if you are using express
This should work.
Upvotes: 0
Reputation: 484
Try adding this line to your package.json in the client folder (where the react files are).
"proxy": {
"/api/*": {
"target": "http://localhost:8080"
}
},
This will redirect your requests to the express server. Also change the Redirect to Redirect to /auth/login
, you don't need to specify the full url.
Upvotes: 0