Problem with login in Laravel 5.3

Asked

Viewed 291 times

1

I have a project that consists of an administration.

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

I have the system that apparently works, what happens is that when I try to log in the always wrong data error and I’m putting in the right data username and password that I created in the table.

You can 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');
    }
}
  • Caro Cesar should be by email field and not by username as described in documentation, expensive that there are other means of authentication, for example, by the class instance User, but it’s not your case.

  • Ready. I edited the answer is the second option.

2 answers

1


This is the standard code for authentication documentation, Realize it’s the countryside email and password, in your case you’re using another field, that’s the problem.

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

This is the basic mode, but there is a mode by 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 way. The second way is to seek the User across the countryside username, if returned a user, check if the password matches the Hash::check and if you went through all this use the instance of that class Auth::login($user) to authenticate, and this works similar to the first but first does all this transparently to the developer with their particular fields.

References:

  • But I want to use the username field that is what exists in my table and not email as I do

  • I will propose the solution @Césarsousa ... 2 minutes

  • this error parse error: syntax error, Unexpected 'Return' (T_RETURN)

  • Missing some keys @Césarsousa look now

  • I’ve tried it and it works right can explain what I did to make me realize

  • @Césarsousa ta edited and the explanation in the answer.

  • 1

    Thanks for all

  • One thing I was here testing my logout that I had done but now I love an error saying that there is no remember_token column in my table how do I fix this

  • You can open a new question @Césarsousa, that’s the practice of the site! put the new information in it.

Show 4 more comments

0

Are you comparing to string NO hash with hash string.

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

Use with the Hash:

$data = [
  'username' => Input::get('username'),
  'passwd' => Hash::make(Input::get('password'))
];
  • That’s not the problem, it’s the email that he should use, the Hash:make only used for recording in the method conference attempt he already does that automatically.

  • rephrase your question since you know where the error is.

  • 1

    I’m not the one with the problem, I’m just pointing out what he should do including create an answer.

  • @Virgilionovic sorry, had not realized that the question is not yours! rs

  • hassle-free!.

Browser other questions tagged

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