无法使用Ajax对登录laravel进行身份验证

时间:2018-09-13 13:38:29

标签: php laravel laravel-5 laravel-4

我有这样的登录功能。我使用两种方式登录第一次是提交和第二次我正在尝试ajax。 如果(!empty($ login)){,此条件适用于ajax 。Auth:: attempt 正在重现true,并且还保存了会话,但是当我再次刷新时,请询问登入 。但是提交表单选项起作用。 laravel 4.4

public function login($login = '') {
        if (!empty($login)) {
            $email = $login['email'];
            $password = $login['password'];
            ob_start();
            if (Auth::attempt(array('email' => $email, 'password' => $password, 'status' => 1), true)) {
                print_r(Session::all());
                die;
                $resp['message'] = 'Login Successful!';
                echo json_encode($resp);
                exit();
            } else {

                $resp['message'] = 'Invalid account/login credentials';
                echo json_encode($resp);
                exit();
            }
            ob_end_clean();   //    the buffer and never prints or returns anything.
        } else {
            $validator = Validator::make(
                            Input::all(), array(
                        'Username' => 'required|exists:tblusers,email',
                        'Password' => 'required'
                            ), array(
                        'required' => 'This field is required.',
                        'exists' => 'Email not found in our database',
                            )
            );

            if ($validator->fails()) {
                $resp['messages'] = Helper::errors($validator->messages());
                return Redirect::to(URL::previous() . "#modalSignIn")->withErrors(['Invalid account/login credentials, please make sure you are using correct credentials or you\'re email is verfied.']);
            } else {
                $email = Input::get('Username');
                $password = Input::get('Password');
                if (Auth::attempt(array('email' => $email, 'password' => $password, 'status' => 1), true)) {

                    if (isset($_POST['designs'])) {
                        // return redirect()->action('DesignsController@index');
                        return Redirect::to("designs");
                    }
                    print_r(Session::all());
                    die;
                    return Redirect::back()->with('login_success_message', 'Login Successful!');
                } else {
                    return Redirect::to(URL::previous() . "#modalSignIn")->withErrors(['Invalid account/login credentials, please make sure you are using correct credentials or you\'re email is verfied.']);
                }
            }
        }
    }

0 个答案:

没有答案