1

Well, after validate, if my password is longer than 3 characters I get this error:

Argument 1 passed to Illuminate\Http\RedirectResponse::withInput() must be of the type array, object given, called in /home/vagrant/Code/stack-laravel/app/Http/Controllers/Admin/AuthController.php on line 50 and defined

And this is my AuthController:

<?php

namespace App\Http\Controllers\Admin;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    protected $redirectPath = '/admin';

    public $loginPath = '/admin';

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    public function getLogin()
    {
        if (Auth::user()){
            return redirect('/admin');
        }

        return view('admin.pages.login.index');

    }

    public function postLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|min:3'
        ]);

        $credentials = $this->getCredentials($request);

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return redirect()->intended($this->redirectPath());
        }

        return redirect($this->loginPath())
            ->withInput($request)
            ->withErrors();
    }
}

What is wrong?

2 Answers 2

2

$request is an object, and withInput() takes an array of inputs to pass along. In order to get an array of your request inputs you could do

return redirect($this->loginPath())
        ->withInput($request->all())
        ->withErrors();

You can also call withInput() without any arguments and by default it will send all of the inputs along:

return redirect($this->loginPath())
        ->withInput()
        ->withErrors();

In this particular case, you probably don't want to pass the password field input back (because they entered an incorrect password presumably, or for security purposes) so you could use the $request->except() method:

return redirect($this->loginPath())
        ->withInput($request->except('password'))
        ->withErrors();

Edit: as for the second error, you actually don't need to use withErrors() when you use the $this->validate() method to validate the input. See here: http://laravel.com/docs/5.0/validation#controller-validation . With controller validation, they will automatically be redirected back by the $this->validate() function. Instead, you could set your code up like this:

public function postLogin(Request $request)
{

    //Validate inputs

    $validator = Validator::make($request->all(), [
        'email' => 'required|email',
        'password' => 'required|min:3'
    ]);

    if($validator->fails()){
        return redirect($this->loginPath())
            ->withInput($request->all())
            ->withErrors($validator);
    }

    //Input is valid, now check credentials

    $credentials = $this->getCredentials($request);

    if (Auth::attempt($credentials, $request->has('remember'))) {
        return redirect()->intended($this->redirectPath());
    }

    //Username and password are incorrect, redirect with error message

    return redirect($this->loginPath())
            ->withInput($request->all())
            ->with('error','Your username and password are not correct');

}

Lastly you will want to display the error message in your login view:

@if (session('error'))
    <div class="alert alert-danger">
        {{ session('error') }}
    </div>
@endif
Sign up to request clarification or add additional context in comments.

7 Comments

Works, but now error with withErrors(), this is happening: Missing argument 1 for Illuminate\Http\RedirectResponse::withErrors(), called in /home/vagrant/Code/stack-laravel/app/Http/Controllers/Admin/AuthController.php on line 51 and defined
with your postLogin() if my password is larger than 3 characters and a real email is given a blank page is returned
OK i added a final redirect for when the username and password are wrong, and showed an example of how you could display that in your login page view
Cool, I'm starting to understand... You passed the error as session right? Can I pass it as variable?
->with('error','Your username and password are not correct'); flashes a variable to the session called "error". Within the view, you can then access this as session('error'). You could use any name for this, eg ->with('foo','bar'); then in the view {{ session('foo') }} would print bar see: laravel.com/docs/5.1/…
|
0

You should use this:

return redirect($this->loginPath())
        ->withInput($validator)
        ->with('error','write here your error message');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.