(各バージョン)
laravel:6.18.40
PHP:7.2.31
nginx:1.19.0"
(やりたいこと)
apiを呼び出した時にlaravel_sessionの値が変更されないようにしたい。
ショッピングサイトを作成しているのですが会員登録をしていなくても購入できるように
sessionIDで購入者か判断したいのでsessionIDが変更されないようにしたいです。
(現状)
フロント側でaxiosでapiを呼び出しコントローラーで処理が行われレスポンスが返って来ています。
デベロッパーツールで確認してみるとリクエストとレスポンスで共にcookieはあるのですがcookieの中身が変わってしまっています。(全てのapi)
以下に例として実装しているコードを記載します。内容としてはテーブルに登録されている商品を取得してページに反映させています。
↓フロント
vue
1<template> 2 ...省略 3</template> 4<script> 5 6export default{ 7 data(){ 8 return { 9 products: [] 10 } 11 }, 12 methods: { 13 async fetchProduct(){ 14 const response = await axios.get(`/api/product/?page=${this.page}`); 15 16 this.products = response.data.data; 17 } 18 }, 19 watch: { 20 $route: { 21 async handler(){ 22 await this.fetchProduct(); 23 }, 24 immediate: true 25 } 26 } 27} 28</script>
↓routes
php
1<?php 2 3use Illuminate\Http\Request; 4 5/* 6|-------------------------------------------------------------------------- 7| API Routes 8|-------------------------------------------------------------------------- 9| 10| Here is where you can register API routes for your application. These 11| routes are loaded by the RouteServiceProvider within a group which 12| is assigned the "api" middleware group. Enjoy building your API! 13| 14*/ 15 16// Route::middleware('auth:api')->get('/user', function (Request $request) { 17// return $request->user(); 18// }); 19 20//トップページ(商品一覧) 21Route::get('/product', 'Auth\ProductController@GetProductList')->name('product');
↓コントローラー(テーブルからデータの一覧を取得しjsonで返却しています。paginate使用しています。)
php
1<?php 2 3namespace App\Http\Controllers\Auth; 4 5use App\Http\Controllers\Controller; 6use Illuminate\Http\Request; 7use App\Models\Product; 8use Illuminate\Support\Facades\Auth; 9 10class ProductController extends Controller 11{ 12 /** 13 * Display a listing of the resource. 14 * 15 * @return \Illuminate\Http\Response 16 */ 17 public function GetProductList(Product $product) 18 { 19 $getData = null; 20 if(Auth::check()){ 21 $getData = $product 22 ->leftjoin('favorites', function($join){ 23 $join->on('product.id', '=', 'favorites.product_id') 24 ->where('favorites.login_id', '=', Auth::user()->login_id); 25 }) 26 ->select('product.*','favorites.login_id') 27 ->orderBy('product.created_at', 'asc') 28 ->paginate(); 29 }else{ 30 $getData = $product->orderBy('created_at', 'asc') 31 ->paginate(); 32 } 33 return $this->jsonResponse($getData); 34 } 35}
↓kernel.php
php
1<?php 2 3namespace App\Http; 4 5use Illuminate\Foundation\Http\Kernel as HttpKernel; 6 7class Kernel extends HttpKernel 8{ 9 /** 10 * The application's global HTTP middleware stack. 11 * 12 * These middleware are run during every request to your application. 13 * 14 * @var array 15 */ 16 protected $middleware = [ 17 \App\Http\Middleware\TrustProxies::class, 18 \App\Http\Middleware\CheckForMaintenanceMode::class, 19 \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 20 \App\Http\Middleware\TrimStrings::class, 21 \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 22 ]; 23 24 /** 25 * The application's route middleware groups. 26 * 27 * @var array 28 */ 29 protected $middlewareGroups = [ 30 'web' => [ 31 \App\Http\Middleware\EncryptCookies::class, 32 \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 33 \Illuminate\Session\Middleware\StartSession::class, 34 // \Illuminate\Session\Middleware\AuthenticateSession::class, 35 \Illuminate\View\Middleware\ShareErrorsFromSession::class, 36 \App\Http\Middleware\VerifyCsrfToken::class, 37 \Illuminate\Routing\Middleware\SubstituteBindings::class, 38 ], 39 40 'api' => [ 41 'throttle:60,1', 42 'bindings', 43 ], 44 ]; 45 46 /** 47 * The application's route middleware. 48 * 49 * These middleware may be assigned to groups or used individually. 50 * 51 * @var array 52 */ 53 protected $routeMiddleware = [ 54 'auth' => \App\Http\Middleware\Authenticate::class, 55 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 56 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 57 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, 58 'can' => \Illuminate\Auth\Middleware\Authorize::class, 59 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 60 'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class, 61 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 62 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 63 ]; 64 65 /** 66 * The priority-sorted list of middleware. 67 * 68 * This forces non-global middleware to always be in the given order. 69 * 70 * @var array 71 */ 72 protected $middlewarePriority = [ 73 \Illuminate\Session\Middleware\StartSession::class, 74 \Illuminate\View\Middleware\ShareErrorsFromSession::class, 75 \App\Http\Middleware\Authenticate::class, 76 \Illuminate\Routing\Middleware\ThrottleRequests::class, 77 \Illuminate\Session\Middleware\AuthenticateSession::class, 78 \Illuminate\Routing\Middleware\SubstituteBindings::class, 79 \Illuminate\Auth\Middleware\Authorize::class, 80 ]; 81} 82
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/01/05 12:44
2021/01/05 13:20
2021/01/05 15:36
2021/01/06 00:13
2021/01/06 01:22