Jan 31, 2016
0
Level 2
malsup ajax form login
malsup ajax form login
I don't know what is the problem in my code
view:
<!-- Modal Login start -->
<div class="modal signUpContent fade" id="ModalLogin" tabindex="-1" role="dialog">
<div class="modal-dialog ">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button>
<h3 class="modal-title-site text-center"> 會員登入</h3>
</div>
<div class="modal-body">
<div class="control-group"><a class="fb_button btn btn-block btn-lg " href="#">FACEBOOK 登入</a></div>
<h5 style="padding:10px 0 10px 0;" class="text-center"> 或 </h5>
<form class="form" id="login" method="post" action="{{ url('/login') }}" autocomplete="off">
<div id="validation-errors"></div>
<div id="success_message"></div>
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<div class="form-group">
<input type="email" class="form-control" name="email" placeholder="電子郵件" value="{{ old('email') }}">
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="密碼">
</div>
<div class="form-group">
<div class="checkbox">
<input type="checkbox" name="remember" checked> 記住我
</div>
</div>
<div>
<div>
<input name="submit" class="btn btn-block btn-lg btn-primary" value="登入" type="submit">
</div>
</div>
<!--userForm-->
</form>
</div>
<div class="modal-footer">
<p class="text-center"> <a class="btn btn-link" data-toggle="modal" data-dismiss="modal"
href="#ModalSignup"> 會員註冊 </a>
<a class="btn btn-link" href="{{ url('/password/reset') }}">忘記密碼?</a> </p>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.Modal Login -->
@section('login-script')
<script type="text/javascript">
$(document).ready(function() {
var options = {
beforeSubmit: showRequest,
success: showResponse,
dataType: 'json',
clearForm: true
};
$('#login').ajaxForm(options);
});
function showRequest(formData, jqForm, options) {
$("#validation-errors").hide().empty();
$("#success_message").hide().empty();
return true;
}
function showResponse(response, statusText, xhr, $form) {
if(response.success == false)
{
var error = response.errors;
$.each(error, function(index, value)
{
if (value.length != 0)
{
$("#validation-errors").append('<p class="alert alert-danger"><strong>'+ value +'</strong></p>');
}
});
$("#validation-errors").show().delay(2000).fadeOut();
} else {
var success = response.message;
$("#success_message").append('<p class="alert alert-success"><strong>'+ success +'</strong></p>');
$("#success_message").show().delay(2000).fadeOut();
}
}
</script>
@stop
route:
Route::post('login', 'Auth\AuthController@signIn');
AuthController:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Role;
use Validator;
use Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$create = User::create([
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
//when create a user, it will attach a member role
$user = User::find($create->id);
$role = Role::where('name', '=', 'member')->firstOrFail();
$user->roles()->attach($role->id);
return $create;
}
public function signIn(Request $request)
{
$this->validate($request, [
$this->loginUsername() => 'required', 'password' => 'required',
]);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
$throttles = $this->isUsingThrottlesLoginsTrait();
if ($throttles && $this->hasTooManyLoginAttempts($request)) {
return $this->sendLockoutResponse($request);
}
$credentials = $this->getCredentials($request);
if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) {
return $this->handleUserWasAuthenticated($request, $throttles);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
if ($throttles) {
$this->incrementLoginAttempts($request);
}
return $this->sendFailedLoginResponse($request);
}
}
Request URL:http://localhost/login Request Method:POST Status Code:302 Found Remote Address:127.0.0.1:80
Please or to participate in this conversation.