I just started to learn Laravel and I was trying to set up an authentication system. I could have used the laravel built in auth but I followed a tutorial to understand the concept. My auth system is built using some modals from bootstrap and the system works (kind of). The sign up works but as soon as I enter the sign in logic the sign up crashes somehow (no errors) and doesn't save my user in the database.
Here's what I'm using:
This is the modal window for the Sign Up. The Log In is pretty much the same, I just changed the action to point to the postSignin method.
<!-- Modal Register area -->
<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myRegisterLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
{!! Form::open(['method' => 'POST', 'action' => 'UserController@postSignup', 'class' => 'login-form']) !!}
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" id="myRegisterLabel">Sign Up</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label>Email:</label>
{!! Form::text('email', Request::old('email'),['placeholder' => 'Email...']) !!}
</div>
<div class="form-group">
<label>Password:</label>
{!! Form::password('password', Request::old('password'),['placeholder' => 'Password...']) !!}
</div>
<div class="row">
<div class="col-sm-12">
<input type="submit" class="btn btn-primary login-btn" value="Sign Up">
</div>
</div>
</div>
{!! Form::close() !!}
@if($errors->any())
@foreach ($errors->all() as $error)
<div class="alert alert-danger">
<p>{{ $error }}</p>
</div>
@endforeach
@endif
</div>
</div>
</div>
<!-- End Modal Register area -->
This is how I set up my controller:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Auth;
class UserController extends Controller
{
public function postSignup(Request $request){
$this->validate($request, [
'email' => 'email|required|unique:users',
'password' => 'required|min:4'
]);
$user = new User([
'email' => $request->input('email'),
'password' => bcrypt($request->input('password'))
]);
$user->save();
return redirect('/');
}
public function postSignin(Request $request){
$this->validate($request, [
'email' => 'email|required',
'password' => 'required|min:4'
]);
if (Auth::attempt([
'email' => $request->input('email'),
'password' => $request->input('password')
])){
return redirect()->route('user.profile');
}
return redirect()->back();
}
public function getProfile(){
return view('user.profile');
}
}
For my routes I used just:
Route::post('/','UserController@postSignup');
Route::post('/','UserController@postSignin');
So, as I said, as soon as I set up the Log In the Sign up is not registering my user into the DB. If I take it out, it works.. The Log In works either way if I have some users into the DB. Thanks for your help!