# Internet Identity pop-up blocked on Safari

**URL:** <https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105>\
**Category:** internet-identity\
**Created:** [August 12, 2024, 12:03pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105 "2024-08-12T12:03:39Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mysticx](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mysticx](https://forum.dfinity.org/u/Mysticx)\
**Post date:** [August 12, 2024, 12:03pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/1 "2024-08-12T12:03:39Z")

</div>

Hello, I am trying to connect users to my web app completely hosted on the IC with II which is working fine on chrome but safari is blocking the pop-up, I guess because client.login is an async call so I’m calling it inside an async function which safari doesn’t allows (?). but I’ve checked some other apps such as [http://nns.ic0.app](http://nns.ic0.app) and open chat which seems to work fine (even though they are also opening in a different tab) any clue on how I could go about fixing this?.  
Any help is appreciated.  
P.S I also have NFID integrated and it seems to be working just fine

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 12, 2024, 12:20pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/2 "2024-08-12T12:20:35Z")

</div>

If I’m not wrong, Safari doesn’t have any issues with opening popups using async code. However, it does require user interaction to do so. Is your login triggered by a button with a `click` event?

---

<div class="post-metadata">

**Author:** ![Mysticx](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mysticx](https://forum.dfinity.org/u/Mysticx)\
**Post date:** [August 12, 2024, 1:05pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/3 "2024-08-12T13:05:58Z")

</div>

yes, though the button is inside of a modal.

---

<div class="post-metadata">

**Author:** ![Mysticx](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mysticx](https://forum.dfinity.org/u/Mysticx)\
**Post date:** [August 12, 2024, 1:14pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/4 "2024-08-12T13:14:04Z")

</div>

I would like to mention that auth.login isn’t called directly inside the `click` event (it is nested inside).  
this is the code snippet:

```js
  const methods = authMethods({
    useAuthStore,
    setIsLoading,
    handleClose,
  });
  async function handleLogin(type: LoginEnum) {
    setSelected(type);
    methods.login(type, navigation, callBackfn);
  }

```

The login function:

```js
const login = async (
    type: LoginEnum,
    navigation: any,
    callBackfn: () => void,
  ) => {
    let ran = false;
    if (
      auth &&
      auth.state === 'anonymous' &&
      handleClose &&
      setIsLoading
    ) {
      setIsLoading(true);
      if (type == LoginEnum.InternetIdentity) {
        const client = await AuthClient.create({
          idleOptions: {
            idleTimeout: (1000 * 60 * 60) / 2,
          },
        });
        await client?.login({
          maxTimeToLive: BigInt(1800) * BigInt(1_000_000_000),
          // windowOpenerFeatures:
          // 'toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100',
          identityProvider:
            process.env.NEXT_PUBLIC_DFX_NETWORK === 'ic'
              ? 'https://identity.ic0.app/#authorize'
              : `http://${process.env.CANISTER_ID_INTERNET_IDENTITY}.localhost:4943/#authorize`,
          onSuccess: () => {
            authenticate(client as AuthClient).then(() => {
              const path = window?.location;
              if (path?.pathname == '/') navigation.push(DASHBOARD_ROUTE);

              if (callBackfn) {
                callBackfn();
              }
            });
          },
          onError: () => {
            handleClose();
          },
        });
      } else if (type == LoginEnum.NFID) {
        setIsLoading(true);

        try {
          const nfid = await NFID.init({
            application: {
              name: appData.name,
              logo: appData.logo,
            },
            idleOptions: {
              idleTimeout: 45 * 60 * 1000, 
              captureScroll: true, 
            },
          });
          const delegationIdentity: Identity = await nfid.getDelegation({
    
            maxTimeToLive: BigInt(2) * BigInt(3_600_000_000_000),
          });
          authenticate(undefined, delegationIdentity).then(() => {
            const path = window?.location;
            if (path?.pathname == '/') path.assign(DASHBOARD_ROUTE);
          });
        } catch (error) {
          setIsLoading(false);
        }
      } else if (auth && !ran && auth.state === 'anonymous') {
        initAuth();
        ran = true;
      } 
    }
  };

