简体   繁体   English

Laravel 5.6-Auth :: check()失败

[英]Laravel 5.6 - Auth::check() Failing

I used the laravel spatie backup in my system, and all my functions such as creating a new backup, deleting, and downloading are working locally. 我在系统中使用了laravel spatie备份,并且所有功能(如创建新备份,删除和下载)都在本地运行。 I tried to deploy my website on a free hosting, everything seems to work except the delete and download function. 我试图将我的网站部署在免费主机上,除删除和下载功能外,其他所有功能似乎都可以正常工作。 Upon investigating, I have seen that it fails because of the Middleware I have created for the download/delete route. 经过调查,我发现它由于我为下载/删除路由创建的中间件而失败。 Here's my StaffMiddleware where only accounts with the staff role can access it. 这是我的StaffMiddleware,只有具有staff角色的帐户才能访问它。

Middleware 中间件

public function handle($request, Closure $next)
{

        if(Auth::check())
        {
            if(Auth::user()->role == 'staff')
            {
                return $next($request);
            }
            else
            {
                return redirect('/'); 
            }
        }
        else
        {
            return redirect('/');
        }
}

Routes 路线

Route::get('backup/create', 'Admin\BackupController@create');
Route::get('backup/download/{file_name}', 'Admin\BackupController@download');
Route::get('backup/delete/{file_name}', 'Admin\BackupController@delete');

When I try to access the download function, it redirects to the homepage since the Auth::check() line fails in my middleware. 当我尝试访问下载功能时,由于中间件中的Auth :: check()行失败,因此它将重定向到主页。 Note that I am logged in and authenticated while accessing the download function. 请注意,我在访问下载功能时已登录并通过身份验证。 This only happens in the live server, but all of the code works locally. 这仅在实时服务器中发生,但是所有代码都在本地工作。 Can you please help me on this one? 你能帮我这个忙吗? Thanks! 谢谢!

can you try this 你可以试试这个吗

public function handle($request, Closure $next)
{
    $user = Auth::user();

    //dd($user); //debug if didn't work 

    if($user && $user->role == 'staff') // if your role is coming from relation then try `$user->role->name == 'staff'`
    {
       return $next($request);
    }

    return redirect('/');
}

I think you have to get the user from the request 我认为您必须从请求中吸引用户

public function handle($request, Closure $next)
{
    if ($request->user() && $request->user()->role == 'staff')) {
        return $next($request);
    }

    return redirect('/');
}

You can try this: 您可以尝试以下方法:

namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;

class AdminMiddleware {
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next,$guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect('admin/login');
            }
        }else{
            if( \Auth::user()->role =="admin" ){
                return $next($request);
            }
        }
        return redirect("admin/login");

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM