有關Laravel Passport認證的問題

艾尼亞爾發表於2021-07-02

有關Laravel Passport認證的問題

前後端分離專案想用Laravel Passporta來做後臺Api認證結果卡在Laravel Passport三天了,哪位大佬幫幫看看

不管怎麼做都不行,直接上程式碼
config/auth.php

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'admins',
            //'hash' => false,
        ],
//        'admin'=>[
//            'driver' => 'passport',
//            'provider' => 'admins',
//        ]
    ],
'providers' => [
  'users' => [
  'driver' => 'eloquent',
  'model' => App\Models\Admin\User::class,
 ],
 'admins'=>[
  'driver' => 'eloquent',
  'model' => App\Models\Admin\Admin::class,
 ]],

routes/admin.php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\Index;
use App\Http\Controllers\Admin\Auth;

Route::prefix('admin')->group(function () {

    Route::namespace('Admin')->group(function () {
        //登入
        Route::post('/login', [\App\Http\Controllers\Admin\AuthController::class, 'logins']);
        //驗證碼
       // Route::get('/captcha', [Auth::class, 'captcha'])->name('captcha');
        //需要驗證
        Route::middleware(['auth:api'])->group(function () {
            Route::get('/admins', [\App\Http\Controllers\Admin\AuthController::class,'admins']);
        });
    });
});
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\AdminRequest\AdminLoginRequest;
use Illuminate\Http\Request;
use App\Models\Admin\Admin;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
  //
  public function logins(AdminLoginRequest $loginRequest)
 {  $data = $loginRequest->only('admin_name', 'password');

  $admin = Admin::where('admin_name', $data['admin_name'])->first();
  if ($admin->id!==0) {
  if (!Hash::check($data['password'], $admin->password)) {
  return $this->failed('賬號或密碼錯誤', 401);
 }  if ($admin->is_lock) {
  return $this->failed('當前賬號已鎖定,若需要請和管理員聯絡', 401);
 }  $admin['token'] = $admin->createToken('MyApp')->accessToken;
  return $this->success($admin);
 }  return $this->failed('賬號或密碼錯誤', 401);
 }     
 public function admins(Request $request){
  dd($request->user());
 }}


postman登入結果如下:

Laravel Passport認證

想獲取使用者資訊結果如下

Laravel Passport認證

哪位大佬幫幫我,在下感激不盡

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章