Laravel JWTAuth::attempt($credentials) 在 Ubuntu 服务器中不工作

Laravel JWTAuth::attempt($credentials) not working in Ubuntu Server

问题:JWTAuth::attempt($credentials) 在我的 windows 本地机器上工作。当用户通过提供凭据(用户名和密码)调用 /userLogin 时,它会验证凭据并为用户创建令牌。但是相同的代码:JWTAuth::attempt($credentials) 在我将项目部署到 Ubuntu 服务器时 NOT 工作。

描述: 我正在使用 tymondesigns/jwt-auth 在我的 Laravel 5.1 项目中实施 JSON Web Tokens。我构建了 2 个 REST API:/userRegister 和 /userLogin。

这是 /userRegisterUserController.php 中的代码:

public function userRegister(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email' => 'required',
        'username' => 'required',
        'password' => 'required',
        'country' => 'required',
        'zip_code' => 'required',
        'date_time' => 'required',
        'gender' => 'required',
    ]);

    try
    {
        if ($validator->fails())
            throw new Exception($validator->errors(), 409);

        $name = $request->input('name');
        $email = $request->input('email');
        $username = $request->input('username');
        $password = bcrypt($request->input('password'));
        $country = $request->input('country');
        $zip_code = $request->input('zip_code');
        $fb_login = $request->input('fb_login');
        $registration_date = $request->input('date_time');
        $gender = $request->input('gender');

        $user_email = UserProfile::where('email','=',$email)->first();
        if($user_email!=null)
        {
            throw new Exception("User with this email is already registered");
        }

        $check_username = UserProfile::where('username','=', $username)->first();
        if ($check_username != null)
        {
            throw new Exception("User with this username is already registered. Please use different username");
        }

        $saveUser = new UserProfile();
        $saveUser->name=$name;
        $saveUser->email=$email;
        $saveUser->username=$username;
        $saveUser->password=bcrypt($password);
        $saveUser->country=$country;
        $saveUser->zipcode=$zip_code;
        $saveUser->gender=$gender;
        $saveUser->fb_login=$fb_login;
        $saveUser->registration_date=$registration_date;

        $build_trial_date = new Carbon($registration_date);
        $trial_end_date = $build_trial_date->addDays(30);
       
        $saveUser->trial_end_date=$trial_end_date;
        $result = $saveUser->save();
        if (!$result)
            throw new Exception("Error in registration. Please try again.");

        $user_id = $saveUser->id;
        $loginUser = UserProfile::find($user_id);
        $loginUser->update(['logged_in' => 1]);

        return ResponseService::buildSuccessResponse("User registered successfully");
    }
    catch(Exception $e)
    {
        $data = [
            'error' => true,
            'result'  => [
                'status_code'   => $e->getCode(),
                'message'       => $e->getMessage(),
            ]
        ];
        $result = ResponseService::buildFailureResponse($data);
        return $result;
    }
}

如您所见,我在保存密码之前使用 bcrypt()

现在,这是 /userLoginUserController 中的代码:

public function userLogin(Request $request)
{
    // grab credentials from the request
    $credentials = $request->only('username', 'password');
    Log::info("username and password obtained from Request: ".json_encode($credentials));

    try
    {
        if(JWTAuth::attempt($credentials)) // For Logging
        {
            Log::info("$ token = JWTAuth::attempt $ credentials Result: TRUE");
        }else{
            Log::info("$ token = JWTAuth::attempt $ credentials Result: FALSE");
        }

        // attempt to verify the credentials and create a token for the user
        if (! $token = JWTAuth::attempt($credentials))
        {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }

    Log::info("Generated token is: ".json_encode(compact('token')));
    // all good so return the token
    return response()->json(compact('token'));
}

帮我解决这个无声无息的隐藏错误。 TIA.

更多详情: 我还在 windows 和 ubuntu 服务器中交叉检查了 jwt.php 文件。他们有相同的配置。另外,我在 ubuntu 服务器上使用了 php artisan jwt:generate

再过一遍代码终于解决了问题。幸好我注意到了!这是我犯的错误。在某个时间点,我添加了这样的代码 bcrypt()

$password = bcrypt($request->input('password')); 

但在将 $password 保存到数据库之前我已经在使用 bcrypt()。

$saveUser = new UserProfile();
$saveUser->password=bcrypt($password);

因为它在保存到数据库之前加密了两次,所以当我调用 /userLogin api 时,JWTAuth::attempt($credentials) 正在检查凭据并且它无法验证。所以我在一个地方删除了 bcrypt(),现在它工作正常。 [已解决].