2016-12-27 108 views
0

我使用Laravel 5.1.33與Dingo Api和JWT Auth,已經安裝了所有這些,但現在我很困惑,如果我需要做更多,例如我想要驗證一個用戶,因此用戶無法先訪問某些路由而不能先登錄。JWT Auth配置Dingo Api Laravel 5.1。*

我havethis修訂於api.php代碼:

'auth' => [ 
    'jwt' => 'Dingo\Api\Auth\Provider\JWT', 
], 

我很困惑,當談到這裏,添加此代碼,它到底做什麼?

app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) { 
    return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']); 
}); 

我已閱讀野狗/ API已經內置了tymondesigns/JWT-auth的支持,這是不是意味着我不需要編寫任何驗證代碼,或這是什麼意思?

誰能告訴我,如果我必須修改當前AuthController其在這一刻看起來如下:

<?php 

namespace App\Http\Controllers\Auth; 

use App\User; 
use Validator; 
use App\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 

class AuthController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Registration & Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users, as well as the 
    | authentication of existing users. By default, this controller uses 
    | a simple trait to add these behaviors. Why don't you explore it? 
    | 
    */ 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 

    /** 
    * Create a new authentication controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest', ['except' => 'getLogout']); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 
     return Validator::make($data, [ 
      'name' => 'required|max:255', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|confirmed|min:6', 
     ]); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 
     return User::create([ 
      'name' => $data['name'], 
      'email' => $data['email'], 
      'password' => bcrypt($data['password']), 
     ]); 
    } 
} 

如果是這樣,需要添加什麼方法? 它說Dingo支持內置的jwt auth,因此我決定使用這個包,不僅僅是這個原因,還有其他一些原因,比如變形金剛,比率限制等等......但是我仍然很困惑我是否需要額外編寫驗證用戶的代碼,因爲它已經在構建中支持...如果不是,我該如何登錄?我沒有聲明用於認證的路由,也沒有註冊用戶,我應該以某種方式將這些路由指向一些控制器,任何人都可以幫助解決這個問題?

回答

-1

look at this github project,你可以參考它的路線和控制器。

一些方法需要添加,例如,

  1. 登錄:用戶登錄拿到令牌。
  2. 刷新令牌:當令牌無效時。
+0

嘗試更多的解釋增加了答案,避免提供鏈接,因爲鏈接可能是一段時間 – xhulio

+1

GitHub的例子項目似乎是OAuth不智威湯遜中實現後不可用 – Sisir

0

下面是步驟:

步驟1:

打開應用\提供商\ AuthServiceProvider。以下代碼粘貼到引導方法

app('Dingo\Api\Auth\Auth')->extend('jwt', function ($app) { 

    return new \Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);   
}); 

步驟2

創建一個身份驗證控制器,其產生的身份驗證令牌,並將其返回

namespace App\Http\Controllers\Api; 

use Illuminate\Http\Request; 
use JWTAuth; 
use Tymon\JWTAuth\Exceptions\JWTException; 

class AuthenticateController extends ApiController 
{ 

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 $this->response->errorInternal('Could not create token'); 
    } 

    return $this->response->array([ 
     'token' => $token, 
     'expire_in' => \Carbon\Carbon::now()->addMinutes(config('jwt.ttl'))->format('Y-m-d H:i:s') 
    ]); 

} 

}

步驟3:

創建一個如下所示的根Api控制器。

namespace App\Http\Controllers\Api; 

use App\Http\Controllers\Controller; 
use Dingo\Api\Routing\Helpers; 


class ApiController extends Controller 
{ 
    use Helpers; 
} 

步驟4

現在,您已經準備好使用巴丁格JWT權威性。只需從ApiController類擴展您的控制器類即可。這必須是所有Api控制器的父代。

namespace App\Http\Controllers\Api; 


use App\Http\Requests\Request; 

class TestController extends ApiController 
{ 
    public function index(Request $request) 
    { 
     $this->auth; # Here Auth is the logged in user object 
     # to return pagination 
     return $this->response->paginator(User::paginate(10), new 
      UserTransformer()); 
     # to return a single Model instance 
     return $this->response->item($user, new UserTransformer()); 
     # to return error. Others error methods as well 
     return $this->response->errorInternal('Error Message'); 
     # to return a custom array 
     return $this->response->array([ 
      'status' => 200, 
      'message' => 'Msg' 
     ]); 
} 

}