Laravel 5 - Auth::Attempt: Error using another table

Asked

Viewed 757 times

2

Good afternoon guys, I’ve been searching on the Internet and unfortunately I could not solve my problem.

I need to authenticate users using the Laravel 5.2 framework but my method needs to be different, that is, instead of using php Artisan make:auth I need to do manual authentication of users, in the case Auth::Attempt.

My problem is that the function is returning FALSE. The credentials entered are correct, but I still can’t.

Settings:

Routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('login', 'LoginController@login');

App User.php

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $table = 'usuarios';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nome', 'email', 'password',
    ];

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

    public function setPasswordAttribute($tmp)
    {
        return $this->attributes['password'] = trim(\Hash::make($tmp));
    }
}

App Controllers Logincontroller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class LoginController extends Controller
{
    public function login()
    {
        if (\Auth::attempt(['email' => '[email protected]', 'password' => '123'])) {
            return 'TRUE';
        } else {
            return 'FALSE';
        }
    }
}
  • You are registered in the table users these data in the columns email and password respectively ?

1 answer

-1

I see that there was not made one request of the information, nor a Guard for those purposes.

See the model below and see what you can do .

 public function postlogin(loginAlunoRequest $inf_valores) {
    // atribuindo valores digitados para uma validação
    $validacao = ['RA' => $inf_valores->get('RA'), 'password' => $inf_valores->get('password'),];

    if (Auth::guard('guardLoginAluno')->attempt($validacao)) {  // Validando as informações.
        $Situacao = auth()->guard('guardLoginAluno')->user()->Situacao; // Atribuindo o valor do campo situação para uma vareavel
        if ($Situacao == "ATIVO") { // validando se o aluno esta ativo
            return redirect('/aluno'); // direcionando o usuario para pagina solicitada
        } else { // condição caso o aluno esteje inativo
            return redirect('/loginaluno') // direcionando para pagina de login 
                            ->withErrors(['Inativo' => 'Aluno Inativo!']) // messagem de erros aluno inativo
                            ->withInput(); // levando as informações de digitada de volta para o form login
        }
    } else {
        return redirect('/loginaluno') // caso a senha ou ra esteja incorretos
                        ->withErrors(['ERRO-LOGIN' => 'RA ou Senha Inválidos!']) // messagem de dados invalidos
                        ->withInput(); // levando as informações de digitada de volta para o form login
    } 

Browser other questions tagged

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