Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

laksh's avatar
Level 1

Middleware not working

i am making a page which i want that nobody can access that page without login also not through url so i am using middleware for that but its not working it will not redirecting me to the page which url i passed can anybody solve my problem

0 likes
27 replies
Nakov's avatar

It is always good if you share the code that you tried so someone can guide you.

laksh's avatar
Level 1

okk here's my route code Route::middleware('newmiddleware')->group(function(){ Route::get('/dashboard', function(){ return view('dashboard'); }); });

and middleware

protected function redirectTo($request) { $user = User::where('name',$request->name)->first();

    if($user = ''){
        return route('login');
    }
}
RayC's avatar

@laksh One thing is this should be:

if($user == ''){
        return route('login');
    }

Using $user = '' is setting $user to ''

You could use:

if(Auth::guest()){
        return route('login');
    }
laksh's avatar
Level 1

it is also not working login page not redirecting to dashboard

RayC's avatar

@laksh You need to add else to the condition to send them to the dashboard. Right now, your just checking to see if they are a guest and if not, you're not doing anything.

    if($user == ''){
        return route('login');
    }
return redirect()->route('dashboard');

Assuming you've named your route 'dashboard'

You should post all your code properly formatted for any help other than what has been given here.

Sinnbeck's avatar

If the user is not found, it will not return an empty string, it will return null

if (!$user) {
   return redirect()->route('login');
}
return $next($request);

But there is a built in middleware for this named auth that you can just use

laksh's avatar
Level 1

getting error after this

Error : Header may not contain more than a single header, new line detected

laksh's avatar
Level 1

namespace App\Http\Middleware;

use App; use Illuminate\Auth\Middleware\Authenticate as Middleware; use App\Models\User; use Illuminate\Support\Facades\Auth;

class NewMiddle extends Middleware {

protected function redirectTo($request)
{
    $user = User::where('name',$request->name)->first();
    
    if(Auth::check()){
        return route('login');
    }
        
    return view('dashboard');
    
}
Sinnbeck's avatar

@laksh Return a redirect

return redirect()->route('login');

And dont return views from middleware. Either return $next($request); or a redirect. Views are for controllers.

laksh's avatar
Level 1

Too few arguments to function App\Http\Middleware\NewMiddle::redirectTo(), 1 passed in D:\xampp\htdocs\demo\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php on line 83 and exactly 2 expected

getting error after use redirect()

Sinnbeck's avatar

@laksh Ah ok didnt see you had that. Try returning the route name then, but remove the view like I suggested.

Snapey's avatar

your concept is messed up. Read the docs regarding authentication and authorisation

you are expecting the url to contain a name, and then provided the database contains that name then you are happy?

What are you trying to do exactly????

laksh's avatar
Level 1

i want to pass the name of route on which middleware is using

laksh's avatar
Level 1

i just wanted to do that i made a page and just wanted that the page cannot accessed by anybody before logging in

laksh's avatar
Level 1

But auth middleware doesn't redirecting me to dashboard infact it is redirecting me to login again after login

Snapey's avatar

@laksh then you have messed up the standard authentication

Are you sure you are actually logged in?

Sinnbeck's avatar

@laksh If you are logged in, it will redirect you to the page you are trying to visit. If not, it will redirect you to login

laksh's avatar
laksh
OP
Best Answer
Level 1

here is my login code can you please tell what's wrong in this

public function show_login_form(){ return view('maincontent.login'); }

public function process_login(Request $request)
{
    $request->validate([
        'name' => 'required',
        'password' => 'required'
    ]);
    
    $credentials = $request->except(['_token']);
    
    $user = User::where('name',$request->name)->first();
    
    if ($user != '') {
        auth()->attempt($credentials);
        return response([ [1] ]);
        
    }else{
        
        return response([ [0] ]);
    }
    
}
Snapey's avatar

@laksh you are doing that compare user to an empty string again...

If you are not confident in your PHP skills, the last thing you should be doing is writing your own authentication - especially as it is handled out of the box for you

laksh's avatar
Level 1

how do i use standard authentication for login

Snapey's avatar

@laksh if you have not done much with the project, install one of the starter kits such as breeze

huyvuong's avatar
    public function handle(Request $request, Closure $next)
    {
        $user = $request->user('admin');
        if (!$user) {
            return redirect()->route('admin.login');
        }
        if (!in_array($user->role, [Roles::ADMINISTRATOR->value, Roles::STAFF->value])) {
            return redirect()->route('admin.home');
        }
        return $next($request);
    }

This is my middleware for admin page. I think you just make it looks more complicated

Snapey's avatar

@huyvuong your doesn't really make any sense either. Middleware should be used as a guard with the exception causing perhaps a redirect, and normal flow through the middleware.

Please or to participate in this conversation.