401 error in staging & production - works in local

I’m using 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 forum which consistently point back to an issue with the 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
    }

Anyone have any ideas?

In my experience, a 401 error here means that the secret is invalid. Double check your env vars and make sure you are using a valid secret.

Thank you for your response @wallslide. Referring back to my post:

The variable I’m referring to contains the Fauna secret key. Sorry that wasn’t clearer.

A thought 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 this means that the issue is in accessing the env variable, not with a bad or wrong key connection. Not sure what this means yet…

Is this server-side or browser-side? A lot of frameworks require you to prefix env vars with something particular in order for it to be available to the browser. Gatsby requires GATSBY_ to preceed any env vars you need exposed to the browser. Does that help?

Thanks for this tip @ptpaterson. Yes, that did resolve the authentication issue. Thank you! I’ll mark that as the solution.

Now I just have to figure out why my doesEmailExist query is always returning false:thinking:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.