question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redirect URL in hybrid (cordova) app results in error

See original GitHub issue

Core Library

MSAL.js v2 (@azure/msal-browser)

Core Library Version

2.27.0

Wrapper Library

MSAL React (@azure/msal-react)

Wrapper Library Version

1.4.3

Description

I’m trying to leverage msal-react within a hybrid app using Cordova. Web is working great but I can’t figure out what redirectUri to specify to get it working in the hybrid app (web view). I’ve leveraged the react B2C sample, modified slightly to test this out in our hybrid app.

The issue I’m having is getting the hybrid app to recognize the redirectUri, which is specified in our App Registration under Single-Page Application Redirect URIs.

Error Message

Running the hybrid app locally with my Android device connected up to Chrome, I get a “Webpage not available” error with ERR_NAME_NOT_RESOLVED.

image

image

If I “refresh” the hybrid app in Dev Tools while on the error message screen the error goes away and my app refreshes and I’m logged in so it looks like msal-react is grabbing the hash from the location.path, just can’t figure out how to handle redirectUri to prevent the initial error.

Msal Logs

No response

MSAL Configuration

auth: {
    clientId: "our-client-id",
    authority:
      "https://our-custom-b2c.domain/our-tenantid.onmicrosoft.com/b2c_1a_our-policy",
    knownAuthorities: ["https://our-custom-b2c.domain"],
    redirectUri: "https://our-app/", // which is listed in our B2C app registration
    postLogoutRedirectUri: "https://our-app/logout",
    navigateToLoginRequestUrl: true
  },
  cache: {
    cacheLocation: "sessionStorage",
    storeAuthStateInCookie: false,
    secureCookies: true
  },
  system: {
    loggerOptions: {
      loggerCallback: (level, message, containsPii) => {
        if (containsPii) {
          return;
        }
        switch (level) {
          case LogLevel.Error:
            console.error(message);
            return;
          case LogLevel.Info:
            console.info(message);
            return;
          case LogLevel.Verbose:
            console.debug(message);
            return;
          case LogLevel.Warning:
            console.warn(message);
            return;
          default:
            return;
        }
      }
    }
  }

Relevant Code Snippets

import React from "react";
import { useMsal } from "@azure/msal-react";
import { loginRequest } from "../util/authConfig";

const LogInButton = () => {
  const { instance } = useMsal();

  const handleLogin = async () => {

    instance
      .loginRedirect(loginRequest)
      .then((response) => {
        console.log("login successful", response);
      })
      .catch((error) => {
        console.log(error);
      });
  };

  return (
    <React.Fragment>
      <button
        className="btn btn-primary"
        onClick={handleLogin}
        type="button"
      >
        Login Redirect
      </button>
    </React.Fragment>
  );
};

export default LogInButton;

Reproduction Steps

Leveraging the React B2C sample app:

  1. build app and run on android device

    `cordova run android --device`
    
  2. Once app opens on device, select Login Redirect button, which executes msalInstance.loginRedirect(msalConfig) image

  3. Sign in using my userid/password image

  4. B2C redirects back to app, which is when I get the error: ERR_NAME_NOT_RESOLVED

  5. Select Refresh in DevTools, note that the app indicates I’ve logged in. I can navigate to the Protected sample to get see the data image

Likewise, if I specify our “web” redirectUri for the hybrid apps, I’m able to login but I end up having to “refresh” or select the login button again for react to recognize that I’ve authenticated.

Expected Behavior

I would expect the hybrid app to handle the redirectUri like it does for the web browser.

Identity Provider

Azure B2C Custom Policy

Browsers Affected (Select all that apply)

Other

Regression

No response

Source

External (Customer)

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
GusLANcommented, Aug 2, 2022

@jasonnutter Same problem in android and iOS i can get this log in xCode -->

2022-08-02 11:49:54.796392+0200 demo-app[6468:453896] webView:didFailProvisionalNavigation - -1004: Could not connect to the server.

2022-08-02 11:50:21.057345+0200 demo-app[6468:453896] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service

To test just add this lines to config.xml file in cordova application and ensure that cordova-plugin-ionic-webview is installed and cordova-plugin-inappbrowser (cordova plugin list --> command to show installed plugins )

Error happen also using capacitor not only Cordova.

config.xml code -->

<preference name="UseScheme" value="true" />
 <preference name="Scheme" value="https" />
 <preference name="HostName" value="mycoolApp" />
 <preference name="iosScheme" value="httpsionic" />

Probably the error is when this library pick the location origin from window.location.origin. In mobile hybrid apps location origin can be fileSystem

Same error

2reactions
phyr0scommented, Aug 2, 2022

@jasonnutter Same problem in android and iOS i can get this log in xCode -->

2022-08-02 11:49:54.796392+0200 demo-app[6468:453896] webView:didFailProvisionalNavigation - -1004: Could not connect to the server.

2022-08-02 11:50:21.057345+0200 demo-app[6468:453896] Could not signal service com.apple.WebKit.WebContent: 113: Could not find specified service

To test just add this lines to config.xml file in cordova application and ensure that cordova-plugin-ionic-webview is installed and cordova-plugin-inappbrowser (cordova plugin list --> command to show installed plugins )

Error happen also using capacitor not only Cordova.

config.xml code -->

<preference name="UseScheme" value="true" />
 <preference name="Scheme" value="https" />
 <preference name="HostName" value="mycoolApp" />
 <preference name="iosScheme" value="httpsionic" />

Probably the error is when this library pick the location origin from window.location.origin. In mobile hybrid apps location origin can be fileSystem

Read more comments on GitHub >

github_iconTop Results From Across the Web

ios cordova app redirect and opens in browser - Stack Overflow
The problem I'm having is in the cordova index.html page I have javascript trying to redirect to the new page. After I have...
Read more >
Kofax PhoneGap Plugin - Developer's Guide - Version: 3.5.0
A sample PhoneGap application, demonstrating the plugin, is available in the \Hybrid\PhoneGap. \Samples\Sdksample folder. The sample application can be used ...
Read more >
Not redirecting to Salesforce login page while opening the app
When I re-built it, there was no error or warnings but I still see the empty page. I am not sure why this...
Read more >
iOS 14's Intelligent Tracking Prevention is Considered Harmful
When a hybrid app uses the Implicit Flow, it redirects to an IDP where the login happens and gets an access token back...
Read more >
Cordova plugin for Amazon Catalog Ingestion (Fire TV)
Make sure you run the cordova platform add android command from your Cordova project directory. (If you get an error that says, "Error:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found