Laravel 5.2 login issue

2019-09-09 09:09发布

Strange issue, when I use the default register route, and I create a user, I can enter inside my app, everything ok. But if I use my CRUD, or the MySQL command for create the user, I can't login. These credentials do not match our records. (sorry for bad english, I attach the image) Andrea and Raffaello can login (create with register default route) Marino can't (generate with my CRUD function) but appear 3 identical records..

enter image description here

routes.php :

Route::group(['middleware' => ['web']], function () {
    Route::resource('dash/reports', 'Dash\\ReportsController');
});

/* ruote for Admin */
Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/categories', 'Dash\\CategoriesController');
});

Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/roles', 'Dash\\RolesController');
});
Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/permissions', 'Dash\\PermissionsController');
});
Route::group(['middleware' => ['role:admin']], function () {
    Route::resource('dash/users', 'Dash\\UsersController');
});

/* another routes */
Route::auth();

Route::get('/home', 'HomeController@index');
Route::get('/', function () {return view('welcome');});

Controller (custom CRUD operation)

<?php

namespace App\Http\Controllers\Dash;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Auth;
use App\User;

use App\Report;
use App\Category;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Session;

use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class UsersController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;


    /**
     * Display a listing of the resource.
     *
     * @return void
     */
    public function index()
    {
        $users = User::paginate(15);

        return view('dash.users.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return void
     */
    public function create()
    {
        return view('dash.users.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return void
     */

    public function store(Request $request)
    {
        $this->validate($request, ['email' => 'required', 'name' => 'required', 'password' => 'required', 'surname' => 'required', ]);


        $user = new User($request->all());
        $user->password = bcrypt($request);
        $user->save();

        return redirect('dash/users');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function show($id)
    {
        $user = User::findOrFail($id);

        return view('dash.users.show', compact('user'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function edit($id)
    {
        $user = User::findOrFail($id);

        return view('dash.users.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function update($id, Request $request)
    {
        $this->validate($request, ['email' => 'required', 'name' => 'required', 'password' => 'required', 'surname' => 'required', ]);

        $user = User::findOrFail($id);
        $user->update($request->all());

        Session::flash('flash_message', 'User updated!');

        return redirect('dash/users');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return void
     */
    public function destroy($id)
    {
        User::destroy($id);

        Session::flash('flash_message', 'User deleted!');

        return redirect('dash/users');
    }
}

view:

@extends('layouts.app')

@section('content')
<div class="container">

    <h1>Create New User</h1>
    <hr/>

    {!! Form::open(['url' => '/dash/users', 'class' => 'form-horizontal']) !!}

            <div class="form-group {{ $errors->has('email') ? 'has-error' : ''}}">
                {!! Form::label('email', trans('users.email'), ['class' => 'col-sm-3 control-label']) !!}
                <div class="col-sm-6">
                    {!! Form::text('email', null, ['class' => 'form-control', 'required' => 'required']) !!}
                    {!! $errors->first('email', '<p class="help-block">:message</p>') !!}
                </div>
            </div>
            <div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
                {!! Form::label('name', trans('users.name'), ['class' => 'col-sm-3 control-label']) !!}
                <div class="col-sm-6">
                    {!! Form::text('name', null, ['class' => 'form-control', 'required' => 'required']) !!}
                    {!! $errors->first('name', '<p class="help-block">:message</p>') !!}
                </div>
            </div>
            <div class="form-group {{ $errors->has('password') ? 'has-error' : ''}}">
                {!! Form::label('password', trans('users.password'), ['class' => 'col-sm-3 control-label']) !!}
                <div class="col-sm-6">
                    {!! Form::text('password', null, ['class' => 'form-control', 'required' => 'required']) !!}
                    {!! $errors->first('password', '<p class="help-block">:message</p>') !!}
                </div>
            </div>

            <div class="form-group {{ $errors->has('surname') ? 'has-error' : ''}}">
                {!! Form::label('surname', trans('users.surname'), ['class' => 'col-sm-3 control-label']) !!}
                <div class="col-sm-6">
                    {!! Form::text('surname', null, ['class' => 'form-control', 'required' => 'required']) !!}
                    {!! $errors->first('surname', '<p class="help-block">:message</p>') !!}
                </div>
            </div>


    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-3">
            {!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
        </div>
    </div>
    {!! Form::close() !!}

    @if ($errors->any())
        <ul class="alert alert-danger">
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    @endif

</div>
@endsection

also my custon postLogin() in AuthController for try to overlap default postLogin() - but not working

public function postLogin(LoginRequest $request)
{

  if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) 
    {
        return redirect('/dash-board');
    }

    return redirect('/login')->withErrors([
        'email' => 'The credentials you entered did not match our records. Riprovare?',
    ]);
}

1条回答
叼着烟拽天下
2楼-- · 2019-09-09 09:43

there is it you need to specify the data you get from $request

you are storing password not correctly

change this :

$user = new User($request->all());
        $user->password = bcrypt($request);
        $user->save();    

to this :

 $user = new User($request->all());
        $user->password = bcrypt($request->pwInputName);
        $user->save();

pwInputName : is the password input name in your view


the issue was that you bcrypt all of $request

查看更多
登录 后发表回答