AzureAD / passport-azure-ad

The code for Passport Azure AD has been moved to the MSAL.js repo. Please open any issues or PRs at the link below.
https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/maintenance/passport-azure-ad
Other
420 stars 177 forks source link

Dynamic redirectUrl for two different domains #516

Closed Mahan-F closed 3 years ago

Mahan-F commented 4 years ago

I have already looked at #308 #487 and #498 and i cant seem to find a solution to use a dynamic redirectUrl.

I have setup my AAD application to support redirect URL for two different domains.

https://domainOne.com/auth/openid/return https://domainTwo.com/auth/openid/return

Both these domains are pointing to the same server and same Node.js web application.

I need to be able to use the req (Express request object) to see the Host by req.get('Host') and then from there change the redirectUrl of passport accordingly so that users authenticating with domainOne are returned to domainOne.com domain and the same for domainTwo users.

Looking at the other issues I linked above, it seems like others are having the same issue but I didn't manage to find a solid working answers. Is this something that's possible?

nino-vrijman commented 3 years ago

It should be possible to set the redirect URL based on some data from the incoming request by doing something like this. I realize this is really similar the answer in #308 and as far as I can see it should work from version 3.0.5 and up. Make sure you're actually using redirect_uri, something different wouldn't work if I see these lines. I successfully implemented this solution in a Nest.js project.

app.get('/login', 
  (req, res, next) =>
    passport.authenticate('azure-ad', {
      extraAuthReqQueryParams: {
        redirect_uri: `https://${req.headers.host}/login/callback`,
      },
  })(req, res, next)
);
sameerag commented 3 years ago

Closing as #308 seems to resolve this issue. Please raise a new issue in our mono repo if the given solution isn't feasible for any of your use cases.

zettry1 commented 3 years ago

@nino-vrijman Hello, I am using this solution and do need to set redirect url at callback request ? router.post( "/openid/return", (req, res, next) => { passport.authenticate("azuread-openidconnect", { response: res, // required failureRedirect: "/auth/reset", extraTokenReqQueryParams: { redirect_uri:https://${req.headers.host}/openid/return, }, })(req, res, next); }, (req, res) => { res.status(200).redirect("/menu"); } );