Using Fauna with Python Flask- Getting faunadb.errors.Unauthorized

I’ve only started using Fauna recently, and currently I’m working on a simple app with Python Flask, but I came across an error that as gotten me stumped.

Here is a snippet of my file

# Imports
import re
from import generate_password_hash, check_password_hash
from flask import Flask, flash, redirect, url_for, render_template, request, session
from faunadb import query as q
from faunadb.client import FaunaClient
from faunadb.objects import Ref
from faunadb.errors import BadRequest, NotFound
import os, secrets

app = Flask(__name__)

client = FaunaClient(secret='fnAEVC9n1aAAQ63NzTNAgnHhs5IPNTzQvzqlSDwm')

def index():
    if session.get('user_id'):
        flash('You are logged in!', 'warning')
        return redirect(url_for('dashboard'))
    return render_template('index.html')

@app.route('/sign_up/', methods=['POST','GET'])
def sign_up():
    if session.get('user_id'):
            flash('You are logged in!', 'warning')
            return redirect(url_for('dashboard'))

    if request.method =='POST':
        name = request.form['name']
        email = request.form['email']
        password = request.form['password']
        email_regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'

        if not, email) or not 6 < len(password) < 20:
            flash('Invalid email or password!, password needs to be between 6 and 20 characters', 'warning')
            return render_template('sign_up.html')

        if password != request.form['confirm_password']:
            flash('Passwords do not match', 'warning')
            return render_template('sign_up.html')

        password = generate_password_hash(password)
        user = {'name': name, 'email': email, 'password': password}
            new_user = client.query(
                            {'data': user}

        except BadRequest:
            flash('Email already exists')
            session['user_id'] = new_user['ref'].id()
            flash('Account created successfully', 'success')
            return redirect(url_for('dashboard'))
    return render_template('sign_up.html')

if __name__ == "__main__":

I’m trying to test the ‘sign_up’ route, but when I enter in information in a form and submit, I’ll get an Unauthorized error pointing to this line:

 new_user = client.query(

Any ideas on where the error is coming from? I’m guessing it has to do with the way I set up the FaunaSecret.

Hi @Ewson and welcome!!

One of the first things to check is if you have set the Region Group domain for your FaunaClient. The default sets the client to connect to the “Classic” Region Group. If your database is in the US or EU Region Groups, then you need to connect to a different domain.

Check out the docs for connecting the python driver.

For example, to connect to the US Region Group:

client = FaunaClient(

The dashboard will tell you which RG you chose for your database.

If that doesn’t work, then let us know! Cheers.

Hey @Ewson did you ever get this working?

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