Udders
Udders

Reputation: 6986

laravel calling route from a controller

I am writing an API at the moment in Laravel, and using passport. My client will consume it's own API, so I am using personal access in Passport.

I am not wanting to show my oauth route and grant id, or secret in the POST request so I have created a route that sits the user posts too to login, and then deals with send a POST request to the oauth/token route, like below,

protected function authenticate(Request $request) {
        //return $request->input();
        //return Response::json($this->client);
        $email = $request->input('username');
            $password = $request->input('password');
            $request->request->add([
                'username' => $email,
                'password' => $password,
                'grant_type' => 'password',
                'client_id' => $this->client->id,
                'client_secret' => $this->client->secret,
                'scope' => '*'
            ]);

            $tokenRequest = Request::create(
                env('APP_URL').'/oauth/token',
                'post'
            );

            return Route::dispatch($tokenRequest)->getContent();

        }

My problem is that my authentication returns 200 irrespective of whether the oauth login was successful. Is there a way to fire a route from a controller and return that http code for that rather than the method it was called from http response?

Upvotes: 1

Views: 1860

Answers (1)

OmEr
OmEr

Reputation: 31

this should fix the problem.

$data = [
        'grant_type'=> 'password',
        'client_id'=> 99,
        'client_secret'=> 'hgfhfhjnhnjnjnjnj',
        'username'=> $request->username,
        'password'=> $request->password,
        'scopes'=> '[*]'
    ];
$request = Request::create('/oauth/token', 'POST', $data);
return app()->handle($request);

Upvotes: 2

Related Questions