douqi1931 2018-04-15 16:39
浏览 146

Laravel Passport - 使用grant_type的凭据无效是密码

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

  • I run composer require laravel/passport
  • Added passport service provider in app.php Laravel\Passport\PassportServiceProvider::class,
  • Migrated the database php artisan migrate
  • Added Passport::routes()
  • Added use HasApiTokens to User model
  • In auth.php set gourds api driver to password
  • Passport client credentials php artisan passport:client --passoword
  • Passport keys php artisan passport:keys
  • Added route and register controller as below

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 );

    }
}
  • 写回答

1条回答 默认 最新

  • dongqi4085 2019-01-16 08:59
    关注

    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'))
        ]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100