简体   繁体   English

Laravel身份验证

[英]Laravel Authentication

$this->beforeFilter(function()
        {
             Config::set('auth.model', 'User');
             if ((Auth::guest())) {
            //dd(Auth::guest());
            $msg3  = "Please Login First";
            // dd("ok");
            return Redirect::to(Request::root().'/auth')->with("error_message", $msg3);

            }

        });

I am using a Auth::guest() function for stopping unauthorized access but when I hit URL unauthorized access works fine but login fails... without this portion of code login using Auth::attempt() works fine... what can be the problem?? 我正在使用Auth :: guest()函数来阻止未经授权的访问,但是当我点击URL时,未经授权的访问工作正常但登录失败...没有这部分代码登录使用Auth :: attempt()工作正常...什么可以是问题??

Edit //AuthController for login 编辑 // AuthController进行登录

<?php

// uncomment this to use namespaced controller
//namespace Modules\admin\Controllers;

class AuthController extends \BaseController
{
    public function __construct()
    {
        $this->beforeFilter(function()
        {
            Config::set('auth.model', 'User');
        });
    }



    public function getIndex()
    {

        return \View::make("login");
    }


    public function postLogin()
    {
        $msg7  = "Invalid email address or password";
        // $results['userData'] = user::get();
        // dd($results);
        //$password=Input::get('password');
        //$password=Hash::make('secret');

        $userData = array(
        'email' => Input::get('email'),
        'password'=> Input::get('password')
        );

         $email=Input::get('email');
        // $password=Input::get('password');
  //    dd($password);
        //dd($userData);
        $rules = array(
        'email' => 'required|email',
        'password' => 'required|min:5'
        );



        $remember=Input::get('remember');

        $remember_me=false;
        if (!empty($remember)) {
            $remember_me=true;
        }

        $validator = Validator::make(Input::get(), $rules);
        if($validator->fails()){

            // redirect to the login page with validation errors
            return Redirect::to(Request::root().'/auth')->withErrors($validator)->withInput();


        }


        else{

            //dd(Auth::attempt($userData));
            // check authentication
            if(Auth::attempt($userData,$remember_me)){
                // dd(Auth::attempt($userData));                // redirect to the dashboard page
                 //dd($userData);

                return Redirect::to(Request::root().'/home');

            }else{
                //dd($userData);
                //DB::table('user')->insert($userData);             
                //dd("test");
                // redirect to the login page with error message
                return Redirect::to(Request::root().'/auth')->with("error_message", $msg7);
            }

        }
    }


// logout function
    public function getLogout()
    {
        // delete all data from sesstion
        Auth::logout();
        Session::flush();
        // redirect to login page
        return Redirect::to(Request::root().'/auth');
    }

}

// DashboardController where unauthorized access is blocked // DashboardController,阻止未经授权的访问

<?php

class DashboardController extends BaseController{

    public function __construct()
    {
        $this->beforeFilter(function()
        {
             Config::set('auth.model', 'User');


            if ((Auth::guest())) {
            //dd(Auth::guest());
            $msg3  = "Please Login First";
            // dd("ok");
            return Redirect::to(Request::root().'/auth')->with("error_message", $msg3);

            }

        });

    }


    public function getIndex()
    {
            //dd(Auth::check());

        return View::make('home');
    }

}

The problem is that you should exclude your action when you send your login form data. 问题是您应该在发送登录表单数据时排除您的操作。 If in your controller you have method postLogin to handle POST data from login form, you should use: 如果你的控制器中有postLogin方法来处理登录表单中的POST数据,你应该使用:

  $this->beforeFilter(function() {
          // here goes code of your filter
  }, array('except' => 'postLogin'));

Reference 参考

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

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