signer function
to the Polybase client. Signing a request populates the ctx.publicKey variable inside your collection code, allowing you to write code that
restricts who can read or write to a record.
This can either be done when instantiating the client, using the signer configuration property:
signer method:
Polybase Auth
To make authentication easier, Polybase provides an authentication library that allows users to sign in to your app using web3 wallets or email. The library hides the complexity of the users chosen authentication method, providing a single API for you to sign requests regardless of the method they use to login.
Install Polybase Auth
From Package Manager:Initialize
You should only run thenew Auth class once, and then pass it to your app.
Next.js
Next.js runs your code both server side and client side. When running server side Next.js has nodocument so Auth will fail to load.
Auth doesn’t make sense server side, so your code should handle this scenario by skipping any auth code. Before calling auth functions (e.g. auth.signIn()),
you will need to check that auth is defined.
Sign in
authState for email login would be:
authState for Metamask login would be:
Get Public Key with Metamask
Metamask does not currently provide thepublicKey directly but it can be obtained by signing a request.
Force open signIn modal
By default, if the user is already logged in to your app, then the sign in modal won’t open. Instead, the auth state will be returned immediately. If you’d like to allow the user to change the auth/account they are logged in with, you can force open the modal by passing{ force: true } to the signIn function.
Sign out
Ethereum Personal Sign
Ethereum Personal Sign is the signing mechanism used by Polybase (and many other dapps) to authenticate ownership of a public key and to sign data.auth.ethPersonalSign() in the Polybase client signer function:
Listening for updates
Your app can listen for updates in the authentication state, by adding an event listener. This can be useful when a user returns to your website.Example
Open in CodeSandboxReact
If you’re using React, you can use our React library@polybase/react to help with control authentication.