8000 Access token is undefined in context · Issue #136 · aerogear/keycloak-connect-graphql · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.
This repository was archived by the owner on Apr 17, 2023. It is now read-only.
Access token is undefined in context #136
Open
@okanji

Description

@okanji

I am trying to make an authenticated request from postman to my node, apollo, express backend. I am getting an error saying that the user is unauthenticated. When I look at the context object, there is no access token and calling context.kauth.isAuthenticated() returns false.

Looking at the access token, I can see that accessToken is indeed blank, but there does exist the Bearer Token in the request header.

enter image description here
enter image description here

So I am not sure why the access token is not being included. Could this be a bug?

I am making the request from postman, I am including the token in the request like so:

enter image description here

In order to get this access token, I am first making a postman request to keycloak to generate this token like so (note that I am intentionally not showing my username and password for this post

enter image description here

I am using the above access token in my postman request above.

This is what my index.js file looks like:

    require("dotenv").config();
    import { ApolloServer } from "apollo-server-express";
    import { ApolloServerPluginDrainHttpServer } from "apollo-server-core";
    const { makeExecutableSchema } = require('@graphql-tools/schema');
    import { configureKeycloak } from "./auth/config"
    import {
      KeycloakContext,
      KeycloakTypeDefs,
      KeycloakSchemaDirectives,
    } from "keycloak-connect-graphql";
    import { applyDirectiveTransformers } from "./auth/transformers";
    import express from "express";
    import http from "http";
    import typeDefs from "./graphql/typeDefs";
    import resolvers from "./graphql/resolvers";
    import { MongoClient } from "mongodb";
    import MongoHelpers from "./dataSources/MongoHelpers";
    
    async function startApolloServer(typeDefs, resolvers) {
    
      const client = new MongoClient(process.env.MONGO_URI);
      client.connect();
    
      let schema = makeExecutableSchema({
        typeDefs: [KeycloakTypeDefs, typeDefs],
        resolvers
      });
    
      schema = applyDirectiveTransformers(schema);
    
      const app = express();
      const httpServer = http.createServer(app);
    
      const { keycloak } = configureKeycloak(app, '/graphql')    
    
      const server = new ApolloServer({
        schema,
        schemaDirectives: KeycloakSchemaDirectives,
        resolvers,
        context: ({ req }) => {
          return {
            kauth: new KeycloakContext({ req }, keycloak) 
          }
          
        },
        plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
      });
      await server.start();
      server.applyMiddleware({ app });
      await new Promise((resolve) => httpServer.listen({ port: 4000 }, resolve));
      console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
    }
    
    startApolloServer(typeDefs, resolvers);

And this is my keyclaok.json file:

enter image description here

I am really quite stummped, my initial thought is that I am not making the reqest from postman correctly. Am grateful for any guidance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0