Git Product home page Git Product logo

Comments (5)

lindyhopchris avatar lindyhopchris commented on August 10, 2024

Hi! I wouldn't ever recommend creating a new request within an existing Laravel request, as Laravel assumes it is only handling one request at once.

Why don't you just update the user model yourself? Why do you have to pass it off as a JSON:API process?

from laravel-json-api.

hymenoby avatar hymenoby commented on August 10, 2024

@lindyhopchris
Okay, thanks for your reply.

I want to reuse the api endpoints. And i want also to benefit of the already implemented validations and rules in place in this api endpoint.

I already used this logic for the passport api without trouble like this:

<?php

namespace App\Http\Controllers\Api\V1\Auth;

use Illuminate\Support\Facades\DB;
use App\Http\Requests\Api\V1\Auth\LoginRequest;
use CloudCreativity\LaravelJsonApi\Document\Error\Error;
use CloudCreativity\LaravelJsonApi\Http\Controllers\JsonApiController;
use Illuminate\Support\Facades\Request;

class LoginController extends JsonApiController
{
    /**
     * Handle the incoming request.
     *
     * @param LoginRequest $request
     * @return mixed
     */
    public function __invoke(LoginRequest $request)
    {
        $client = DB::table('oauth_clients')->where('password_client', 1)->first();

        $oauthRequest = Request::create(
            route('passport.token', [], false),
            'POST',
            [
                'grant_type' => 'password',
                'client_id' => $client->id,
                'client_secret' => $client->secret,
                'username' => $request->email,
                'password' => $request->password,
                'scope' => '',
            ],
        );
        $response = app()->handle($oauthRequest);

        if ($response->status() == 200) {
            return json_decode((string)$response->getContent(), true);
        } else {
            $error = json_decode((string)$response->getContent(), true);
            return $this->reply()->errors([
                Error::fromArray([
                    'title' => 'Bad Request',
                    'detail' => $error['message'],
                    'status' => '400',
                ])
            ]);
        }
    }
}
```

from laravel-json-api.

lindyhopchris avatar lindyhopchris commented on August 10, 2024

Sending a request within another request is just not something I'd recommend doing. Using the HTTP client would seem like a lot better idea.

I think this shows a need for a developer being able to execute a JSON:API "operation" wherever they want. This is something I'd support in a future version, as I need to separate out the operations to support the Atomic Operations extension. However, at the moment that's not something that's supportable in the current codebase as the operations are coupled to the HTTP requests due to using form request classes.

from laravel-json-api.

hymenoby avatar hymenoby commented on August 10, 2024

Okay thanks, i will go with the http client then.
Thanks

from laravel-json-api.

lindyhopchris avatar lindyhopchris commented on August 10, 2024

I'm going to leave this open as a reminder that I need to make it easier for developers to manually trigger a JSON:API operation.

from laravel-json-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.