0

I'm doing a login with passport (in API) I'm trying to get the tokens generated by the authentication server. However, I can't add extra parameters to the request which is an instance of FormRequest.

On the other hand, if I change my request to an instance of Request, it works.

So, my question how I can add parameters to my query $loginRequest (which is instance of FormRequest)

$loginRequest->request->add($params);

Here my code:

class AuthController extends Controller
{
    use ThrottlesLogins;

    public function store(LoginRequest $loginRequest)
    {
        $loginRequest->validated();
        if ($this->hasTooManyLoginAttempts($loginRequest)) {
            $this->fireLockoutEvent($loginRequest);
            return $this->sendLockoutResponse($loginRequest);
        }
        if (Auth::attempt($this->credentials($loginRequest))){
            $client = $this->getClient($loginRequest->name);
            $params = [
                'grant_type'    => 'password',
                'client_id'     => $client->id,
                'client_secret' => $client->secret,
                'username'      => $loginRequest->email,
                'password'      => $loginRequest->password,
                'scopes'         => 'fd',
            ];
            $loginRequest->request->add($params);
            $req = Request::create('oauth/token', 'POST');
            $response = Route::dispatch($req)->getContent();
            return $response;
        }

        $this->incrementLoginAttempts($loginRequest);
        $this->sendFailedLoginResponse($loginRequest);
    }
}
diego
  • 408
  • 8
  • 21
  • 1
    Just checked, no problem with using `add()` on custom form requests. – Harven Jan 19 '20 at 22:31
  • the extra parameters don't add up, I know this because I have a server error oauth @Harven – diego Jan 22 '20 at 21:24
  • Server oauth error is not a proof that `add()` does not work. Try to dump `$login->request` right after calling `add()` method and check it manually. – Harven Jan 22 '20 at 22:02
  • the parameters are well added but why doesn't my oauth server receive these parameters? – diego Jan 22 '20 at 22:13

1 Answers1

1

To append properties to an instance of FormRequest you can use the merge() method.

public function store(LoginRequest $loginRequest) {
    $params = [
        'foo' => 'bar',
    ];

    $loginRequest->merge($params);
}
Ed B
  • 849
  • 1
  • 7
  • 25
  • it doesn't work because the auth server sent me this error `"error": "unsupported_grant_type", "error_description": "The authorization grant type is not supported by the authorization server.", "hint": "Check that all required parameters have been provided", "message": "The authorization grant type is not supported by the authorization server."` – diego Jan 22 '20 at 21:26
  • however when I replace my `Loginrequest` by `Request`, I don't get the weird error anymore – diego Jan 22 '20 at 21:28
  • That seems to be an unrelated issue with Laravel Passport and which you will have to fix independently of the `FormRequest` issue. Try the solution in a Laravel project that doesn't use Passport. – Ed B Jan 22 '20 at 22:16
  • Indeed, the `merge` and `add` method works. However, it does not work with passport. – diego Jan 22 '20 at 22:20
  • Sounds like you are missing a middleware or something, in relation to authorization. Look into how to use custom `FormRequests` with Passport. – Ed B Jan 22 '20 at 22:27