简体   繁体   English

如何使用 laravel 5.4 通过 jQuery/ajax 创建登录/注销

[英]How to create login/logout via jQuery/ajax using laravel 5.4

I am new in Laravel and I am simply create login via jQuery/ajax.我是 Laravel 的新手,我只是通过 jQuery/ajax 创建登录。 According to me its work perfectly code mention below:根据我的说法,它的工作完美代码如下:

Views: login.blade.php查看:login.blade.php

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            email = $("#email").val();
            password = $("#password").val();
            $.ajax({
                type:"POST",
                data:{"email":email, "password":password, "_token":"{{csrf_token()}}"},
                url:"{{URL::to('login_redirect')}}",
                success:function(data){
                    if (typeof data !== 'object') {
                        data = JSON.parse(data);
                    }
                    if (data.redirect) 
                    {
                        window.location.replace(data.redirect);
                    } 
                    else 
                    {
                        $("#success").html('<p style="color:red;">' + data.error + '</p>');
                    }
                }
            });
        });
    });
</script> 
<div id="success"></div>
<input type="text" name="email" id="email" placeholder="Email">
<input type="password" name="password" id="password" placeholder="Password">
<input type="submit" name="submit" id="submit" class="btn btn-primary">

contollers: Mycontroller.php控制器:Mycontroller.php

<?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    use App\Http\Requests;
    use Auth;
    use Session;
    use DB;

    class Mycontroller extends Controller 
    {   
        public function login_redirect(Request $request)
        {
            $email = $request->input('email');
            $password = $request->input('password');
            $sql = DB::table('user')->where('email', '=', $email)->where('password', '=', $password)->count();
            if($sql > 0)
            {
                $query = DB::table('user')->where('email', '=', $email)->where('password', '=', $password)->get();
                Session::put('user', $query);
                if (!isset($_POST)) 
                {
                    header ("Location: dashboard");
                } 
                else 
                {
                    echo json_encode(array('redirect' => "dashboard"));
                }
            }
            else 
            {
                echo json_encode(array('error' => 'Wrong email or password or may be your account not activated.'));
            }
        }

        public function dashboard()
        {
            $user = Session::get('user');
            return view('user.dashboard',['data'=>$user]);
        }

        public function logout(Request $request) {
            Auth::logout();
            Session::flush();
            return redirect('/login');
        }
    }

views: dashboard.php视图:dashboard.php

<?php
    if(empty($data))
    {
        header('location:{{url("login")}}');
    }
?>
@if (is_array($data) || is_object($data))
    @foreach($data as $row)
        <h3>Welcome, {{ $row->username }}</h3>
    @endforeach
@endif
<a href="{{url('logout')}}">Logout</a>

Now, the problem is that when I click on logout button it redirect me on login page which is fine but when I directly browse dashboard page on my url without login it again accessible which is wrong.现在,问题是当我单击注销按钮时,它会将我重定向到登录页面,这很好,但是当我直接浏览我的 url 上的dashboard页面而没有登录时,它再次访问,这是错误的。 I want once user logout it can't access dashboard directly.我希望一旦用户注销它就不能直接访问dashboard So, How can I do this?那么,我该怎么做呢? Please help me.请帮我。

Thank You谢谢你

You should need to check the user is login status in the dashboard.您应该需要在仪表板中检查用户的登录状态。 For simply, you can try the below answer.简单来说,您可以尝试以下答案。 As using Laravel, why can't you use the middleware property?在使用 Laravel 时,为什么不能使用middleware属性?

Middleware provide a convenient mechanism for filtering HTTP requests entering your application.中间件提供了一种方便的机制来过滤进入应用程序的 HTTP 请求。 For example, Laravel includes a middleware that verifies the user of your application is authenticated.例如,Laravel 包含一个中间件,用于验证您的应用程序的用户是否已通过身份验证。 If the user is not authenticated, the middleware will redirect the user to the login screen.如果用户未通过身份验证,中间件会将用户重定向到登录屏幕。 However, if the user is authenticated, the middleware will allow the request to proceed further into the application.但是,如果用户通过了身份验证,中间件将允许请求进一步进入应用程序。

public function dashboard()
{
    $user = Session::get('user');
    if($user)
       return view('user.dashboard',['data'=>$user]);
    else
       return redirect('/login');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM