daniel-de-wit / lighthouse-sanctum

Laravel Sanctum support for Laravel Lighthouse
MIT License
56 stars 9 forks source link

Multiple users providers ? #66

Closed LAGGOUNE-Walid closed 2 years ago

LAGGOUNE-Walid commented 3 years ago

Hello , when i modify the lighthouse-sanctum.provider variable to an array of providers an error appears:

  "errors": [
    {
      "debugMessage": "Array to string conversion",
      "message": "Internal server error",
      "extensions": {
        "category": "internal"
      },
      "locations": [
        {
          "line": 2,
          "column": 5
        }
      ],
      "path": [
        "login"
      ],
      "trace": [
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Auth/CreatesUserProviders.php",
          "line": 57,
          "call": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions::handleError(2, 'Array to string conversion', '/var/www/html/api/vendor/laravel/framework/src/Illuminate/Auth/CreatesUserProviders.php', 57)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Auth/CreatesUserProviders.php",
          "line": 26,
          "call": "Illuminate\\Auth\\AuthManager::getProviderConfiguration(array(2))"
        },
        {
          "file": "/var/www/html/api/vendor/daniel-de-wit/lighthouse-sanctum/src/Traits/CreatesUserProvider.php",
          "line": 21,
          "call": "Illuminate\\Auth\\AuthManager::createUserProvider(array(2))"
        },
        {
          "file": "/var/www/html/api/vendor/daniel-de-wit/lighthouse-sanctum/src/GraphQL/Mutations/Login.php",
          "line": 34,
          "call": "DanielDeWit\\LighthouseSanctum\\GraphQL\\Mutations\\Login::createUserProvider()"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Schema/Directives/FieldDirective.php",
          "line": 51,
          "call": "DanielDeWit\\LighthouseSanctum\\GraphQL\\Mutations\\Login::__invoke(null, array(4), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Schema/Directives/RenameArgsDirective.php",
          "line": 33,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\FieldDirective::Nuwave\\Lighthouse\\Schema\\Directives\\{closure}(null, array(3), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Schema/Directives/SpreadDirective.php",
          "line": 34,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\RenameArgsDirective::Nuwave\\Lighthouse\\Schema\\Directives\\{closure}(null, array(3), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Schema/Directives/ArgTraversalDirective.php",
          "line": 29,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\SpreadDirective::Nuwave\\Lighthouse\\Schema\\Directives\\{closure}(null, array(1), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Validation/ValidateDirective.php",
          "line": 53,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\ArgTraversalDirective::Nuwave\\Lighthouse\\Schema\\Directives\\{closure}(null, array(1), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Schema/Directives/ArgTraversalDirective.php",
          "line": 29,
          "call": "Nuwave\\Lighthouse\\Validation\\ValidateDirective::Nuwave\\Lighthouse\\Validation\\{closure}(null, array(1), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Schema/Directives/TrimDirective.php",
          "line": 56,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\ArgTraversalDirective::Nuwave\\Lighthouse\\Schema\\Directives\\{closure}(null, array(1), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Schema/Factories/FieldFactory.php",
          "line": 92,
          "call": "Nuwave\\Lighthouse\\Schema\\Directives\\TrimDirective::Nuwave\\Lighthouse\\Schema\\Directives\\{closure}(null, array(1), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 623,
          "call": "Nuwave\\Lighthouse\\Schema\\Factories\\FieldFactory::Nuwave\\Lighthouse\\Schema\\Factories\\{closure}(null, array(1), instance of Nuwave\\Lighthouse\\Schema\\Context, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 550,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveFieldValueOrError(instance of GraphQL\\Type\\Definition\\FieldDefinition, instance of GraphQL\\Language\\AST\\FieldNode, instance of Closure, null, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 474,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField(GraphQLType: Mutation, null, instance of ArrayObject(1), array(1))"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 857,
          "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0), 'login')"
        },
        {
          "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0), 'login')"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 859,
          "function": "array_reduce(array(1), instance of Closure, array(0))"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 490,
          "call": "GraphQL\\Executor\\ReferenceExecutor::promiseReduce(array(1), instance of Closure, array(0))"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 263,
          "call": "GraphQL\\Executor\\ReferenceExecutor::executeFieldsSerially(GraphQLType: Mutation, null, array(0), instance of ArrayObject(1))"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php",
          "line": 215,
          "call": "GraphQL\\Executor\\ReferenceExecutor::executeOperation(instance of GraphQL\\Language\\AST\\OperationDefinitionNode, null)"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 156,
          "call": "GraphQL\\Executor\\ReferenceExecutor::doExecute()"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/GraphQL.php",
          "line": 162,
          "call": "GraphQL\\Executor\\Executor::promiseToExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter, instance of GraphQL\\Type\\Schema, instance of GraphQL\\Language\\AST\\DocumentNode, null, instance of Nuwave\\Lighthouse\\Schema\\Context, array(0), null, null)"
        },
        {
          "file": "/var/www/html/api/vendor/webonyx/graphql-php/src/GraphQL.php",
          "line": 94,
          "call": "GraphQL\\GraphQL::promiseToExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter, instance of GraphQL\\Type\\Schema, instance of GraphQL\\Language\\AST\\DocumentNode, null, instance of Nuwave\\Lighthouse\\Schema\\Context, array(0), null, null, array(29))"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/GraphQL.php",
          "line": 213,
          "call": "GraphQL\\GraphQL::executeQuery(instance of GraphQL\\Type\\Schema, instance of GraphQL\\Language\\AST\\DocumentNode, null, instance of Nuwave\\Lighthouse\\Schema\\Context, array(0), null, null, array(29))"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/GraphQL.php",
          "line": 164,
          "call": "Nuwave\\Lighthouse\\GraphQL::executeQuery(instance of GraphQL\\Language\\AST\\DocumentNode, instance of Nuwave\\Lighthouse\\Schema\\Context, array(0), null, null)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/GraphQL.php",
          "line": 118,
          "call": "Nuwave\\Lighthouse\\GraphQL::executeOperation(instance of GraphQL\\Server\\OperationParams, instance of Nuwave\\Lighthouse\\Schema\\Context)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Support/Utils.php",
          "line": 98,
          "call": "Nuwave\\Lighthouse\\GraphQL::Nuwave\\Lighthouse\\{closure}(instance of GraphQL\\Server\\OperationParams)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/GraphQL.php",
          "line": 117,
          "call": "Nuwave\\Lighthouse\\Support\\Utils::applyEach(instance of Closure, instance of GraphQL\\Server\\OperationParams)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Support/Http/Controllers/GraphQLController.php",
          "line": 32,
          "call": "Nuwave\\Lighthouse\\GraphQL::executeOperationOrOperations(instance of GraphQL\\Server\\OperationParams, instance of Nuwave\\Lighthouse\\Schema\\Context)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
          "line": 48,
          "call": "Nuwave\\Lighthouse\\Support\\Http\\Controllers\\GraphQLController::__invoke(instance of Illuminate\\Http\\Request, instance of Nuwave\\Lighthouse\\GraphQL, instance of Illuminate\\Events\\Dispatcher, instance of Laragraph\\Utils\\RequestParser, instance of Nuwave\\Lighthouse\\Execution\\SingleResponse, instance of Nuwave\\Lighthouse\\Execution\\ContextFactory)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
          "line": 261,
          "call": "Illuminate\\Routing\\ControllerDispatcher::dispatch(instance of Illuminate\\Routing\\Route, instance of Nuwave\\Lighthouse\\Support\\Http\\Controllers\\GraphQLController, '__invoke')"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
          "line": 204,
          "call": "Illuminate\\Routing\\Route::runController()"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 695,
          "call": "Illuminate\\Routing\\Route::run()"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 128,
          "call": "Illuminate\\Routing\\Router::Illuminate\\Routing\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Support/Http/Middleware/AttemptAuthentication.php",
          "line": 33,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Nuwave\\Lighthouse\\Support\\Http\\Middleware\\AttemptAuthentication::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/nuwave/lighthouse/src/Support/Http/Middleware/AcceptJson.php",
          "line": 27,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Nuwave\\Lighthouse\\Support\\Http\\Middleware\\AcceptJson::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php",
          "line": 33,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 128,
          "call": "Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful::Laravel\\Sanctum\\Http\\Middleware\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 103,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php",
          "line": 34,
          "call": "Illuminate\\Pipeline\\Pipeline::then(instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 103,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 697,
          "call": "Illuminate\\Pipeline\\Pipeline::then(instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 672,
          "call": "Illuminate\\Routing\\Router::runRouteWithinStack(instance of Illuminate\\Routing\\Route, instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 636,
          "call": "Illuminate\\Routing\\Router::runRoute(instance of Illuminate\\Http\\Request, instance of Illuminate\\Routing\\Route)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
          "line": 625,
          "call": "Illuminate\\Routing\\Router::dispatchToRoute(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
          "line": 166,
          "call": "Illuminate\\Routing\\Router::dispatch(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 128,
          "call": "Illuminate\\Foundation\\Http\\Kernel::Illuminate\\Foundation\\Http\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
          "line": 21,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php",
          "line": 31,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
          "line": 21,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php",
          "line": 40,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
          "line": 27,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
          "line": 86,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/fruitcake/laravel-cors/src/HandleCors.php",
          "line": 52,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Fruitcake\\Cors\\HandleCors::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/fideloper/proxy/src/TrustProxies.php",
          "line": 57,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 167,
          "call": "Fideloper\\Proxy\\TrustProxies::handle(instance of Illuminate\\Http\\Request, instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
          "line": 103,
          "call": "Illuminate\\Pipeline\\Pipeline::Illuminate\\Pipeline\\{closure}(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
          "line": 141,
          "call": "Illuminate\\Pipeline\\Pipeline::then(instance of Closure)"
        },
        {
          "file": "/var/www/html/api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
          "line": 110,
          "call": "Illuminate\\Foundation\\Http\\Kernel::sendRequestThroughRouter(instance of Illuminate\\Http\\Request)"
        },
        {
          "file": "/var/www/html/api/public/index.php",
          "line": 52,
          "call": "Illuminate\\Foundation\\Http\\Kernel::handle(instance of Illuminate\\Http\\Request)"
        }
      ]
    }
  ]
}
daniel-de-wit commented 2 years ago

I'm not aware Laravel Sanctum supports multiple user providers when using API Token Authentication.

I intend to stay as close as possible to the original Laravel Sanctum package.

LAGGOUNE-Walid commented 2 years ago

I figured out a way to support multiple users login . when executing Login query , the client should specify which user type he is :

mutation {
  login(
    input: {
      usertype: "X"
      username: "mario@super.com"
      password: "mario"
    }
  ) {
    token
    user {
      ... on User {
        id
      }
    }
  }
}

and then create union type to group all users in one response type

union authenticatedUser = X | Y | Z

and changed the type of the LoginResponse on sanctum.graphql :

type LoginResponse {
    token: String
    user: authenticatedUser
}

and also changed the code on Mutations/Login.php

if ($args["usertype"] == "x") {
            $provider = "x";
        }elseif($args["usertype"] == "y") {
            $provider = "y";
        }else {
            $provider = "z";
        }

i hope this could help someone.