3

I'm trying to validate a login/signup system in Laravel 5.2.

I'm using welcome.blade.php as the index page which contains two forms for both signup and signin

This is how my UserController.php is set up:

namespace authme\Http\Controllers;

use Illuminate\Http\Request;
use authme\User;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    public function getDashboard()
    {
        return view('dashboard');
    }
    public function doSignUp(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email|unique:users',
            'first_name' => 'required|max:120',
            'password' => 'required|min:6'
        ]);

        $email = $request['email'];
        $password = bcrypt($request['password']);
        $first_name = $request['first_name'];
        $last_name = $request['last_name'];
        $location = $request['location'];
        $phone = $request['phone'];

        $user = new User();
        $user->email = $email;
        $user->password = $password;
        $user->firstname = $first_name;
        $user->lastname = $last_name;
        $user->location = $location;
        $user->phone = $phone;

        $user->save();

        Auth::login($user);

        return redirect()->route('dashboard');

    }

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

        if(Auth::attempt(['email' => $request['email'], 'password' => $request['password']])){
            return redirect()->route('dashboard');
        }
        return redirect()->back();
    }
}

To show the errors during signup/login, in my view file (welcome.blade.php), I've included:

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

and the routes are set up like this:

Route::group(['middleware' => ['web']], function() {

Route::get('/', function () {
    return view('welcome');
});

Route::post('/signup', [
    'uses'  => 'UserController@doSignUp',
    'as' => 'signup'
]);

Route::post('/signin', [
    'uses'  => 'UserController@doSignIn',
    'as' => 'signin'
]);

Route::get('/dashboard', [
    'uses' => 'UserController@getDashboard',
    'as' => 'dashboard'
]);
});

The forms are working fine when entering correct details. However when I enter incorrect data:

  • When I try to submit the blank form, it just redirects back to the original page, but no errors are shown
  • When I try to enter an email that is already in use, it again redirects back to the original page, but no errors are displayed

However, I couldn't find any possible syntax error in my code. Is there any logical errors?

PS:

  1. I've not edited or modified App\Exceptions\Handler
  2. None of the answers to questions asked both here and here solves my problem
Community
  • 1
  • 1
byteseeker
  • 1,335
  • 2
  • 13
  • 22
  • Take a look at [Validation error in Laravel - $errors array does not get populated after the validation failure](http://stackoverflow.com/questions/36503825/validation-error-in-laravel-errors-array-does-not-get-populated-after-the-val) – Thomas Kim Apr 14 '16 at 21:27

3 Answers3

5

If you're using Laravel version higher or equal to 5.2.25 you no longer need to put your routes in the web middleware. It is now automatically applied.

Putting your routes in the web middleware in the routes.php actually causes the problem.

Explained here.

Community
  • 1
  • 1
pawelmysior
  • 3,190
  • 3
  • 28
  • 37
1

You can also use following codes to show the errors for sign in with invalid credential.

public function doSignIn(Request $request)
{
    $rules = array(
        'email'    => 'required',
        'password' => 'required'
    );
    $validator = Validator::make($request->all(), $rules);
    if ($validator->fails()){
            return redirect('welcome')
            ->withErrors($validator)
            ->withInput();
    }else{
        if(Auth::attempt(['email' => $request['email'], 'password' => $request['password']])){
            return redirect()->route('dashboard');
        }
    }
}
0

It seems to me your validation error messages are not getting saved to the session. Have a look at this.

https://github.com/laravel/framework/issues/12022

Corona
  • 81
  • 9