Skip to content

Backend — Setup Your Database API

ff6347 edited this page Sep 12, 2023 · 5 revisions

You can find more detailed explanations in the APIs repository.

  • Login to your auth0.com account. The authorization of the API is based on this tutorial. We are not using Express.js but the over all concept applies. The take away should be:
    • The credentials for your API (jwksuri, audience, issuer)
    • Tools for testing the API (obtaining a token). e.g.
curl --location --request POST 'https://YOUR_AUTH0_DOMAIN.eu.auth0.com/oauth/token' \
--header 'content-type: application/json' \
--data-raw '{
    "client_id": "YOUR_CLIENT_ID",
    "client_secret": "YOUR_CLIENT_SECRET",
    "audience": "YOUR_AUDIENCE",
    "grant_type": "client_credentials"
}'
  • Login to your vercel.com account
# Move into the api folder
cd gdk-api
# Use nvm to install the used Node.js version
nvm install
# Install all Node.js dependencies
npm ci
# follow the instructions
npx vercel login
  • Deploy once to setup the API Hint!: This will produce errors due to missing environment variables.
# Move into the api folder
cd gdk-api
# Do the initial deploy
npx vercel
  • Setup your variables. Hint!: You can also do this using the vercel's user interface if you feel more comfortable with that.
# the user for the postgres db
npx vercel env add user
# the database name
npx vercel env add database
# the database password
npx vercel env add password
# the host of the db, aws? render.com? localhost?
npx vercel env add host
# defaults to 5432
npx vercel env add port
# below are all taken from auth0.com
npx vercel env add jwksuri
npx vercel env add audience
npx vercel env add issuer
  • Deploy again
npx vercel --prod

Your API should be up and running now. Test it by making a request.

curl --location --request GET 'https://YOUR-APIS-NAME.vercel.app//get?queryType=byage&start=1700&end=1799'

To test if your authentication works try to obtain a access token like mentioned above and then make a request.

curl --location --request POST 'https://giessdenkiez-de-postgres-api.vercel.app//post' \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
    "queryType":"adopt",
    "tree_id":"REAL_TREE_ID_FROM_YOUR_DB",
    "uuid": "auth0|123"
}'

Hint!: Obtaining an access token for your API is only needed for development. The frontend application does not need to do this step. There we only pass the users token as Request Header Authorization: Bearer YOUR_kUSERS_TOKEN which can be retrieved for logged in users using the Auth0 Javascript SDK.