Redirect After Login

Hi, first post here…I’ve got some functionality in place that logs in the user and logs the user out. I’m stuck on finding a way to redirect the user after logging in and logging out. Is there a way to not use react-router? Appreciate any direction on it. Thanks!

Hi @justin

Welcome to the community.

Redirects are easy to do with plain JavaScript - you can use window.location.href to redirect to another webpage and you can call that if a condition that you specify applies. For example:

let userIsLoggedIn = true;

if (userIsLoggedIn) {
   window.location.href = "https://mysite.com/logged-in-page/";
}

See: https://www.w3schools.com/howto/howto_js_redirect_webpage.asp

Hi @justin

I realised that my earlier answer would force a page reload, which is probably not what you want.

With Frontity you can use actions.router.set.

See: https://docs.frontity.org/api-reference-1/router

Hi @mburridge,

Appreciate confirming that. Works well.

In case others stumble on this thread, I simply implemented it like this:

const Login = ({ state, actions }) => {
... // Fetch logic here and before catch
actions.router.set('/'); // To go back home after logging in.
}.catch...
3 Likes

window.location.replace('http://w3c.com');

It’s better than using window.location.href = ‘http://w3c.com’;

Using replace() is better for javascript redirect , because it does not keep the originating page in the session history, meaning the user won’t get stuck in a never-ending back-button fiasco.

Hi,
I wanted to call this actions.router.set(’/’); in a class function e.g) GetLoginDetails=()=>{}
so any idea on how to go through it

Thanks