Laravel 5 - how to use basic auth with username instead of email?

Hi guys!

So in Laravel 4 we could do

Route::filter('auth.basic', function()
{
    return Auth::basic('username');
});

But now this is impossible, and the dock does not give clues on how to do this. So can anyone help?

Thank!

+4
source share
3 answers

Create a new custom middleware using the same code as the standard one:

https://github.com/laravel/framework/blob/5.0/src/Illuminate/Auth/Middleware/AuthenticateWithBasicAuth.php

and override the default email field, for example:

return $this->auth->basic('username') ?: $next($request);
+8
source

This is what I use

class AuthenticateOnceWithBasicAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return Auth::onceBasic('username') ?: $next($request);
    }
}
0
source

Laravel 5.7, handle :

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @param  string|null  $field
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null, $field = null)
{
    return $this->auth->guard($guard)->basic($field ?: 'email') ?: $next($request);
}

, $field.

Laravel, :

:. :

, :

Route::middleware('auth.basic:,username')->get('/<route>', 'MyController@action');

:,username . :

public function handle($request, Closure $next, $guard = null, $field = null)

, $next . $guard null, , /, . ( , ) - $field .

0

Source: https://habr.com/ru/post/1589097/


All Articles