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: