如何使用 laravel、Jquary-Ajax 将自定义单用户登录系统更新为多用户登录系统

问题描述 投票:0回答:0

你好,我是 laravel 的新手,我已经创建了一个自定义的单用户登录系统。现在我必须更新这个 具有自定义多重身份验证的登录页面,将根据用户的角色重定向用户。 但我不知道如何根据他们的角色将经过身份验证的用户重定向到不同的页面?

我有3个用户->管理员用户,医生用户和接待员用户 我用过 laravel,Jquery-Ajax。

请帮我创建多用户登录页面。

登录页面

@extends('layouts.index')

@section('title') {{'Login'}} @endsection
@section('content')

<div class="container-fluid">
    <div class="row d-flex justify-content-center align-item-center min-vh-100">
        <div class="col-lg-4">
            <div class="card shadow">
                <div class="card-header">
                    <h4 class="text-center">System Login</h4>
                </div>
                <div class="card-body p-5">
                    <div id="login_alert"></div>
                    <form method="POST" action="#" id="login_form">
                        @csrf
                        <div class="mb-3">
                            <input type="email" name="email" id="email" class="form-control rounded-0" placeholder="E-mail">
                            <div class="invalid-feedback"></div>
                        </div>

                        <div class="mb-3">
                            <input type="password" name="password" id="password" class="form-control rounded-0" placeholder="Password">
                            <div class="invalid-feedback"></div>
                        </div>

                        <div class="mb-3">
                            <a class="text-decoration-none" href="{{ url('/forgot_password')}}">Forgot Password?</a>
                        </div>

                        <div class="mb-3 d-grid">
                            <input type="submit" value="Login" class="btn btn-dark rounded-0" id="login-btn">
                        </div>

                        <div class="text-center text-secondary">
                            <div>Don't Have an account? <a href="{{ url('/register')}}" class="text-decoration-none">Register Here</a></div>
                         
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@section('script')
<script>
    $(function() {
        $("#login_form").submit(function(e){
        e.preventDefault();
        $("#login-btn").val('please wait...');
        $.ajax ({
        url:'{{ route('auth.login')}}',
        method: 'post',
        data: $(this).serialize(),
        dataType: 'json',
        success: function(res) {
            if(res.status == 400) {
            showError('email', res.messages.email);
            showError('password', res.messages.password);
            $("#login_btn").val('Login');
        } else if(res.status == 401) {
            $("#login_alert").html(showMessage('danger',res.messages));
            $("#login_btn").val('Login');
        }else {
            if(res.status == 200 && res.messages == 'success'){
                window.location = '{{ route('profile')}}';
            }

        }
      }
    });
  });
});
</script>
@endsection

UserController.php

//handle login user ajax 
    `public function loginUser(Request $request){
        $validator = Validator::make($request->all(), [
            'email' => 'required|email|max:100',
            'password' => 'required|min:8|max:50'
        ]);

        if($validator->fails()){
            return response()->json([
                'status' => 400,
                'messages' => $validator->getMessageBag()
            ]);
           }else{
              $user = User::where('email', $request->email)->first();
              if($user){
                if(Hash::check($request->password, $user->password)){
                    $request->session()->put('loggedInUser',$user->id);
                    return response()->json([
                        'status' => 200,
                        'messages' => 'success'
                        
                    ]);
                } else{
                    return response()->json([
                        'status' => 401,
                        'messages' => 'E-mail or Password is incorrect!'
                    ]);
                }
              } else{
                    return response()->json([
                        'status' => 401,
                        'messages' => 'User not found!'
                ]);
              }
           }
    }

     //profile page
    public function profile(){
        return view('profile');
    }

    //logout method
    public function logout(){
        if(session()->has('loggedInUser')){
            session()->pull('loggedInUser');
            return redirect('/login');
        }
    }

kernel.php

protected $middlewareAliases = [

 'LoginCheck' => \App\Http\Middleware\LoginCheck::class,
    ];

LoginCheck.php-中间件

public function handle(Request $request, Closure $next): Response
    {
        if(!session()->has('loggedInUser') && ($request->path() != '/login' && $request->path() !='/register')){
            return redirect('/login');
        }
        if(session()->has('loggedInUser') && ($request->path() == '/login' || $request->path() =='/register')){
            return back();
        }
        return $next($request)->header('Cache-Control', 'no-cache, no-store, max-age=0,must-revalidate')
        ->header('Pragma', 'no-cache')->header('Expires','Sat 01 Jan 1990 00:00:00 GMT');
    }

请帮我更新登录表格。 提前谢谢你。

javascript jquery ajax laravel
© www.soinside.com 2019 - 2024. All rights reserved.