I managed to implement the canister calls and it is working fine, but the login cant be implemented. When i click the login button it opens a browser, but the ii says you were sent here for authentication but no request found.
So, you managed to resolve the original Failed to decode CBOR error you shared, right? How did you solve it? It would be helpful if you could share some details in case someone else encounters it in the future.
I might be wrong, but as I mentioned in my previous message, I doubt that postMessage is supported by WebView on mobile devices, so it might not work out of the box and could be the reason for the error you’re encountering. However, these are just assumptions.
In other words, I doubt that Internet Identity can be used with Ionic/Capacitor straight out of the box using only Agent-js as you would on the web. Again, just an assumption.
This is code i have tried to hanlde ii authentication in native apps with the in app browser with cordova plugin. The approach works fine till the authentication, but stays on the identity.ic0.app and do not redirect to the application.
@lmuntaner i tried the implementation without using the authclient from the resources you provided. It works until opening the url and authentication. But i cannot make it to the redirecting to the custom scheme for handling the deep links. Here is my code.
export const authWithNativeII = async ({
url: url_,
maxTimeToLive,
allowPinAuthentication,
derivationOrigin,
sessionIdentity,
autoSelectionPrincipal,
}: {
url: string;
maxTimeToLive?: bigint;
allowPinAuthentication?: boolean;
derivationOrigin?: string;
autoSelectionPrincipal?: string;
sessionIdentity: SignIdentity;
}): Promise<{ identity: DelegationIdentity; authnMethod: string }> => {
// Step 1: Construct the II URL with the authorize hash
const iiUrl = new URL(url_);
iiUrl.hash = '#authorize';
iiUrl.searchParams.append('redirect_uri', 'io.ionic.starter://callback');
const requestParams = new URLSearchParams({
kind: "authorize-client",
sessionPublicKey: JSON.stringify(new Uint8Array(sessionIdentity.getPublicKey().toDer())),
maxTimeToLive: maxTimeToLive?.toString() ?? '',
derivationOrigin: derivationOrigin ?? '',
allowPinAuthentication: allowPinAuthentication ? 'true' : 'false',
autoSelectionPrincipal: autoSelectionPrincipal ?? '',
})
const authUrl = `${iiUrl.toString()}&${requestParams.toString()}`;
// Step 2: Open the II URL using Capacitor Browser
await Browser.open({ url: authUrl });
// Step 3: Listen for the app scheme callback
const redirectPromise = new Promise<string>(async (resolve, reject) => {
const handler = await App.addListener('appUrlOpen', (event) => {
const callbackUrl = event.url;
// Check if the URL matches the expected scheme
if (callbackUrl.startsWith('io.ionic.starter://callback')) {
App.removeAllListeners(); // Remove all listeners once URL is handled
resolve(callbackUrl);
} else {
reject(new Error('Unexpected URL scheme received.'));
}
});
// Fallback in case the listener is never triggered
setTimeout(() => {
handler.remove(); // Clean up the listener after timeout
reject(new Error('Timeout waiting for callback URL.'));
}, 60000); // 60 seconds timeout
});
// Wait for the redirect URL
const callbackUrl = await redirectPromise;
// Step 4: Extract the parameters from the callback URL
const url = new URL(callbackUrl);
const delegations = url.searchParams.get('delegations');
const userPublicKey = url.searchParams.get('userPublicKey');
const authnMethod = url.searchParams.get('authnMethod');
if (!delegations || !userPublicKey) {
throw new Error('Missing required parameters in callback URL.');
}
// Step 5: Process the response and create the DelegationIdentity
const response = {
kind: 'authorize-client-success',
delegations: JSON.parse(decodeURIComponent(delegations)),
userPublicKey: Uint8Array.from(
atob(decodeURIComponent(userPublicKey)),
(c) => c.charCodeAt(0)
),
authnMethod,
};
const identity = identityFromResponse({
response: response as AuthResponseSuccess,
sessionIdentity,
});
// Step 6: Close the browser (optional)
await Browser.close();
return { identity, authnMethod: authnMethod || 'unknown' };
};
is there something missing in the code to redirected to the custom scheme after succesful login or will it be automatic.