Laravel Passport没有身份验证 [英] Laravel Passport no authentication

查看:69
本文介绍了Laravel Passport没有身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的laravel应用出现一些奇怪的问题.我已经使用Passport创建了身份验证系统,并根据需要安装了所有内容,但无法对用户进行身份验证.当我登录时,它可以正确创建令牌,并且一切似乎都可以正常工作,但是当我想在邮递员中添加身份验证时,它始终为未身份验证".Laravel 8就是这种情况,我已经从Laravel 7应用程序中复制了所有内容,并且运行良好.我将为您提供一些代码.

I have some strange problems with my laravel app. I have created an authentication system with Passport, I have installed everything as needed but I can't authenticate the user. When I log in it's creating a token correctly and everything seems to work fine, but when I want to add authentication in postman, it's always "Not Authenticated". This is happening with Laravel 8, I have copied everything from my Laravel 7 app and there is working pretty fine. I will provide you with some code.

这是我创建的用于检查身份验证的中间件:

This is middleware that I created to check authentication:

public function handle(Request $request, Closure $next)
    {
        if(!Auth::id()){
            return response()->json(['response' => false, 'status' => 403, 'message' => 'Not Authenticated'], 403);
        } else {
            return $next($request);
        }
    }

这里是内核

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        // \App\Http\Middleware\TrustHosts::class,
        \App\Http\Middleware\TrustProxies::class,
        \Fruitcake\Cors\HandleCors::class,
        \App\Http\Middleware\PreventRequestsDuringMaintenance::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\UserSecurity::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'check.auth' => \App\Http\Middleware\UserAuth::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    ];
}

这是application.blade.php

And here is application.blade.php

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <!-- <link rel="icon" href="<%= BASE_URL %>favicon.ico"> -->

    <title>Vuexy  - Vuejs, HTML & Laravel Admin Dashboard Template</title>
    <!-- Styles -->
    <link rel="stylesheet" href="{{ asset(mix('css/main.css')) }}">
    <link rel="stylesheet" href="{{ asset(mix('css/iconfont.css')) }}">
    <link rel="stylesheet" href="{{ asset(mix('css/material-icons/material-icons.css')) }}">
    <link rel="stylesheet" href="{{ asset(mix('css/vuesax.css')) }}">
    <link rel="stylesheet" href="{{ asset(mix('css/prism-tomorrow.css')) }}">
    <link rel="stylesheet" href="{{ asset(mix('css/app.css')) }}">
    <link
    rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
  />

    <!-- Favicon -->
    <link rel="shortcut icon" href="{{ asset('images/logo/favicon.png') }}">
  </head>
  <body>
    <noscript>
      <strong>We're sorry but Vuexy  - Vuejs, HTML & Laravel Admin Dashboard Template doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app">
    </div>

    <!-- <script src="js/app.js"></script> -->
    <script src="{{ asset(mix('js/app.js')) }}"></script>

  </body>
</html>

这是路由器:

Route::group(['middleware' => 'auth:api', 'middleware' => 'auth:api'], function() {
        Route::get('profile', [UserController::class, 'getUserDetails'])->name('profile');
        Route::post('logout', [UserController::class, 'destroySession'])->name('logout');
    });

现在我注意到的是,当我使用auth:API而不是我的中间件时,出现如下错误:

Now what I have noticed is, when I use auth:API and not my middleware, I'm getting error like:

RuntimeException: Session store not set on request. in file C:\xampp\htdocs\sss\vendor\laravel\framework\src\Illuminate\Http\Request.php on line 483

有什么想法吗?

推荐答案

您的条件不正确;您应该改写:

Your condition was wrong; you should write instead:

public function handle(Request $request, Closure $next) { 
   if(auth()->check()){ 
       return $next($request); 
    }
    return response()->json(['response' => false,'status' => 403,'message' => 'Not Authenticated'], 403);
}

这篇关于Laravel Passport没有身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