我如何使用jwt.auth middleware将令牌传递给标头,而不是正文:laravel 5.6

时间:2019-02-19 11:03:42

标签: php laravel laravel-5 jwt laravel-5.6

我使用邮递员来测试我的api,因此,当我在标头中发送令牌时,没有提供令牌,但是当我以json格式将其以原始形式传递给正文时,则获得成功结果,所以我想获得成功结果当我在标头中传递令牌时

我如何进行此更改?

这是我的Postcompanies控制器

class CompaniesController extends Controller
{
    public function index(Request $request)
    {
        # code...
        //  $Ads = ads::all();
        //  return $this->sendResponse($Ads->toArray(), 'Ads read succesfully');
        // This is the name of the column you wish to search
        $input = $request->all();
        $validator =    Validator::make($input, [
            'user_id'=> 'required'
        ] );

        $Companies = Companies::where('user_id','=', $request->user_id)->first();

        return response()->json(['Companies'=>$Companies]);
    }

    public function stockcompanies (Request $request){
        $input = $request->all();

        $validator =    Validator::make($input, [
            'title'=> 'required',
            'description'=> 'required',
            'logo_path'=> 'image|nullable|max:1999'
        ] );


        $user_id = Auth::id();
        if($request->hasFile('logo_path')){
            // Get filename with the extension
            $filenameWithExt = $request->file('logo_path')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            // Get just ext
            $extension = $request->file('logo_path')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore= $filename.'_'.time().'.'.$extension;
            // Upload Image
            $path = $request->file('logo_path')->storeAs('public/cover_images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }
        if ($validator -> fails()) {
            # code...
            return response()->json($validator->errors());
        }

        //$Cards = CreditCards::create($input,$user_id);
        $companies = Companies::create([
            'title' => $request->get('title'),
            'description' => $request->get('description'),
            'logo_path' => $fileNameToStore,
            'user_id' => $user_id
        ]);
        return response()->json(['Companies'=>$companies]);
    }
}

这是我的api:

Route::group(['middleware' => ['jwt.auth']], function() {
        Route::post('postmycompanies', 'CompaniesController@stockcompanies');
        Route::get('test', function(){
        return response()->json(['foo'=>'bar']);
});

1 个答案:

答案 0 :(得分:1)

我解决了我的问题: 我使用自定义的中间件     

Map

接下来,我们需要注册我们的中间件。打开app / http / Kernel.php并添加以下内容:

namespace App\Http\Middleware;

use Closure;
use JWTAuth;
use Exception;
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;

class JwtMiddleware extends BaseMiddleware
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        try {
            $user = JWTAuth::parseToken()->authenticate();
        } catch (Exception $e) {
            if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){
                return response()->json(['status' => 'Token is Invalid']);
            }else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){
                return response()->json(['status' => 'Token is Expired']);
            }else{
                return response()->json(['status' => 'Authorization Token not found']);
            }
        }
        return $next($request);
    }
}

接下来,打开routes / api.php并添加以下内容:

[...]
protected $routeMiddleware = [
    [...]
    'jwt.verify' => \App\Http\Middleware\JwtMiddleware::class,
];
[...]