Laravel JWT Auth不能正常工作,$ token为false / true

时间:2019-04-22 22:30:50

标签: php laravel laravel-5.7

我正在使用Laravel 5.7和“ tymon / jwt-auth”:“ 1.0.0-rc.3”进行身份验证。注册用户后,当我尝试强化他的身份时,我总是得到布尔值:true或false,而不是令牌。 我尝试了在互联网上找到的所有“修复程序”,但没有任何效果。我想念什么?

这是注册方法:

protected function create(array $data)
{

    if ($data['legitimation_id'][0] === 'E') {
        $user_type = 1;
    }
    else if ($data['legitimation_id'][0] === 'P') {
        $user_type = 2;
    }

    $data['user_type'] = $user_type;
    $data['password'] = Hash::make($data['password']);

    $user = User::create($data);
    LegitimationController::delete($data['legitimation_id']);

    return $user;
}

public function register(Request $request) {

    $data = $request->all();

    if ($this->validator($data)->fails()) {
        return $this->sendError('Validation Error.', $this->validator($data)->errors());
    }

    if (mb_substr($data['legitimation_id'], 0, 1) != 'P' && mb_substr($data['legitimation_id'], 0, 1) != 'E' ) {
        return $this->sendError('Validation Error.', array("legitimation_id" => 'Fail'));
    };

    $user = $this->create($data);

    $success = ['first_name' => $user['first_name'], 'last_name' => $user['last_name']];

    return $this->sendResponse($success, 'Succes');
}

这是登录方法:

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');

    $jwt_token = null;

    if (!$jwt_token = auth('api')->attempt($credentials)) {
        $this->sendError("Unauthorized", ['error' => 'Invalid Email or Password'], 401);
    }

    $this->sendResponse(['token' => $jwt_token], "Succes");
}

api.php

Route::group([

'middleware' => 'cors',
'prefix' => 'v1'

], function () {
    Route::post('register', 'Auth\RegisterController@register');
    Route::post('login', 'Auth\LoginController@login');
});

auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
],

0 个答案:

没有答案