2017-06-20 100 views
0

我使用Auth::attempt(['email' => $request->email_id, 'password' => $request->password])進行了檢查,它在web端工作,但在api端給出了錯誤。如何在流明API中進行身份驗證?

if (Auth::attempt(['email' => $request->email_id, 'password' => $request->password])) 
      dd("Successfully Authenticated "); 
       else dd("false"); 
+1

請學會申請[PSR1](http://www.php-fig.org/psr/psr-1/)和[PSR2](http://www.php-fig.org/psr/psr- 2 /)在你的代碼中 –

回答

0

使用JWT身份驗證來驗證您的流明API

composer require tymon/jwt-auth 

安裝下面的配置

https://github.com/tymondesigns/jwt-auth/wiki

下面身份驗證功能,幫助進行身份驗證並返回API令牌之後

use JWTAuth; 
use Tymon\JWTAuth\Exceptions\JWTException; 

class AuthenticateController extends Controller 
{ 
    public function authenticate(Request $request) 
    { 
     // grab credentials from the request 
     $credentials = $request->only('email', 'password'); 

     try { 
      // 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); 
     } 

     // all good so return the token 
     return response()->json(compact('token')); 
    } 
} 
相關問題