Customization of Fields Login Laravel

1

It's been a while since I've been headlining this Laravel Authentication System and I need to learn how to properly override functions to be able to use custom fields by default. Laravel uses email and password . I want to use pass_senha and cpf as user for example, but I'm not getting

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class AdminLoginController extends Controller
{
    public function __construct() {
        $this->middleware('guest:admin');
        // somente quem não estiver logado como admin terá acesso ao login
    }

    public function index() {
        return view('auth.admin-login');
    }
    public function getAuthPassword()
    {
        return $this->password;
    }

    // =>>>>> Primeiro esse:
    // public function login(Request $request) {
    //     return true;
    // }

    // =>>>>> Depois esse:
    public function login(Request $request) {

        // validar o dado que vem do formulario
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required'
        ]);

        // tentar logar
        $credentials = [ 'email'    => $request->email,  
        'password' => $request->password ];
        // $credentials = $request->only('email', 'password');

        // Auth::attempt($credentials, $remember);  //=> se eu utilizar assim, utilizarei o 'default' e nao o novo, admin
        // INCLUIR: use Auth;
        $authOk = Auth::guard('admin')->attempt($credentials, $request->remember); // ==> assim eu utilizo o guard do admin


        // se ok, então direcionar para a localização interna
        if ($authOk) {
            // Quando um usuário tenta acessar uma página que necessita de login
            // e o Laravel redireciona direto pro login, essa página é mantida 
            // pelo framework e pode ser chamada através do método redirect()->intended()
            // Se nao houver nenhuma página requisitada anterior ao login, 
            // o Laravel redireciona para a rota passada por parâmetro
            return redirect()->intended(route('admin.dashboard'));
        }

        // se não, redirecionar novamente para o login, passando novamente os parametros do request
        return redirect()->back()->withInput($request->only('email','remember'));
    }
}

I have a controller login admin and I created a Guard admin no auth/config !

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    use Notifiable;

    // =====>>> novo
    protected $guard = 'admin';




    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

Here is the Admin model

    
asked by anonymous 03.12.2018 / 21:48

1 answer

2

You can overwrite the getCredentials method ()

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function getCredentials(Request $request)
{
    return $request->only( 'field_email', 'field_passwd' );
}

By default, Laravel uses email for authentication but if you need to change just the username you simply create the following method in the LoginController:

public function username()
{
    return 'cpf';
}
    
04.12.2018 / 12:54