ronatory
ronatory

Reputation: 7324

Hasura Authentication Passport.js Auth Webhook does not work

I tried to use the Passport.js Auth Webhook Boilerplate here to setup a custom authentication in my app. I followed the Readme and tried to test the setup locally first. I could successfully create users via Postman using the /signup API and also use the /login API to receive the token. I implemented a simple login form and tried to made a request to the /login API with username and password from an already created user, then I received the response with the token and saved it in the local storage to add the Authorization: Bearer <token> to the header. Then when I was trying to access a page where I've made a graphQL request I only got this Error: GraphQL error: Internal Server Error. Without using the authentication handling via the webhook, it worked without problems by using the HASURA_GRAPHQL_ADMIN_SECRET in the header. This I've tried yesterday. Today when I start all the servers locally like yesterday I can't even get a successful request via Postman. What's also important to mention I've added the HASURA_GRAPHQL_AUTH_HOOK -> http://localhost:8081/webhook (I removed the admin secret from the authorization header to enable the webhook) and also set permissions for the user to access the todos table like shown here

What might I be doing wrong? Here is my setup. Let me know if you need more information

This is to get the Hasura GraphQL engine and Postgres running as Docker containers, shown here (my docker-compose.yaml file):

version: '3.6'
services:
  postgres:
    image: postgres
    restart: always
    ports:
    - "5432:5432"
    volumes:
    - db_data:/var/lib/postgresql/data
  graphql-engine:
    image: hasura/graphql-engine:v1.0.0-beta.6
    ports:
    - "8080:8080"
    depends_on:
    - "postgres"
    restart: always
    environment:
      HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:@postgres:5432/postgres
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      HASURA_GRAPHQL_ADMIN_SECRET: secret
      HASURA_GRAPHQL_AUTH_HOOK: http://localhost:8081/webhook
volumes:
  db_data:

The passport.js boilerplate project is running on http://localhost:8081

The hasura/graphql-engine is running on http://localhost:8080

The main app is running on http://localhost:3000

Upvotes: 1

Views: 2022

Answers (1)

ronatory
ronatory

Reputation: 7324

So there were two issues:

  1. Like @praveenweb wrote in the comments (in my case for a mac) I had to replace localhost with host.docker.internal for the container to access the host machine's auth server. So instead of HASURA_GRAPHQL_AUTH_HOOK: http://localhost:8081/webhook -> HASURA_GRAPHQL_AUTH_HOOK: http://host.docker.internal:8081/webhook

  2. The reason for why I got no successful response anymore on the second day was that I totally forgot to set the DATABASE_URL in the environment variables like shown here. So in my case export DATABASE_URL=postgres://postgres:@localhost:5432/postgres

Now everything works as expected. Hope this helps someone else too.

Upvotes: 5

Related Questions