Problem with login in laravel 5.3

1

I have a project that consists of an administration.

What I'm doing is login, I already created the table and entered a user with password in the Hash mode of Laravel.

I have the system that apparently works, what happens is that when I try to log in I always get the wrong data error and I'm putting the correct data username and password into the table.

Can you help me figure out the problem?

Controller

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use DB;
use Auth;
use Redirect;
use Hash;
use Illuminate\Support\Facades\Input;

class LoginController extends Controller
{
    public function showLogin ()
    {
        if (Auth::check()) {
            return Redirect::to('/admin');
        }
        return view('admin/login');
    } 

    public function postLogin()
    {
        $data = [
            'username' => Input::get('username'),
            'passwd' => Input::get('password')
        ];

        if (Auth::attempt($data)) {
            return Redirect::intended('admin');
        }

        return Redirect::back()->with('error_message', 'Dados Incorrectos')->withInput();
    }

    public function logOut()
    {
        Auth::logout();
        return Redirect::to('admin/login')->with('error_message', 'Logged out correctly');
    }
}
    
asked by anonymous 19.01.2017 / 15:14

2 answers

1

This is the default code for authentication documentation , notice that it is the email field and password , in your case you are using another field, that's the problem.

if (Auth::attempt(['email' => $email, 'password' => $password])) {
     // Authentication passed...
     return redirect()->intended('dashboard');
}

This is the basic way, but there is a way for the user instance example :

public function postLogin()
{

    $username = Input::get('username');
    $passwd   = Input::get('password');

    $user = User::where('username', $username)
        ->first();

    if ($user && Hash::check($passwd, $user->passwd))
    {
        Auth::login($user);
        return Redirect::intended('admin');     
    }

    return Redirect::back()->with('error_message', 'Dados Incorrectos')->withInput();
}

is also a valid form. The second way is to search for User for the username field, if a user has returned, verify that the password matches the Hash::check and if all that happens use the instance of this Auth::login($user) class to authenticate, and this works just like the first one that first does it all transparently to the developer with their particular fields.

References

19.01.2017 / 15:21
0

You are comparing to string hash with hash string.

$data = [
  'username' => Input::get('username'),
  'passwd' => Input::get('password')
];

Use with Hash:

$data = [
  'username' => Input::get('username'),
  'passwd' => Hash::make(Input::get('password'))
];
    
19.01.2017 / 15:17