<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Michael Pierce</title>
    <description>The latest articles on DEV Community by Michael Pierce (@59023g).</description>
    <link>https://dev.to/59023g</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F878928%2F25e34ef4-b0d9-4796-a95a-57ae0ad5fe8a.jpeg</url>
      <title>DEV Community: Michael Pierce</title>
      <link>https://dev.to/59023g</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/59023g"/>
    <language>en</language>
    <item>
      <title>Supabase &amp; Metamask Signed Authentication (Web3)</title>
      <dc:creator>Michael Pierce</dc:creator>
      <pubDate>Thu, 26 Jan 2023 01:38:09 +0000</pubDate>
      <link>https://dev.to/59023g/supabase-metamask-signed-authentication-web3-53e1</link>
      <guid>https://dev.to/59023g/supabase-metamask-signed-authentication-web3-53e1</guid>
      <description>&lt;p&gt;Supabase is a great tool. But currently lacks the ability to natively use a &lt;a href="https://github.com/supabase/gotrue/pull/282" rel="noopener noreferrer"&gt;Web3 Provider to authenticate&lt;/a&gt;. This guide aims to provide a walkthrough so you'll be able to issue JSON web tokens to users who sign-in with their Ethereum Wallet.&lt;/p&gt;

&lt;h4&gt;
  
  
  What you'll need:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://supabase.com/" rel="noopener noreferrer"&gt;Supabase&lt;/a&gt; account

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;JWT&lt;/code&gt; Key and &lt;code&gt;Service&lt;/code&gt; Key&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;Supabase &lt;code&gt;public.users&lt;/code&gt; table&lt;/li&gt;

&lt;li&gt;A Client able to interact with Wallet Provider 

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethers-io/ethers.js/" rel="noopener noreferrer"&gt;ethers.js&lt;/a&gt; and MetaMask&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Serverless Function Endpoints:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  /api/nonce
  /api/login
  /api/write
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;This walkthrough assumes your user has already connected their wallet. If you need help with that, check out the &lt;a href="https://docs.ethers.org/v5/" rel="noopener noreferrer"&gt;ethers.js&lt;/a&gt; documentation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Supabase &lt;code&gt;public.users&lt;/code&gt; Table
&lt;/h4&gt;

&lt;p&gt;Before we can get into the code, we'll need to set up a new table in our Supabase project. It's basically a copy of &lt;code&gt;auth.users&lt;/code&gt;. Supabase's private, built-in Auth table. This is necessary because Supabase does not allow you to query their Auth table by email or, in our case, Ethereum address (&lt;code&gt;address&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;But why do we need to query it? Well, in order to manually sign a JWT auth &lt;code&gt;token&lt;/code&gt;, we need the user's &lt;code&gt;id&lt;/code&gt; as it's stored in the &lt;code&gt;auth.users&lt;/code&gt; table. So we'll need to store a copy ourselves. We'll also store other user data as it comes up, like a profile picture or email address. Another value is the user's login &lt;code&gt;nonce&lt;/code&gt;. Which we'll get into in the next section.&lt;/p&gt;

&lt;p&gt;Below you'll see what my &lt;code&gt;public.users&lt;/code&gt; table looks like with mock data:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqd9i31w6xvi2ytxmqe8n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqd9i31w6xvi2ytxmqe8n.png" alt=" raw `public.users` endraw  table with mock data" width="800" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Set, Insert, then Return Nonce
&lt;/h4&gt;

&lt;p&gt;Once the tables are setup and the user has connected their wallet, the first thing we'll do behind the scenes in our client app code is make a POST request to the &lt;code&gt;/api/nonce&lt;/code&gt; endpoint. In it, we'll include the just-connected wallet address.&lt;/p&gt;

&lt;p&gt;What's a &lt;code&gt;nonce&lt;/code&gt;? It's a one-time use number we'll include in our &lt;code&gt;/api/login&lt;/code&gt; request to add another layer of security. This is where the &lt;code&gt;/api/nonce&lt;/code&gt; endpoint comes into play. And it's why we hit it first. So, once the server receives the request, it'll generate a random &lt;code&gt;nonce&lt;/code&gt;, insert it to the proper &lt;code&gt;public.users&lt;/code&gt; database row, then send the &lt;code&gt;nonce&lt;/code&gt; back to the client. Once we've done all that, then we'll have them sign message with this nonce. Here's an idea what that endpoint would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// /api/nonce

const { address } = req.body
const nonce = Math.floor(Math.random() * 1000000)

await database
  .from(SUPABASE_TABLE_USER)
  .update({ auth: {
              genNonce: nonce,
              lastAuth: new Date().toISOString(),
              lastAuthStatus: "pending"
          }}) 
  .eq('address', address)

return res.status(200).json({ nonce })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Use the Nonce, Sign a Message
&lt;/h4&gt;

