在Axios请求上的laravel auth attemp会话部分不起作用

时间:2018-11-19 12:56:23

标签: php ajax laravel vue.js axios

使用引导程序模型制作登录屏幕,当我请求使用axisos调用服务器时一切正常,但是当响应到来时,我刷新页面laravel会话不起作用

这是我在控制器中的代码:

public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function showLoginForm()
    {
      return view('login');
    }

    //use AuthenticatesUsers;

    public function login(Request $request)
    {

      $validator = Validator::make($request->all(), [
                    'phone' => 'required|numeric|',
                    'password' => 'required|min:6',

        ]);



        if ($validator->fails()) {
            // return redirect()->back()->with('errors',$validator->errors())->withInput($request->only('phone', 'remember'));
              return response()->json(['success'=>false,'error'=>$validator->errors()], 403);
        }



      // Attempt to log the user in
      if (Auth::guard('web')->attempt(['phone' => '+91'.$request->phone, 'password' => $request->password], $request->remember)) {
        // if successful, then redirect to their intended location

          return response()->json(['success'=>true,'message'=>'login successfully'], 200);
      }

      // if unsuccessful, then redirect back to the login with the form data
     //dd('auth fail am  here');
      //return redirect()->back()->withErrors(['Invalid Phone Or Password'])->withInput($request->only('phone', 'remember'));
      return response()->json(['success'=>false,'error'=>'Invalid Phone Or Password'], 401);
    }

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
   // protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function logout()
    {
        Auth::guard('web')->logout();
        return redirect('/login');
    }
}

这是我的axios请求:

axios.post(process.env.MIX_BASEURL+'/login', vm.loginDetails)
                        .then(response => {
                        event.target.disabled = true;
                            console.log(response);
                           // console.log(response.config.method)

                            if (response.status == 200){
                            alert('login success')
                           vm.loginsuccess = true
                           $("#phone").addClass("is-valid");
                           $("#password").addClass("is-valid");
                           vm.successlogin=response.data.message
                            toastr["success"](vm.successlogin);

                            if(response.status===200){
                               let intended=response.data.intended;
                               alert(intended);

                               //return;
                               window.location.href = intended;
                                this.closeModal(); 


                            } 



                            }
                        })
                        .catch(error => {
                            console.log('am here in error');
                            console.log(error.response);
                            var errors = error.response
                            console.log(error.response)
                            ///IF EMPTY FIELDS FOUND///
                            if (errors.status === 403) {
                                //alert('empty fields Forbidden')
                                //alert(errors.data.error.phone[0])
                                if (errors.data) {
                                    if (errors.data.error.phone) {
                                        vm.errorsPhone = true
                                        $("#phone").addClass("is-invalid");
                                        vm.PhoneError = _.isArray(errors.data.error.phone) ? errors.data.error.phone[0] : errors.data.error.phone
                                        //alert(vm.PhoneError)
                                        toastr["error"](vm.PhoneError);
                                    }
                                    if (errors.data.error.password) {
                                        vm.errorsPassword = true
                                        $("#password").addClass("is-invalid");
                                        vm.passwordError = _.isArray(errors.data.error.password) ? errors.data.error.password[0] : errors.data.error.password
                                        //alert(vm.passwordError)
                                        toastr["error"](vm.passwordError);
                                    }


                                }
                            }
                             if (errors.status === 401) {
                             //alert('invalid login details')
                             vm.errorslogin = true
                             vm.loginerror=errors.data.error
                             //alert(vm.loginerror)
                             toastr["error"](vm.loginerror);

                             }

                        });
            }

**我尝试了什么:**当我单击登录时,我的数据不见了auth attemp successl我得到200和我设置的msg作为响应,然后调用位置重新加载(我第一次尝试并失败)它重新加载页以获取新数据,但会话数据不在我的刀片服务器模板中

0 个答案:

没有答案