'Call authenticate manually in router middleware

I created a CustomAuth middleware to use in routes to authenticate by "user_id" in request body or "Authentication" in request header. I need call Authenticate class case "user_id" isn't passed.

class CustomAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $guards)
    {
        if ($request->get('user_id')) {
            Auth::loginUsingId($request->get('user_id'));
        } else {
            <-- here -->
        }

        return $next($request);
    }
}

Obs: I use whitelist ip middleware as well.



Solution 1:[1]

Do you want to authenticate user in login form by user_id? if yes you should change login form and make change in this file: vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php

public function username()
{
    return 'email'; // default is email but you can change it to id or user_id if you have this column in your users table
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Amu