Dfx 0.12.1 - importing createActor - dfx generate - do not import from .dfx when using React

Sorry for the bad title. I’ll just go through it step by step:

I just upgraded to dfx 0.12.1 and was using the following lines to make non-anonymous calls from my React frontend but this doesn’t work anymore since createActor is deprecated:

import { AuthClient } from "@dfinity/auth-client";
import { createActor, canisterId } from '../../declarations/my_project';

    const getPrincipal = async () => {
        const authClient = await AuthClient.create();
        const identity = await authClient.getIdentity();
        const myActor = createActor(canisterId, {agentOptions: {identity}});
        const response = await myActor.whoami();

The /declarations/my_project_backend/index.js from where I’m importing createActor says that we should not import from .dfx but instead use dfx generate, so I did the following:

  1. Delete the declarations folder from the frontend
  2. dfx canister create -all
  3. dfx generate
  4. dfx deploy

Usually I would just call dfx deploy and all would be good but as soon as I run that, it overwrites /declarations/my_project_backend/index.js and VisualCode again shows createActor as deprecated.

The React app also gives a 403 error for a POST request to the backend canister: Body: Failed to authenticate request 0x427c252e5babb46736a6d8f729cfbcbfcb59bd4791ddf7c5a54dc80d63cd4147 due to: Invalid delegation: Invalid canister signature: IcCanisterSignature signature could not be verified

Should I just downgrade dfx? What’s up with the dfx generate since it does not seem to be run when using dfx deploy? Any idea how to fix this project?

Typically speaking, the “signature could not be verified” error comes from using a delegated identity that was mainnet on a local replica.

I don’t see your login flow in the example provided above, but can you verify that you are running a local instance of Internet Identity and have logged in with it?

Yes, I do. I just wasn’t aware that it might be connected to the II because the error arose at the same time as the problem with createActor(). This is how I log in to the local II:

    const requestLoginII = async () => {
        const authClient = await AuthClient.create();
        const resolve = await authClient.login({
            identityProvider: 'http://localhost:8000/?canisterId=rkp4c-7iaaa-aaaaa-aaaca-cai'
            // identityProvider: 'https://identity.ic0.app/'

Okay, I’ve just updated my auth client demo for dfx 0.12.1 and didn’t reproduce your issue. Take a look at GitHub - krpeacock/auth-client-demo: Example demo of how to use https://www.npmjs.com/package/@dfinity/auth-client to make authenticated calls to an IC app and hopefully the difference will make sense.

If not, if you can provide me with a minimal repo that reproduces the issue, I’ll look into it!

1 Like

Thank you Kyle! Idk what happened but now it’s working again without me touching anything. Also the look of the II authentication page has slightly changed. Maybe a caching-thing? Anyway, this is resolved for me now.