```

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 12, 2024, 2:12pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/5 "2024-08-12T14:12:08Z")

</div>

> [@Mysticx](#):
>
> I would like to mention that auth.login isn’t called directly inside the `click` event

Does your button calls `handleLogin`?

> [@Mysticx](#):
>
> `methods.login(type, navigation, callBackfn);`

If for testing purpose you replace this code by a call to the login `(await AuthClient.create(...)).login()`, does the popup open?

A bit hard to tell based on the snippet. Can you share a link to the repo?

---

<div class="post-metadata">

**Author:** ![Mysticx](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mysticx](https://forum.dfinity.org/u/Mysticx)\
**Post date:** [August 12, 2024, 2:16pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/6 "2024-08-12T14:16:43Z")

</div>

> [@peterparker](#):
>
> Does your button calls `handleLogin`?

yes it calls `handleLogin`.

> [@peterparker](#):
>
> If for testing purpose you replace this code by a call to the login `(await AuthClient.create(...)).login()`, does the popup open?

I’ll try

> [@peterparker](#):
>
> A bit hard to tell based on the snippet. Can you share a link to the repo?

I’m sorry but this code isn’t public (yet) so I can’t really share the repo right now but if you want I can explain it in more details

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 12, 2024, 2:40pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/7 "2024-08-12T14:40:25Z")

</div>

Let me know if the test work. If it doesn’t, maybe you can try to share a minimal sample repo to reproduce the issue?

---

<div class="post-metadata">

**Author:** ![Mysticx](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mysticx](https://forum.dfinity.org/u/Mysticx)\
**Post date:** [August 12, 2024, 4:40pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/9 "2024-08-12T16:40:42Z")

</div>

I’ve created the repo for the II authentication [here](https://github.com/AbdullahUmar-fantasy/II-authentication/tree/master).  
And I did verify the issue it still persists (on Mac safari). I tested it by deploying to the playground

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 12, 2024, 4:55pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/10 "2024-08-12T16:55:41Z")

</div>

Thanks for the sample repo 👍. I can reproduce the issue with it. After some debugging, I believe the problem is related to creating the `AuthClient` within the same function or click event that handles the login. If I instantiate the `AuthClient` on mount and perform only the login within the function, then the popup opens correctly in Safari.

```auto

function HomePage() {

  const [authClient, setAuthClient] = useState(undefined);

  useEffect(() => {
    (async () => {
      const client = await AuthClient.create({
        idleOptions: {
          idleTimeout: (1000 * 60 * 60) / 2 // set to half an hour
        }
      })
      setAuthClient(client);
    })();
  }, [])

  async function handleLogin() {
    // await AuthClient.create(...) <---- Doing this here too blocks the popup

    await authClient?.login();
  }
  return (
    <div className={styles.container}>
      <Head>
        <title>Internet Computer</title>
      </Head>
      <main className={styles.main}>
        <h3 className={styles.title}>
          Welcome to Next.js Internet Computer Starter Template!
        </h3>
        <button onClick={handleLogin}>Login</button>
        
      </main>
    </div>
  )
}

export default HomePage

