Skip to content

Commit

Permalink
Merge 87bb795 into 4f37583
Browse files Browse the repository at this point in the history
  • Loading branch information
kresnasatya committed Aug 27, 2019
2 parents 4f37583 + 87bb795 commit ca4c531
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions app/Http/Controllers/Api/Auth/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,28 @@

namespace App\Http\Controllers\Api\Auth;

use Route;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuthController extends Controller
{
public function login(Request $request)
{
$http = new \GuzzleHttp\Client;

try {
$response = $http->post(url('/oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'),
'client_secret' => config('services.passport.client_secret'),
'username' => $request->email,
'password' => $request->password,
],
]);

return $response->getBody();
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getCode() === 400) {
return response()->json('Invalid request. Please input an email or a password', $e->getCode());
} elseif ($e->getCode() === 401) {
return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
}

return response()->json('Something wrong on the server', $e->getCode());
}
$form = [
'grant_type' => 'password',
'client_id' => config('services.passport.client_id'),
'client_secret' => config('services.passport.client_secret'),
'username' => $request->email,
'password' => $request->password,
];

$request->request->add($form);

$requestToken = Request::create('oauth/token', 'POST');
$response = Route::dispatch($requestToken);

return response()->json(json_decode((string) $response->content(), true), $response->status());
}

public function logout()
Expand Down

0 comments on commit ca4c531

Please sign in to comment.