Laravel Passport上的invalid_credentials

时间:2018-12-01 15:24:06

标签: laravel-passport

我对护照有疑问。 我收到消息:

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

当我尝试注册或登录时。 但是用户仍然存储在数据库中。 这是我的代码: AuthServiceProvider:

<?php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Carbon\Carbon;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();

        Passport::tokensExpireIn(Carbon::now()->addMinutes(1));

        Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
    }
}
RegisterController:

<?php

namespace App\Http\Controllers\Api\Auth;

use Illuminate\Http\Request;
//use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
use App\User;
use Laravel\Passport\Client;

class RegisterController extends Controller
{


	private $client;

    	public function __construct(){

    		$this->client = Client::find(1);
    	}


    public function register(Request $request){


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


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


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


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

    	$proxy = Request::create('oauth/token', 'POST');

    	return Route::dispatch($proxy);

    }
}

LoginController:

<?php

namespace App\Http\Controllers\Api\Auth;

use Illuminate\Http\Request;
//use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Laravel\Passport\Client;

class LoginController extends Controller
{


	//use IssueTokenTrait;


	private $client;

	public function __construct(){

		$this->client = Client::find(1);
 	}


 	public function test(){

 		return 'Tu essaie de te loger !';
 	}



    public function login(Request $request){

    	$this->validate($request, [
    		'username' => 'required',
    		'password' => 'required'
    	]);


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


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

    	$proxy = Request::create('oauth/token', 'POST');

    	return Route::dispatch($proxy);


    }



    public function refresh(Request $request){

    	$this->validate($request, [
    		'refresh_token' => 'required'
    	]);


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


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

    	$proxy = Request::create('oauth/token', 'POST');

    	return Route::dispatch($proxy);

    }



    public function logout(Request $request){

    	$accessToken = Auth::user()->token();

    	DB::table('oauth_refresh_tokens')
    	->where('access_token_id', $accessToken->id)
    	->update(['revoked' => true]);

    	$accessToken->revoke();

    	return response()->json([], 204);

    }
}

因此,在寄存器的情况下,如果数据不好,我会得到正确的消息。 但是当它们很好时,我会收到以下两条消息:   “错误”:“ invalid_credentials”,      “ message”:“用户凭据不正确。” 登录情况相同。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我终于找到了。

我将解决方案放在这里,以防某人某天遇到同样的问题。

只需更改代码即可

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

在RegisterController的功能寄存器(请求$ request)中 通过:

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

,其他所有内容(包括登录名:)都将起作用。