```

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [August 12, 2024, 5:22pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/11 "2024-08-12T17:22:30Z")

</div>

Thank you for helping debug, @peterparker!

@Mysticx his recommendation matches what I also recommend, as the maintainer of the library.

Safari can be very particular about how it allows new windows to be opened, so as a rule you should avoid any async behavior in between the `click` handler and the function that opens the window.

`AuthClient.create` is async because it needs to make an asynchronous query against indexeddb to see if you have a previously created delegation or key it can use to restore your session. That check is getting in between the click and opening the window.

The correct way to handle this is to construct the auth client during the `mount`, `init`, or equivalent phase of your app, and then to invoke the `login` method directly from your click handler. Passing both `onSuccess` and `onError` handlers is also a best practice!

---

<div class="post-metadata">

**Author:** ![Mysticx](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Mysticx](https://forum.dfinity.org/u/Mysticx)\
**Post date:** [August 13, 2024, 3:12am UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/12 "2024-08-13T03:12:27Z")

</div>

Thanks for the help! @peterparker,  
I had the thought that I should try to move the client.create (so ashamed of myself rn).

> [@anon74414410](#):
>
> Safari can be very particular about how it allows new windows to be opened, so as a rule you should avoid any async behavior in between the `click` handler and the function that opens the window.

it is still weird to me how safari doesn’t allow async calls in between the event and the pop-up opening function.

> [@anon74414410](#):
>
> The correct way to handle this is to construct the auth client during the `mount` , `init` , or equivalent phase of your app, and then to invoke the `login` method directly from your click handler. Passing both `onSuccess` and `onError` handlers is also a best practice!

Yea this is what I’m doing in my actual implementation. (just couldn’t be bothered to add all that in the repo)

---

<div class="post-metadata">

**Author:** ![ilbert](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ilbert/32/8717_2.png) [@ilbert](https://forum.dfinity.org/u/ilbert)\
**Post date:** [September 13, 2024, 1:39am UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/13 "2024-09-13T01:39:43Z")

</div>

You saved the [Fortune Wheel](https://github.com/ilbertt/fortune-wheel-booth) for multiple live conferences! Now the admin login button is working again!

cc @Vivienne

---

<div class="post-metadata">

**Author:** ![msalvatti](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/msalvatti/32/19014_2.png) [@msalvatti](https://forum.dfinity.org/u/msalvatti)\
**Post date:** [October 23, 2024, 8:18pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/14 "2024-10-23T20:18:36Z")

</div>

@anon74414410  
I have the login code and it works on desktop, but on mobile it doesn’t open the window, whether new or tab, to log in.  
I tried using the windowOpenerFeatures property, but it only works on desktop.  
Is there any way to make it work on mobile? I tested it with browsers like brave, edge, safari and they all look the same, they don’t open any window or tab.

```auto
export async function IdentityAuthenticate(
  dispatch: AppDispatch,
): Promise<void> {
  try {
    const authClient = await AuthClient.create()
    const HTTP_AGENT_HOST = `${process.env.HTTP_AGENT_HOST}`
    const AUTH_EXPIRATION_INTERNET_IDENTITY = BigInt(
      30 * 24 * 60 * 60 * 1000 * 1000 * 1000, 
    )

    let windowFeatures = undefined
    const isDesktop = window.innerWidth > 768
    if (isDesktop) {
      const width = 500
      const height = 600
      const left = window.screenX + (window.innerWidth - width) / 2
      const top = window.screenY + (window.innerHeight - height) / 2
      windowFeatures = `left=${left},top=${top},width=${width},height=${height}`
    }

    await authClient.login({
      maxTimeToLive: AUTH_EXPIRATION_INTERNET_IDENTITY,
      identityProvider: HTTP_AGENT_HOST,
      derivationOrigin: `${process.env.ENV_AUTH_DERIVATION_ORIGIN}`,
      windowOpenerFeatures: windowFeatures,
      onSuccess: () => {
        const identity = authClient.getIdentity()
        const myAgent = getAgent(identity)
        doLogin(myAgent, dispatch)
      },
      onError: (error) => {
        console.error('Internet Identity authentication failed', error)
      },
    })
  } catch (error) {
    console.error('Unexpected error during authentication process', error)
  }
}

```

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [October 23, 2024, 9:30pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/15 "2024-10-23T21:30:51Z")

</div>

@msalvatti see my earlier comment in this thread - the best practice is to avoid async behavior between the `click` event and the `login` method.

Create your `authClient` instance beforehand and just call `login` when the user interacts. This is a browser behavior that we have no way of working around

---

<div class="post-metadata">

**Author:** ![msalvatti](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/msalvatti/32/19014_2.png) [@msalvatti](https://forum.dfinity.org/u/msalvatti)\
**Post date:** [October 24, 2024, 6:21am UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/16 "2024-10-24T06:21:14Z")

</div>

@anon74414410 I saw and followed your comment above, but the behavior remained the same, on mobile the new tab still does not open doing as you said or as I sent.  
Would there be something different to do?

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [October 24, 2024, 6:38pm UTC](https://forum.dfinity.org/t/internet-identity-pop-up-blocked-on-safari/34105/17 "2024-10-24T18:38:31Z")

</div>

Try a pattern that looks more like this: [auth-client-demo/src/auth\_client\_demo\_assets/react/use-auth-client.jsx at main · krpeacock/auth-client-demo · GitHub](https://github.com/krpeacock/auth-client-demo/blob/main/src/auth_client_demo_assets/react/use-auth-client.jsx#L62)

I have examples for React, Vue, Svelte, and vanilla JS. There’s also a hook library if you’re using React - [@dfinity/use-auth-client - npm](https://www.npmjs.com/package/@dfinity/use-auth-client)
