Code Lover
Code Lover

Reputation: 8378

Laravel Passport - Invalid credentials using grant_type is passowrd

I am having difficulty setting up Passport in Laravel 5.6. The first time when I followed this tutorial, I had implemented perfectly but now again when I am following than getting following error.

{
    "error": "invalid_credentials",
    "message": "The user credentials were incorrect."
}

I have tried out all possible solutions but none of them works. So thought to post it here.

Info: I am using iMac - High Sierra. storage directory has 777 permission. I have set using sudo chmod -R 777 storage command. However, when I checked the laravel.log file didn't have permission so I have grant 777 to it as well. still getting the error.

Laravel error log - laravel.log

local.ERROR: The user credentials were incorrect. {"exception":"[object] (League\\OAuth2\\Server\\Exception\\OAuthServerException(code: 6): The user credentials were incorrect. at /Users/username/Sites/mysite/vendor/league/oauth2-server/src/Exception/OAuthServerException.php:160)

My Implementation Steps

Route

Route::post( 'register', 'Api\Auth\RegisterController@register' );

RegisterController Class

namespace App\Http\Controllers\Api\Auth;

use App\User;
use function bcrypt;
use function dd;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Route;
use Laravel\Passport\Client;
use function response;

class RegisterController extends Controller
{

    private $client;

    public function __construct() {
        $this->client = Client::findOrFail(1);
    }

    public function register( Request $request ) {

        $this->validate( $request, [
            'name'     => 'required',
            'email'    => 'required|email|unique:users,email',
            'password' => 'required|min:6|confirmed',
        ] );

        $user = User::create( [
                                  'name'     => request( 'name' ),
                                  'email'    => request( 'email' ),
                                  'password' => bcrypt( 'password' )
                              ] );

        $params = [
            'grant_type'    => 'password',
            'client_id'     => $this->client->id,
            'client_secret' => $this->client->secret,
            'username'      => request( 'email' ),
            'password'      => request( 'password' ),
            'scope'         => '*'
        ];

        $request->request->add( $params );
        $proxy = Request::create( 'oauth/token', 'POST' );

        return Route::dispatch( $proxy );

    }
}

Upvotes: 1

Views: 2001

Answers (2)

hullabaloon
hullabaloon

Reputation: 664

You are hashing the word 'password' not the actual password coming from request.

You should use it like this:

bcrypt(request('password'))

Upvotes: 1

vetjurv4
vetjurv4

Reputation: 45

try to use hash::make function instead of bcrypt for your password when creating user like below

$user = User::create([
    'name' => request('name'),
    'email' => request('email'),
    'password' => Hash::make(request('password'))
    ]);

Upvotes: 0

Related Questions