Fauna DB v4 + Env variables in Netlify

Hey all! I’ve brought this up in the Fauna forum as well, but I think it has more to do with the Netlify Env Variables:

I’m using Fauna v4 to set up a simple db to store data in my Gatsby / React web application. The submission works locally, but as soon as I push it into my sandbox branch to test live it stops working.

I have checked the other posts in the Fauna forum which consistently point back to an issue with the secret key variable in Netlify or how it’s referenced somewhere else.

Going over this possibility I have not been able to correct the issue. I double and triple checked that the variables matched between Fauna dashboard, .env and Netlify env variables - they all matched but still had the error so instead I just revoked the keys and created new ones and updated Netlify env and local env.

Here’s my variables:

const client = new Client({
  secret: process.env.SERVER_FAUNA_SECRET,
})
const q = faunadb.query
const rejectDb = process.env.TESTX_FAUNA_COLLECTION
const successDb = process.env.TEST_FAUNA_COLLECTION

Here’s the error:

Error checking email existence: Unauthorized: unauthorized. Check that endpoint, schema, port and secret are correct during client’s instantiation 

Here’s the code that threw the error:

 try {
      // check if email is already in database
      const doesEmailExist = await client.query(
        q.Exists(q.Match(q.Index('byEmail'), body.email))
      )

      // function to send form data to fauna - reject
      if (doesEmailExist) {
        setError('You are already entered to win.')
        console.log(
          'This email has already been entered and cannot be entered again.'
        )
        return false
      }
      await client.query(
        q.Create(q.Collection(rejectDb), {
          data: {
            name: body.name,
            email: body.email,
          },
        })
      )
      return true
    } catch (error) {
      console.error(`Error checking email existence: ${error}`)
      setError('Error: Unable to verify email.')
      return false
    }

A thought also occurred to me regarding this issue so I created a temporary secret key and placed it directly into my new Client({secret: ...}) variable then pushed an insecure version into my sandbox. It worked.

I’ve reverted the change and revoked the key for security purposes now, but I believe this means that the issue is in accessing the env variable, not with a wrong key. Not sure what this means yet for solving it though…

The sandbox can be found at: https://sandbox--josephcenter.netlify.app/harley-davidson-giveaway

Anyone have any ideas?

In the Fauna forum I received this answer:

Gatsby requires GATSBY_ to preceed any env vars you need exposed to the browser.

Prepending my Fauna env variables with GATSBY_ did resolve the authentication issue in case anyone runs into this in the future.