2016-01-22 185 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()在一個地方,現在它的工作正常。 [解決了]。

相關問題