doulai2025 2018-11-19 12:56
浏览 56

在Axios请求laravel auth尝试会话部分不工作

am making a login screen using bootstrap model and when I request using axisos call to server everything working fine but when the response came and I refresh page laravel session part not working

here is my code in controller :

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');
    }
}

this is my axios request:

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);

                             }

                        });
            }

**what i have try :**when i click on login my data is gone auth attemp successfull i got 200 and my msg as i set in response then i call to location reload (my first try and fail) it reload page for new data but session data not coming in my blade template

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog