jeudi 16 septembre 2021

Getting 302 found in ajax jquery request in laravel

I'm using ajax request in laravel and passing csrf token also but some times my application gets stuck and getting this error :

enter image description here

Code :

  var formData = $(this).serialize();
    $.ajaxSetup({
        header:$('meta[name="_token"]').attr('content')
    });
    $.ajax({
        url:$(this).attr('action'),
        type:'POST',
        data:formData,
        headers: {
            Accept: "application/json"
        },
        success:function(response) {

This form is in modal popup :

 <form id="user-login" class="login" action="" method="post">
 

Controller :

  public function login(Request $request)
    {  
        $validator = Validator::make($request->all(),[
            'email' => ['required'],
            'password' => ['required', 'string', 'min:5'],
        ]);
        if ($validator->passes()) {
        if (\Auth::guard('user')->attempt(['email' => $request->email, 'password' => $request->password, 'status' => 'active','isAdmin'=>'0']) || \Auth::guard('user')->attempt(['contact_number' => $request->email, 'password' => $request->password, 'status' => 'active','isAdmin'=>'0'])) {
            $request->session()->regenerate();
            return response(['success' => true,'message'=>'Successfully Login'], 200);
         }
        else
        {
            $message = 'Invalid username or password';
            return response()->json(['success'=>false,'message' => $message]);
        }
    }
    return Response::json(['errors' => $validator->errors()]);
    }

Route:

Route::post('/user-login', 'Auth\UserRegisterController@login')->name('user-login');

RedirectIfAuthenticated Middleware:

 public function handle($request, Closure $next, $guard = null)
    {
        if ($guard == "employee" && Auth::guard($guard)->check()) {
                return redirect(RouteServiceProvider::EMPLOYEE_HOME);
        }

        if (Auth::guard($guard)->check()) {
            return redirect(RouteServiceProvider::HOME);
        }

        return $next($request);
    }

Any solution, Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire