Skip to content

hharzer/netlify-faunadb-graphql-auth

Repository files navigation

Netlify + FaunaDB + GraphQL + Auth    

Example of using FaunaDB with Netlify functions.

This example shows how to use HTTP only cookies for auth with FaunaDB's native Graphql API.

About this application

This application is using React for the frontend, Netlify Functions for API calls, and FaunaDB as the backing database, utilizing the Graphql API with Apollo. This project is bundled with Parcel.

The todo editing is enabled by Draft.js

This project is primarily derived from netlify-faunadb-example

faunadb netlify

Easy setup with Netlify dev

Netlify Dev works with parcel-bundler out of the box!

(it also works with create-react-app)

So there is no need to install netlify-lambda and set up function proxies.

How to Setup & Run Locally

Get your code

  1. Clone down the repository

    git clone https://github.com/ptpaterson/netlify-faunadb-graphql-auth.git
  2. Enter the repo directory

    cd netlify-faunadb-example
  3. Install the dependencies

    npm install
    # -OR-
    yarn

Prepare FaunaDB database

  1. Sign up for a FaunaDB account

    https://dashboard.fauna.com/accounts/register

  2. Create a master database. It will become the parent database for the app database generated by the script provided. In the Fauna Cloud Console:

    • Click “New Database”
    • Enter any name, e.g. “Netlify”, as the “Database Name”
    • Click “Save”
  3. Create a database access key

    In the Fauna Cloud Console:

    • Click “Security” in the left navigation
    • Click “New Key”
    • Make sure that the “Database” field is set to “Netlify” (or whatever you named it)
    • Make sure that the “Role” field is set to “Admin”
    • Enter any name, e.g. “Netlify Admin key” as the “Key Name”
    • Click “Save”
  4. Copy the database access key’s secret

    Save the secret somewhere safe; you won’t get a second chance to see it.

Bootstrap the database

  1. Set your database access secret for running the bootstrapping scripts Either set the environment variables directly in your terminal, or use a .env file. The scripts will pick up anything in a .env file.

    In your terminal, run the following command:

    # bash
    export FAUNADB_ADMIN_KEY="YourFaunaDBSecretHere"

    NOTE: Make sure to replace YourFaunaDBSecretHere with the value of the secret that you copied in the previous step.

    NOTE: We'll set a different variable for FAUNADB_PUBLIC_KEY in a bit, which is the important key to actually run the app!

  2. Bootstrap your FaunaDB collection and indexes

    npm run bootstrap-db

    The bootstrap-db script will output some important information. Copy down the created keys shown to you in the output. If you lose the keys, you can generate them by hand or run the bootstrap command again.

    Output should be similar to:

    Creating your FaunaDB Database...
    
    1) Create database "netlify-auth..."
    + Created Database "aut_example".
    
    2) Establish keys..."
    + Created "Admin key for auth_example"
    o Using FAUNADB_APP_ADMIN_KEY: fnADl6mmyxACE_l3awMn6E0-hVWMJfsj9YEGMLcP
    + Created "Server key for auth_example"
    o Using FAUNADB_SERVER_KEY: fnADl6mm1hACEyjnsgULIdYdY8OO0wFnb6fGIcjG
    
    3) Create "User"...
    + Created new Collection "User".
    + Created Index "unique_User_email".
    
    4) Create "Todo"...
    + Created Collection "Todo"
    + Created Index "all_todos"
    + Created Index "todo_owner_by_user"
    
    5) Create User Defined Functions...
    + Created UDF "login"
    + Created UDF "logout"
    + Created UDF "me"
    + Created UDF "user_create_todo"
    
    6) Create custom roles...
    + Created Role "public"
    + Created new public client key.
    o Using FAUNADB_PUBLIC_KEY: fnADl6mnYoACE0tFKEmd6TVXhyuwdm8jVV9Zqalm
    + Created Role "user"
    
    Fauna Database schema has been created
    Claim your fauna database with "netlify addons:auth fauna"
    
  3. Upload Graphql Schema

    In the Fauna Cloud Console:

  4. (Optional) Add example data

    Set the FAUNADB_SERVER_KEY variable (or save to .env file).

    Then run

    npm run bootstrap-example-data

    Or, use the GraphQL Playground now available in the cloud console.

Run project Locally

  1. Get netlify-cli

    npm install netlify-cli -g
  2. Run the project

    netlify dev

Updating the Schema

This project uses apollo-server-lambda for the lambda function. Lambdas are not a great solution for fetching remote schemas. Because of this, any time a new schema is uploaded to FaunaDB, the SDL should be downloaded and placed as a string in functions/graphql/remoteSchema.js.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published