【问题标题】:Laravel Auth::check() only returns true on post pageLaravel Auth::check() 仅在帖子页面上返回 true
【发布时间】:2014-02-06 18:30:27
【问题描述】:

我是 Laravel 的新手,在简单的登录和授权页面上遇到了一些困难。 我跟着 laracasts.com 的一个很棒的视频教程(该教程的学分,真的很有帮助)。

我的情况:
在对我的页面实施授权时,登录后的页面通过了授权检查。 所以:loginform > press button > 你现在已经登录了。

我的问题:
在我按下后退按钮并刷新后,它仍然提供登录表单。不应该。

routes.php

<?php

Route::get('login', 'SessionsController@create');

Route::get('logout', 'SessionsController@destroy');

Route::resource('users', 'UsersController');

Route::resource('sessions', 'SessionsController');

Route::get('admin', function(){
    return 'Admin page';
})->before('auth');



Route::get('dashboard', ['before' => 'auth', function(){

    return 'Dashboard';
}]);

SessionsController.php:

<?php

class SessionsController extends BaseController{


    public function create()
    {

        if ( Auth::check() )
        {
            Redirect::to('/admin');
        }


        return View::make('sessions.create');
    }


    public function store()
    {
        if( Auth::attempt(Input::only('email', 'password')) )
        {
            // if(Auth::check())
            // {
            //  return 'check worked!';
            // }
            return 'Welcome ' . Auth::user()->username; //You are now logged in
        }

        return 'Failed';
    }

    public function destroy()
    {
        Auth::logout();

        return Redirect::to('sessions.create');
    }
}

用户.php

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    public $timestamps = true;


    protected $fillable = ['username', 'email', 'password'];
    protected $guarded = ['id'];

    public static $rules = [
        'username' => 'required',
        'password' => 'required'

    ];

    public $errors;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }


    public function isValid()
    {

        $validation = Validator::make($this->attributes, static::$rules );

        if($validation->passes() )
            return true;

        $this->errors = $validation->messages();
        return false;


    }

}

create.blade.php

@extends('layouts.default')
@section('content')


    <h1>Create new user</h1>


    {{Form::open([ 'route' => 'users.store' ]) }}

        <div>
            {{Form::label('username', 'Username: ')}}
            {{Form::text('username')}}
            {{$errors->first('username')}}

        </div>

        <div>
            {{Form::label('password', 'Password: ')}}
            {{Form::password('password')}}
            {{$errors->first('password')}}

        </div>

        <div>
            {{Form::submit('Create User')}}
        </div>

    {{Form::close() }}
@stop

可以这么说:它永远不会进入“管理员”路线。

【问题讨论】:

    标签: authentication laravel laravel-4


    【解决方案1】:

    您的验证码正确且有效。你的 Laravel 应用程序、Web 服务器甚至 PHP 的任何其他部分都出了问题。

    由于我们没有看到您的所有代码,我们只能猜测,所以我的第一个可能是 Session 未正确存储。当前登录的用户存储在 Laravel Session 中。因此,请检查您的会话驱动程序,如果它在“本机”中,请将其更改为“数据库”,但您必须创建一个会话表,查看文档。如果您已经在使用“数据库”,请将其改回“本机”甚至“文件”。

    【讨论】:

    • 感谢您快速回复并确认代码正确无误。我现在尝试了几个会话驱动程序,但这些似乎都没有像我期望的那样工作。我尝试了“本机”、“数据库”和 cookie。我没有比这更多的代码了。我使用 wamp 的 php.ini,会不会有问题?
    • 我的网络服务器不支持安装 Laravel 的文件夹的会话 cookie。
    • 你用的是什么服务器?我貌似也是同样的问题
    【解决方案2】:

    而不是运行

     return 'Welcome ' . Auth::user()->username; //You are now logged in
    

    请尝试

     return Redirect::to('admin');
    

    【讨论】:

    • 不太可能工作,因为 OP 尝试手动进入管理路由,它只是将浏览器再次发送到身份验证。
    • 代码对我来说看起来非常好,这就是我要求尝试它的原因。我想把它放在评论中,但它会破坏代码格式。我需要检查是否是因为 Auth::check() 有问题或其他原因。
    猜你喜欢
    • 2015-03-03
    • 2018-01-07
    • 2019-05-06
    • 2018-09-08
    • 2017-11-27
    • 2016-06-08
    • 2016-08-18
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多