dongxing4643 2014-12-22 20:12
浏览 11
已采纳

Laravel陷入了身份验证循环

I am stuck in an authentication loop, and for the life of me cannot understand why.

Route::get('/', function()
{
    return View::make('hello');
    //return 'Hello ' . Auth::user()->first_name . '!';
});

Route::get('/login', function() {
    return View::make('login'); 
});

Route::post('/login', function() {

    // @TODO: Validate the login details!

    Auth::attempt(array(
        'email' => Input::get('email'),
        'password' => Input::get('password')
        ));

    return Redirect::to('/');
});


Route::group(array('before' => 'auth'), function() {

    Route::get('/home', function()
    {
        //return View::make('hello');
        return 'Hello ' . Auth::user()->first_name . '!';
    });


    /*
    |--------------------------------------------------------------------------
    | API Route Handlers
    |--------------------------------------------------------------------------
    |
    | The API Routes.
    |
    */

    Route::group(array('prefix' => 'api/v1'), function() {

        Route::resource('events', 'EventsController');

    });

});

Here is the sequence of events:

I go to localhost:8000/home it works and redirects me to /login.

I then move on to log in.

It redirects me to the / if I then go to /home it still takes me to /login

Ideal case:

Go to /home only to be redirected to /login

I log in then.

It redirects me to /

I can then move freely between / and /home without the worry of logging in.

EDIT 1

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');

            $table->string('email')->unique();
            $table->string('password');

            $table->string('first_name');
            $table->string('last_name');
            $table->string('id_number')->unique();

            $table->enum('user_type', array('option 1', 'option 2', 'option 3'));

            $table->rememberToken();

            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
  • 写回答

1条回答 默认 最新

  • duanliaoyu8419 2014-12-22 22:14
    关注

    Fixed it - The problem was that Auth::attempt()'s value wasn't being set.

    Here is my new updated version of the code.

    Route::get('/', function()
    {
        return View::make('hello');
        //return 'Hello ' . Auth::user()->first_name . '!';
    });
    
    Route::get('/login', function() {
        return View::make('login'); 
    });
    
    Route::post('/login', function() {
    
        // @TODO: Validate the login details!
    
        $valid = Auth::attempt(array(
            'email' => Input::get('email'),
            'password' => Input::get('password')
            ));
    
        if ($valid)
        {
            return Redirect::to('/home');
        } 
        else 
        {
            return Redirect::to('/');
        }
    
    });
    
    
    Route::group(array('before' => 'auth'), function() {
    
        Route::get('/home', function()
        {
            //return View::make('hello');
            return 'Hello ' . Auth::user()->first_name . '!';
        });
    
    
        /*
        |--------------------------------------------------------------------------
        | API Route Handlers
        |--------------------------------------------------------------------------
        |
        | The API Routes.
        |
        */
    
        Route::group(array('prefix' => 'api/v1'), function() {
    
            Route::resource('events', 'EventsController');
    
        });
    
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥15 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)