Customization of Fields Login Laravel

Asked

Viewed 794 times

1

It’s been a while since I’ve been banging my head with this Laravel Authentication System and I need to learn how to correctly overwrite the 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'));
    }
}

Above I have a controller login admin and created a Guard admin in the 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

1 answer

2

You can make a overwrite in the method getCredentials()

/**
 * 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 the Laravel uses email for authentication but if you need to change only the username just create the following method in Logincontroller:

public function username()
{
    return 'cpf';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.