当前位置:网站首页>Laravel8 authentication login

Laravel8 authentication login

2022-06-12 05:33:00 Python's path to becoming a God

// Sign in 
    public function loginDo(CheckLogin $request)
    {
        $postData=$request->all();
//        auth Sign in 
        $data=auth()->attempt(['username'=>$postData['username'],'password'=>$postData['password']]);
        if(!$data){
            return redirect('admin/login')->withErrors(['error'=>' Wrong user name or password ']);
        }
        session(['username'=>$postData['username']]);
        $id=auth()->id();
        $res=User::with(['Role','Role.Node'])->where('id',$id)->get()->toArray();
//         Check all permissions according to roles 
        $datas=[];
        foreach($res as $val) {
            $datas = $val['role']['node'];
        }
        $routeName=array_column($datas,'route_name');
//         Permission cache 
        session(['node'=>$routeName]);
        return redirect('index');
    }

middleware :

  public function handle(Request $request, Closure $next)
    {
//         Check whether the user is logged in 
        if(!auth()->check()){
            return redirect('admin/login')->withErrors(['error'=>' Please log in ']);
        }
        return $next($request);
    }
//         Get the permissions in the cache 
        $node=session('node');
//        dd($node);
//         Authority verification 
        if(auth()->user()['username']!=config('admin.admin')){
            if(!in_array($request->path(),$node)){
                return redirect('welcome')->withErrors(' Insufficient authority ');
            }
        }
        return $next($request);
    }

原网站

版权声明
本文为[Python's path to becoming a God]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010613199604.html