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

vandan's avatar
Level 13

how to pass id into middleware in laravel

here is my middleware

 public function handle($request, Closure $next)
    {
        if(\Auth::check() && \Auth::signup()->verify())
        {            
                return $next($request);             
        }
        return \Redirect::route('login');
    }

here is my route

Route::group(['middleware'=>'signup'],function()
{
    Route::get('otp/{id}',array('as'=>'otp.view','uses'=>'Signup\SignupController@otp_view'));

}

how to pass register id in my middleware thank you

0 likes
30 replies
vandan's avatar
Level 13

i try but error Trying to get property of non-object

if(\Auth::check() && \Auth::signup()->verify())
    {         
            $request->route()->parameter('id');
            return $next($request);             
    }
hjortur17's avatar

You have $request. Maybe you can do something like this $request->id

or for example

public function handle($request, Closure $next, $id)
    {
        if (auth()->check() == $id) {
            return $next($request);
        }

        return redirect('login');
    }
vandan's avatar
Level 13

@HJORTUR17 - i try this but error Missing argument 3 for App\Http\Middleware\SignupMiddleware::handle(), called in D:\laragon\www\employeesystem\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php on line 149 and defined

public function handle($request, Closure $next,$id)
    {
        if(\Auth::check()==$id && \Auth::signup()->verify())
        {            
                // return $next($request);
                return \Redirect::route('login');
        }
        return \Redirect::route('otp.view',$id);
    }
Snapey's avatar

Why?

What is it you are hoping to get from the request and what will you do with it?

Snapey's avatar

Why are you trying to compare Auth::check (returns a boolean) with an ID?

1 like
vandan's avatar
Level 13

@SNAPEY - i try to use middleware

i have signup form which field like id,name,otp when user signup then get redirect to otp page and enter otp but whenever i try in url like

http://localhost/employeesystem/public/otp/1

so i change value like otp/2 so redirect id 2 page so i used middleware but i cant run it

error like

Missing argument 3 for App\Http\Middleware\SignupMiddleware::handle(), called in D:\laragon\www\employeesystem\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php on line 149 and defined

Snapey's avatar

What are you trying to do? Your middleware makes no sense.

vandan's avatar
Level 13

@SNAPEY - wait i will send a code

middleware code

public function handle($request, Closure $next,$id)
    {
        if(\Auth::check() && \Auth::signup()->verify())
        {            
                // return $next($request);
                return \Redirect::route('login');
        }
        return \Redirect::route('otp.view',$id);
    }

here is my signup model

public function Verify($id)
    {
        $id = Signup::where('id','=',$id)->first();
        return($this->verify=='yes');
    }

here is my controller

public function otp_view($id)
    {   
        $otp=Signup::where('id','=',$id)->first();
    return view('otp',compact('otp'));
    }

here is error "Missing argument 3 for App\Http\Middleware\SignupMiddleware::handle(), called in D:\laragon\www\employeesystem\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php on line 149 and defined

Sergiu17's avatar

@VAN1310 - OK, delete 3rd parameter from your handle function, and it will work,

return \Redirect::route('otp.view', auth()->id); // auth()->id, instead if $id
1 like
JAY_CHAUHAN's avatar

when you enter in controller the first check

Determining If The Current User Is Authenticated

To determine if the user is already logged into your application, you may use the check method on the Auth facade, which will return true if the user is authenticated:

use Illuminate\Support\Facades\Auth;

if (Auth::check()) { // The user is logged in... }

try this,

2 likes
vandan's avatar
Level 13

@SERGIU17 - ok thanks but still error

Undefined property: Illuminate\Auth\AuthManager::$id

Sergiu17's avatar

@VAN1310 - read this

if(\Auth::check() && \Auth::signup()->verify())
{            
                return \Redirect::route('login'); 
}
Sergiu17's avatar

@VAN1310 - ok,

if ( user is logged in && signed in user has verified account ) {
    redirect to login page
}

Why redirect authenticated user to login page?

vandan's avatar
Level 13

@SERGIU17 - but i try this condtion false redirect login page

if(\Auth::check() && \Auth::signup()->verify())
    {            
        // return $next($request);
        return \Redirect::route('otp.view',auth()->id);
    }
    return \Redirect::route('login');
vandan's avatar
Level 13

@SERGIU17 - i cant understand here my code

Trying to get property of non-object

if(Auth::check() && Auth::signup()->verify())
    {            
        // return $next($request);
        return \Redirect::route('otp.view',auth()->id);
    }
vandan's avatar
Level 13

@SERGIU17 - #41 D:\laragon\www\employeesystem\public\index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) #42 {main}

can you please more realiable solution

Trying to get property of non-object

    $response->headers->setCookie(  //this line error 
        new Cookie(
            'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']),
            $config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null
        )
    );
Snapey's avatar

You should check the session for the presence of OTP being passed

Then in the middleware, if the session does not contain that confirmation then you direct them to the OTP page.

On the OTP page, you accept the code, and if it passes inspection, set the OTP checked flag in session.

On the next request then OTP check will be ok and the middleware just passes to the next in the stack.

All routes except login and the OTP page should be protected by the OTP middleware.

The above flow does not require passing any additional information into the middleware.

The above assumes that the OTP code is required after every login and is not just an alternative to email verification.

1 like
vandan's avatar
Level 13

@SNAPEY - i understand but i cant implement code please help me to give example code

jlrdw's avatar

How is the user getting the token or code. Is this an API. Are you using a call or text message or email.

Is this OTP a school project. It sounds as though you need more training in such processes.

I don't understand:

if(\Auth::check() && \Auth::signup()->verify())

Normally OTP is for a one time only transaction.

Snapey's avatar

Is this to authorise login as two-factor authentication?

vandan's avatar
Level 13

@SNAPEY - i dont know i have two table so two diffrent table two authentication so how can i

for example i have two table user and signup

signup and user two diffrent authentication i cant do it

Snapey's avatar

I don't know what you are trying to do. You talk about OTP as if you know what it means - but clearly not.

1 like
vandan's avatar
vandan
OP
Best Answer
Level 13

@SNAPEY - sorry for inconiveinces

i sovled my self

adminmiddleware

public function handle($request, Closure $next)
    {
        if(\Auth::check() && \Auth::user()->isAdmin())
        {            
                return $next($request);             
        }
        elseif(\Auth::check() && \Auth::user()->isUser())
        {
                return $next($request);             
        }
        return \Redirect::route('login');
    }

login controller

public function loginstore(Request $request)
    {
        if(User::loginUser($request->email,$request->password))
        {
                $email=$request->get('email');
                $role=User::where('email','=',$email)->pluck('role')->first();
                if($role=='admin')
                {
                    $name=User::where('email','=',$email)->pluck('name')->first();
                    return \Redirect::route('dashboard',['name'=>$name]);
                }
                if($role=='2')
                {
                    $id=User::where('email','=',$email)->pluck('id')->first();
                    return \Redirect::route('job.view',['id'=>$id]);
                }
        }
        else
        {                   
                flash('Password Or Email Incorrect')->error();            
                return redirect()->back();            
        }
    }

user model

public static function loginUser($email,$password)
    {
        return \Auth::attempt(array('email'=>$email,'password'=>$password));
    }

public function isAdmin()
    {
        return($this->role=='admin');
    }

public function isUser()
    {
        return ($this->role=="2");
    }

i solved this thank you for helping @sergiu17 @snapey @jlrdw @hjortur17 thank you everyone

Please or to participate in this conversation.