My login application (react) and actual (react) application are in the same domain. My login application sets localStorage when user logs in successfully. In my actual application i have to check that value . if it is not there i have to redirect to login application in onload . What is the best practice to do that
Asked
Active
Viewed 542 times
0
-
Possible Duplicate of [Programmatically Navigate using react-router](https://stackoverflow.com/questions/44127739/programmatically-navigate-using-react-router/44128108#44128108) – Shubham Khatri May 08 '18 at 07:44
-
i can not use mentioned solution because my login and actual application are different. – Lakshmi Prasanna May 08 '18 at 07:48
-
In that case you cannot use Router, you need to use window.location – Shubham Khatri May 08 '18 at 07:50
1 Answers
0
You can check it in the componentDidMount
life cycle method as below.
class LoggedInComponent extends Component {
componentDidMount() {
if (!localStorage.get('loggedIn')) {
window.location = "/login";
// You mentioned login app and actual app is deferent.
// So you should use window.location
// Otherwise you can use react-router's router.push('/login') function.
}
}
}
Use componentDidMount
only. Because react v17
is going to deprecate the componentWillMount
function.

Akhil P
- 1,580
- 2
- 20
- 29
-
thank you for the response. Can' we do in the router part..? i am just checking. Because i am just thinking about user experience while switching – Lakshmi Prasanna May 08 '18 at 07:37
-
No you cannot do it with the router if both applications are different. – Akhil P May 08 '18 at 07:40