Backend Auth with a NEAR Wallet
Authenticating users is a common use-case for backends and web applications. This enables services to provide a personalized experience to users, and to protect sensitive data. To authenticate a user, the backend must verify that the user is who they say they are. To do so, the backend must verify that the user has access to a full-access key that is associated with their account. For this three basic steps are needed:- Create a challenge for the user to sign.
- Ask the user to sign the challenge with the wallet.
- Verify the signature corresponds to the user.
1. Create a Challenge
Assume we want to login the user into our application namedapplication-name.
We first need to create a challenge that the user will sign with their wallet. For this, it is recommended to use a cryptographically secure random number generator to create the challenge.
Here we use crypto.randomBytes to generate a 32 byte random buffer.
2. Ask the User to Sign the Challenge
ThesignMessage method needed to sign the challenge is supported by these wallets:
- Meteor Wallet
- Here Wallet
- Near Snap
- Nightly Wallet
- WELLDONE Wallet
- NearMobileWallet
- MyNearWallet
- Sender
- Intear Wallet
- Message: The message that the user is signing.
- Recipient: The recipient of the message.
- Nonce: The challenge that the user is signing.
- Callback URL: The URL that the wallet will call with the signature.
3. Verify the Signature
Once the user has signed the challenge, the wallet will call thecallbackUrl with the signature. The backend can then verify the signature.