Laravel Auth :: attempt () returns false

I am a home hobby and am studying Laravel, currently in version 5.3. I use a Mac, neither homestead, nor vagrant.

I am currently working on a website that uses a login and registration system to create users.

I used php artisan migrateto manage the database locally.

Screen Shot 2016-12-27 at 4.18.38 PM.png

As indicated below, it has three fields, namely:

  • Email
  • Username
  • Password

I have a model User(users.php):

<?php

namespace blog;

use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;

class User extends Model implements Authenticatable {
    use \Illuminate\Auth\Authenticatable;

    use Notifiable;

    protected $fillable = [
        'username', 'email', 'password',
    ];

}

And also the class UserController(UserController.php):

<?php

namespace blog\Http\Controllers;

use Auth;
use blog\User;
use Illuminate\Http\Request;

class UserController extends Controller {

    public function postRegister(Request $request) {
        $username = $request['username'];
        $email = $request['email'];
        $password = bcrypt($request['password']);

        $user = new User();
        $user->email = $email;
        $user->username = $username;
        $user->password = $password;

        $user->save();

        return redirect()->route('login');        
    }

    public function postLogin(Request $request) {

        $credentials = [
            'username' => $request['username'],
            'password' => $request['password'],
        ];

        if(Auth::attempt($credentials)) {
            return redirect()->route('dashboard');       
        }

        return 'Failure'; 
    }
}

?>

As you can see, I use bcrypt()as a hashing method.

However, this problem always leads to failure.

Screen Shot 2016-12-27 at 4.41.38 PM.png

I checked the following links:

, Input.

+9
3

, login . , $request .

, : postRegister name, email password. . , , login. postLogin , . Auth::attempt($credentials) , Failure .

dd($credentials) , , :

public function postLogin(Request $request)
{
    $credentials = [
        'username' => $request['username'],
        'password' => $request['password'],
    ];

    // Dump data
    dd($credentials);

    if (Auth::attempt($credentials)) {
        return redirect()->route('dashboard');
    }

    return 'Failure';
}

- :

array:2 [
  "username" => null
  "password" => null
]

( , URL-), , . , HTTP. , .

, , , , .

1.

, , postRegister() ( ), postLogin(), Session facade, session() Illuminate\Session\SessionManager.

:
( , , , ..)

public function postRegister(Request $request)
{
    // Retrieve all request data including username, email & password.
    // I assume that the data IS validated.
    $input = $request->all();

    // Hash the password
    $input['password'] = bcrypt($input['password']);

    // Create the user
    User::create($input);

    // Redirect
    return redirect()
        // To the route named `login`
        ->route('login')

        // And flash the request data into the session,
        // if you flash the `$input` into the session, you'll
        // get a "Failure" message again. That because the 
        // password in the $input array is already hashed and 
        // the attempt() method requires user password, not 
        // the hashed copy of it. 
        //
        ->with($request->only('username', 'password'));
}

public function postLogin(Request $request)
{
    // Create the array using the values from the session
    $credentials = [
        'username' => session('username'),
        'password' => session('password'),
    ];

    // Attempt to login the user
    if (Auth::attempt($credentials)) {
        return redirect()->route('dashboard');
    }

    return 'Failure';
}

. , postLogin(), , , . , postLogin postRegister.

2.

; , , ?

, Laravel .

, :
( , , Laravel . , .)

public function postRegister(Request $request)
{
    $input = $request->all();

    $input['password'] = bcrypt($input['password']);

    User::create($input);

    // event(UserWasCreated::class);

    if (Auth::attempt($request->only('username', 'password'))) {
        return redirect()
            ->route('dashboard')
            ->with('Welcome! Your account has been successfully created!');
    }

    // Redirect
    return redirect()
        // To the previous page (probably the one generated by a `getRegister` method)
        ->back()
        // And with the input data (so that the form will get populated again)
        ->withInput();
}

, ! . events, . , , .

, . . Laravel, RegistersUsers AuthenticatesUsers, .

; Illuminate\Auth\Authenticatable User, Authenticatable, .

+5

- , bcrypt (pass). Auth:: , , ,

+1

Auth::attemptuses \Hash::make($someString)to generate a hash. You should use this also in order to generate the same hashes from the same lines (I assume the seed is different from the function bcrypt()).

So change this line:

$password = bcrypt($request['password']);

To:

$password = \Hash::make($request['password']);
-2
source

Source: https://habr.com/ru/post/1664983/


All Articles