Why is NEAR requesting authorization when I call a change method on the contract?

I have a change method on my NEAR AssemblyScript Contract, which I'm calling from my front end (browser) code. The user is already signed in and can read the view methods on the contract. It's only after trying to call the change method that the user is prompted with the NEAR Wallet requesting authorization.

How can I call the change method without receiving the prompt to request permission again?

NEAR requesting authorization

Solution 1:

You can also see some examples here:

  • basic github.com/Learn-NEAR/starter--near-api-js
  • advanced github.com/Learn-NEAR/starter--near-api-js

There's basically 2 interfaces here:

// pass the contract name (as you did)
wallet.requestSignIn('dev-1634098284641-40067785396400');
// pass an object with contract and empty list of methods
wallet.requestSignIn({
  contractId: 'dev-1634098284641-40067785396400',
  methodNames: []
});
// add a list of methods to constrain what you can call
wallet.requestSignIn({
  contractId: 'dev-1634098284641-40067785396400',
  methodNames: ['write']
});

Solution 2:

I'm answering my own question, because I spent some time trying to figure it out.

When I was calling the function requestSignIn() on the wallet, I also had to specify the contract name.

import {
  connect,
  WalletConnection
} from 'near-api-js';

...

const near = await connect(nearConfig);
const wallet = new WalletConnection(near);
wallet.requestSignIn('dev-xxxx-yyyy'); // Need to pass the correct contract name to be able to call change methods on behalf of the user without requesting permission again