2016-01-22 187 views
0

问题:JWTAuth :: attempt($ credentials)正在我的windows本地机器上工作。当用户调用通过提供凭据(用户名密码&)/ USERLOGIN,它验证凭据,并为用户创建一个令牌。但是,同样的代码:JWTAuth ::尝试($凭证)当我到Ubuntu服务器部署该项目的工作。

描述: 我使用tymondesigns/jwt-auth在我Laravel 5.1项目实施JSON网络令牌。我已经建立2的REST API:/ userRegister &/USERLOGIN。

这里是UserController.php为/userRegister代码:

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')); 
} 
  • /放在userRegistration API是工作无论是在我的Windows本地M/C以及 在Ubuntu的服务器。此外bcrypt()密码在 工作吧。

  • 但是,/ userLogin正在我的Windows本地m/c工作,但不工作在 Ubuntu服务器。

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

更多详情: 我也在Windows和ubuntu服务器上交叉检查了jwt.php文件。他们有相同的配置。此外,我已经在ubuntu服务器上使用php artisan jwt:生成

回答

1

我终于可以解决问题了,当我再次通过代码。幸运的是我可以听到这些!这是我犯的错误。在某个时间点我加入这个代码bcrypt()这样的:

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

但我已经使用节省$密码进入数据库之前bcrypt()。

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

因为它保存到数据库中,当我打电话/ USERLOGIN API之前加密两次,JWTAuth ::尝试($凭证)被检查credintials,这是无法验证。所以我删除了bcrypt()在一个地方,现在它的工作正常。 [解决了]。

相关问题