简体   繁体   中英

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. 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.

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. 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");

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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