iT邦幫忙

2025 iThome 鐵人賽

DAY 19
0
Modern Web

Laravel 是甚麼系列 第 19

要有token才可以

  • 分享至 

  • xImage
  •  

修改AuthController 程式碼:
https://ithelp.ithome.com.tw/upload/images/20250825/20119035D6uGwldxKP.png
到POSTMAN設定,每次都不一樣~
TOKEN 是eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9....
https://ithelp.ithome.com.tw/upload/images/20250825/20119035Octqp6vvaC.png

:貼上TOKEN
https://ithelp.ithome.com.tw/upload/images/20250825/20119035Udl7EEsyo5.png
設定 路由:
https://ithelp.ithome.com.tw/upload/images/20250825/20119035fnNNkV7MbS.png
設定完要讓伺服器重跑,才可以再測試成功:
確保你的 AuthController 被正確加載了,嘗試運行:

sh
複製
編輯
php artisan route:clear
php artisan config:clear
php artisan cache:clear
重啟 Laravel 服務器:
php artisan serve

設定測試網址: localhost:8000/user
https://ithelp.ithome.com.tw/upload/images/20250825/20119035n5hYqKnsVu.png

可以做登出的
修改AuthController 程式碼
https://ithelp.ithome.com.tw/upload/images/20250825/20119035fZanhhSWcO.png
增加路由
https://ithelp.ithome.com.tw/upload/images/20250825/20119035T4uGVUxoSL.png
POSTMAN測試
帶入token
修改測試網址: localhost:8000/logout
https://ithelp.ithome.com.tw/upload/images/20250825/2011903595e4kzlnCQ.png

到localhost:8000/user顯示
https://ithelp.ithome.com.tw/upload/images/20250825/20119035I0k1MFsf8E.png
oauth_access_tokens資料庫查看
https://ithelp.ithome.com.tw/upload/images/20250825/20119035PypNxHLOzb.png
修改Authenticate 程式碼
https://ithelp.ithome.com.tw/upload/images/20250825/201190355RwxAhjIbJ.png
修改前程式碼:

<?php

namespace App\Http\Middleware;

use Illuminate\Auth\Middleware\Authenticate as Middleware;

class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('login');
        }
    }
}

修改後程式碼:
https://ithelp.ithome.com.tw/upload/images/20250827/20119035AJG6dqw5bv.png
到 Handler程式碼加入套件:
https://ithelp.ithome.com.tw/upload/images/20250827/20119035Sjz8kEPj1J.png
修改後程式碼:
https://ithelp.ithome.com.tw/upload/images/20250827/201190351a9QT6ZJH2.png
下面增加程式碼:
https://ithelp.ithome.com.tw/upload/images/20250827/20119035ZsTE1wgjUx.png
POSTMAN測試
https://ithelp.ithome.com.tw/upload/images/20250827/20119035Inet1Pr5BZ.png
再測試登入一次,拿到新的token
POSTMAN用POST貼email:joe@gmail.com
password:12345678 在localhost:8000/login
https://ithelp.ithome.com.tw/upload/images/20250827/20119035BxrUZKMfrG.png
在GET在貼上新的TOKEN成功登入
在localhost:8000/user
https://ithelp.ithome.com.tw/upload/images/20250827/20119035txCMZ3iZ5i.png
把token隨便移掉一個字顯示
https://ithelp.ithome.com.tw/upload/images/20250827/201190356f5BATDpYu.png
在購物車加上 user id
先用指令: php artisan make:migration add_user_id_to_carts
https://ithelp.ithome.com.tw/upload/images/20250827/20119035qBunnps71d.png
修改AddUserIdToCarts 程式碼

程式碼修改前:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUserIdToCarts extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('carts', function (Blueprint $table) {
            //
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('carts', function (Blueprint $table) {
            //
        });
    }
}

https://ithelp.ithome.com.tw/upload/images/20250827/20119035XvIA02R5JE.png
先把users資料表把id原來是1的改成0

程式碼修改後:
https://ithelp.ithome.com.tw/upload/images/20250827/20119035FvZMYzRV4p.png
在terminal下指令php artisan migrate
https://ithelp.ithome.com.tw/upload/images/20250827/20119035lXHIclpnhf.png
到carts資料表查看多了user_id欄位
https://ithelp.ithome.com.tw/upload/images/20250827/20119035Tpdy63xPez.png
按carts資料表查看 把手
https://ithelp.ithome.com.tw/upload/images/20250827/20119035zgDx99XlMW.png
選Foreign key都有加上
https://ithelp.ithome.com.tw/upload/images/20250827/20119035lKWmwoCfT4.png
修改 Cart程式碼:
修改前程式碼:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Cart extends Model
{
    use HasFactory;

    public function cartItems()
    {
        return $this->hasMany(CartItem::class);
    }

}

https://ithelp.ithome.com.tw/upload/images/20250827/20119035b19NZx9vQQ.png

修改後程式碼: 加入protected $guarded = [''];

https://ithelp.ithome.com.tw/upload/images/20250827/20119035A0tjM4XiW9.png
修改路由:
修改前程式碼:
https://ithelp.ithome.com.tw/upload/images/20250827/20119035WgP4hWYe8K.png
修改後程式碼:
https://ithelp.ithome.com.tw/upload/images/20250827/20119035Q1Rz6TzNgJ.png
修改CartController 程式碼:
程式碼修改前:
public function index()
{
$cart = Cart::with(['cartItems'])->firstOrCreate();

    return response($cart);
}

https://ithelp.ithome.com.tw/upload/images/20250827/20119035M3Bh6R6wIZ.png
程式碼修改後:
https://ithelp.ithome.com.tw/upload/images/20250827/20119035hsCl9HV21E.png
用POSTMAN測試
POST在localhost:8000/signup
貼上name:joe2
email:joe2@gmail.com
password:12345678
password_confirmation:12345678
https://ithelp.ithome.com.tw/upload/images/20250827/20119035LtTaQ4H08e.png
測試登入
用POST在localhost:8000/login
貼上email:joe2@gmail.com
password:12345678
拿到新的TOKEN
https://ithelp.ithome.com.tw/upload/images/20250827/20119035OSpJ3wamXB.png
把token貼到
GET在localhost:8000/carts
https://ithelp.ithome.com.tw/upload/images/20250827/20119035oCFXiX5eli.png
先加入資料
https://ithelp.ithome.com.tw/upload/images/20250827/20119035ojG9Cv29Ib.png
查看
https://ithelp.ithome.com.tw/upload/images/20250827/20119035awljzE0NdO.png

大家明天見~


上一篇
會員註冊開始~
下一篇
測試CRUD含登入
系列文
Laravel 是甚麼30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言