Home > Mobile >  how to redirect to another app after login?
how to redirect to another app after login?

Time:04-05

I have a React js application "login", which makes a request to the backend and gets the jwt. I need that after the authentication is done, the "login" application redirects to another react application (hosted on another server) which needs to use the jwt

CodePudding user response:

You can redirect a user to another page via the res.redirect method of ExpressJS.

So for example after a user logged in to your backend, you can do something like this:

app.all('/login', (req, res) => {
    let isAuthenticated = tryAuthenticate(req);
    
    if (isAuthenticated)
        return res.redirect('/anotherApplicationOnAnotherServer');
    
    else
        return res.redirect('/login');
});

CodePudding user response:

Framework-agnostic way

Whenever you get your jwt from the backend you call this to redirect:

window.location.href = `https://path-to-another-app?token=${jwt}`;

Then, at the target app you can get your token like this:

const url = new URL(window.location.href);
const jwt = url.searchParams.get("token");
  • Related