&lt;p&gt;Then, once the client has received the &lt;code&gt;nonce&lt;/code&gt;, we'll then automatically prompt the user to &lt;code&gt;sign()&lt;/code&gt; a message in their wallet. This message should include the address and nonce, but can include whatever you want. It's also usually a good idea from a UX perspective to inform the user this is off-chain and costs no gas. Not everyone is familiar with this concept. &lt;/p&gt;

&lt;p&gt;If you've used Opensea or most other Web3 apps, I'm sure you've seen this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3k9mvhdn9owjh674poxk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3k9mvhdn9owjh674poxk.png" alt="Example Sign()" width="472" height="732"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the client, the code will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // client code

  // prompt user to sign message in wallet
  const msg = await state.activeProvider
    .send("personal_sign", [
      ethers.utils.hexlify(ethers.utils.toUtf8Bytes(message)), state.address.toLowerCase()
    ]);

  // post sign message to api/verify with nonce and address
  const verifyRequest = await postData(
    `${state.config.API_URL}/api/login`, 
    { signed: msg, 
      nonce: nonceRequest.nonce,
      address: state.address 
    }
  )

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Then Login
&lt;/h4&gt;

&lt;p&gt;Now for the fun part. After the user has signed, we hit the &lt;code&gt;/api/login&lt;/code&gt; endpoint with their signed message and &lt;code&gt;nonce&lt;/code&gt;. Then we'll see if the user has an &lt;code&gt;id&lt;/code&gt; yet in the &lt;code&gt;public.users&lt;/code&gt; table. If not, we'll invoke &lt;code&gt;auth.admin.createUser()&lt;/code&gt; to create a user in the &lt;code&gt;auth.users&lt;/code&gt; table, which'll then return the &lt;code&gt;id&lt;/code&gt;. Once we have the id, we'll insert it into our &lt;code&gt;public.users&lt;/code&gt; table, along with any other information we need. In the future, I can query to get an &lt;code&gt;address&lt;/code&gt;'s &lt;code&gt;id&lt;/code&gt;. I know it's a little awkward, but it is a workaround. Check out this code fragment from the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  // api/login (only run this code on server)

  /*
    1. verify the signed message matches the requested address
    2. select * from public.user table where address matches
    3. verify the nonce included in the request matches what's 
    already in public.users table for that address
    4. if there's no public.users.id for that address, then you
    need to create a user in the auth.users table
  */

  const { data: user, error } = await supabase.auth.admin.createUser({
    email: `user@email.com`,
    user_metadata: { address: address }
  })

  // 5. insert response into public.users table with id

  await supabase
    .from(SUPABASE_TABLE_USERS)
    .update({ auth: {
        genNonce: newNonce, // update the nonce, so it can't be reused
        lastAuth: new Date().toISOString(),
        lastAuthStatus: "success"
      },
     id: user.id, // same uuid as auth.users table
    })
    .eq('address', address) // primary key

  // 6. lastly, we sign the token, then return it to client

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we need to sign a &lt;code&gt;token&lt;/code&gt; with our Supabase &lt;code&gt;JWT&lt;/code&gt; then return it to the client. This will allow us to create an RLS Policy so only a particular &lt;code&gt;address&lt;/code&gt; can insert data to either the &lt;code&gt;public.users&lt;/code&gt; table, as an updated profile, for example. Or to another table, which contains off-chain app data that only certain token holders can upload. Whatever you'd like.&lt;/p&gt;

&lt;p&gt;It also introduces a concept of "authenticated" to your client app, beyond just the standard wallet provider connection. Another nice thing is the user won't have to "sign" a message everytime they enter your application. They'll only need to do this again after their JWT expired. Here's an example JWT creation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// /api/login (6.)

const token = jwt.sign({
  address: address, // this will be read by RLS policy
  sub: user.id,
  aud: 'authenticated'
}, JWT, { expiresIn: 60*2 } )

res.status(200).send(token)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the JWT &lt;code&gt;token&lt;/code&gt; is on the client side, we'll want to set up a Supabase RLS Policy for the &lt;code&gt;public.users&lt;/code&gt; table, or any other table we want authenticated users to be able to write to. Shout out to &lt;a href="https://medium.com/@gracew/using-supabase-rls-with-a-custom-auth-provider-b31564172d5d" rel="noopener noreferrer"&gt;Grace Wang&lt;/a&gt; for the scoop on this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmyt2ump0i8x6shq2v665.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmyt2ump0i8x6shq2v665.png" alt="RLS Policy" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This policy tells Postgres/Supabase to decode the &lt;code&gt;token&lt;/code&gt; then compare its' &lt;code&gt;address&lt;/code&gt; value with the row's column &lt;code&gt;address&lt;/code&gt;. If they're equal, it'll allow the database write. If not, no luck. We do this so only the logged in address can write to rows it "owns."  &lt;/p&gt;

&lt;p&gt;And that's it! Please let me know if you have questions or comments.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
