laravel 4.2 并记住我更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23134702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:22:37  来源:igfitidea点击:

laravel 4.2 and remember me update

phpauthenticationlaravellaravel-4remember-me

提问by user2020432

I add getRememberToken, setRememberToken, getRememberTokenName to User model, add "remember_token" column to user DB, run composer update.

我将 getRememberToken、setRememberToken、getRememberTokenName 添加到用户模型,将“remember_token”列添加到用户数据库,运行 composer update。

Result: no errors, log in with remember me option goes same as it was before - "remember_token" in DB is still null. + i do not see retrieveByToken()and updateRememberToken()in Illuminate\Auth\UserProviderInterface

结果:没有错误,使用记住我选项登录与之前相同 - DB 中的“remember_token”仍然为空。+我没有看到retrieveByToken(),并updateRememberToken()Illuminate\Auth\UserProviderInterface

What goes wrong? I did or did not do something? Composer for some reason did not goes with "laravel/framework": "4.2.*" as it should? v4.2 did not have fix for remember me problem? Auth::attempt()should be modified somehow to generate and put data in remember_token? Auth::logout()not touching remember_token (i put random data and it still same after logout and login with or without remember me) Where to dig?

出了什么问题?我做了还是没有做某事?由于某种原因,作曲家没有像它应该的那样使用 "laravel/framework": "4.2.*" 吗?v4.2 没有修复记住我的问题?Auth::attempt()应该以某种方式修改以生成并将数据放入remember_token?Auth::logout()不接触remember_token(我放了随机数据,在注销和登录后仍然相同,无论是否记住我)在哪里挖掘?

    <?php

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

    class User extends Eloquent implements UserInterface, RemindableInterface {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    protected $fillable = ['name','email','password','temp_password','code','status', 'remember_token'];

    /**
     * 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 scopeCode($query, $code)
    {
            return $query->whereCode($code);
    }

    public function scopeEmail($query, $email)
    {
            return $query->whereEmail($email);
    }

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }           
   }

回答by Aaron Adams

Here's my Usermodel for Laravel dev:

这是我User的 Laravel 开发模型:

<?php

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

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * 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');

    protected $fillable = array('email', 'password');

}

A bunch of things to note here:

这里有一些需要注意的事项:

  • Traits! Five of your methods should really be coming from UserTrait, and another from RemindableTrait.
  • In composer.jsonyou'll need to set minimum-stabilityto devand then php composer.phar update(or, if you experience memory errors like I often do, rm -rf vendor/ && php composer.phar install). Note that betawon't currently work with the above user model, because RemindableTraithas actually been moved since the BETA1release; so I recommend the devbranch until the release of 4.2.0-BETA2.
  • Also note that currently there is a 4.2.0-BETA1version of laravel/framework, but no 4.2.*release of laravel/laravel. So if you try (like I did, for quite some time, in vain) to create-project laravel/laravel --stability=beta, you'll continue to get 4.1.*as of right now, even though the 4.2 beta is out. It's all very confusing.
  • 特质!你的五个方法应该真的来自UserTrait,另一个来自RemindableTrait.
  • composer.json您需要设置minimum-stabilitydev然后php composer.phar update(或者,如果您像我经常那样遇到内存错误,rm -rf vendor/ && php composer.phar install)。请注意,beta目前不适用于上述用户模型,因为RemindableTraitBETA1发布以来实际上已被移动;所以我推荐dev分支直到4.2.0-BETA2.
  • 另外请注意,目前有一个4.2.0-BETA1版本laravel/framework,但没有4.2.*释放laravel/laravel。因此,如果您尝试(像我一样,在很长一段时间内徒劳无功)到,即使 4.2 测试版已经出局create-project laravel/laravel --stability=beta,您仍将继续获得4.1.*现在的结果。这一切都非常令人困惑。

Finally, don't forget your user schema (stub at php artisan auth:reminders-table):

最后,不要忘记您的用户架构(存根在php artisan auth:reminders-table):

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration {

    /**
     * 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('remember_token', 100)->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

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

}

And your reminders controller (stub at php artisan auth:reminders-controller):

和您的提醒控制器(存根在php artisan auth:reminders-controller):

<?php

class RemindersController extends Controller {

    /**
     * Display the password reminder view.
     *
     * @return Response
     */
    public function getRemind()
    {
        return View::make('password.remind');
    }

    /**
     * Handle a POST request to remind a user of their password.
     *
     * @return Response
     */
    public function postRemind()
    {
        switch ($response = Password::remind(Input::only('email')))
        {
            case Password::INVALID_USER:
                return Redirect::back()->with('error', Lang::get($response));

            case Password::REMINDER_SENT:
                return Redirect::back()->with('status', Lang::get($response));
        }
    }

    /**
     * Display the password reset view for the given token.
     *
     * @param  string  $token
     * @return Response
     */
    public function getReset($token = null)
    {
        if (is_null($token)) App::abort(404);

        return View::make('password.reset')->with('token', $token);
    }

    /**
     * Handle a POST request to reset a user's password.
     *
     * @return Response
     */
    public function postReset()
    {
        $credentials = Input::only(
            'email', 'password', 'password_confirmation', 'token'
        );

        $response = Password::reset($credentials, function($user, $password)
        {
            $user->password = Hash::make($password);

            $user->save();
        });

        switch ($response)
        {
            case Password::INVALID_PASSWORD:
            case Password::INVALID_TOKEN:
            case Password::INVALID_USER:
                return Redirect::back()->with('error', Lang::get($response));

            case Password::PASSWORD_RESET:
                return Redirect::to('/');
        }
    }

}

回答by Oguzhan

Just check my answer at the end of this thread!

只需在本主题末尾查看我的答案即可!

http://laravel.io/forum/04-21-2014-remember-token.

http://laravel.io/forum/04-21-2014-remember-token

if you look at the Guard.php file the regenerating-token function is missing.

如果您查看 Guard.php 文件,则缺少重新生成令牌的功能。

回答by PeterKA

I am using Laravel 4.2; I have an input with name="rememberme"on my login view with the following code in my controller:

我正在使用 Laravel 4.2;我name="rememberme"在我的登录视图中有一个输入,在我的控制器中使用以下代码:

if(Auth::attempt($logindata, Input::has('rememberme'))) 
{
    return Redirect::intended('/');
} 

It works great; once I log in my session does not end until I explicitly log out.

效果很好;一旦我登录,我的会话就不会结束,直到我明确注销。

回答by Muhammad

Laravel 4.2 Remember me steps:

Laravel 4.2 记住我的步骤:

1- your main user's table which used to check login must have rememberToken() in schema

1- 用于检查登录的主用户表必须在架构中包含 rememberToken()

Schema::create('Users',function($table)
{
    $table->increments('id');
    $table->string('email');
    $table->string('password');
    $table->bigInteger('organizer_id');
    $table->boolean('status');
    $table->rememberToken();
    $table->timestamps();
    $table->softDeletes();

});

2- View: checkbox for remember me, like i have

2-查看:记住我的复选框,就像我一样

{{Form::label('remember','Remember Me')}}

3- Login Controller:

3- 登录控制器:

if (\Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password') ), (Input::get('remember')==1 ? true : false)))
{
   return Redirect::to('_admin/home')->with('message', 'You are now logged in!');
} else {
  return \Redirect::back()
    ->with('message', 'Your username/password combination was incorrect')
    ->withInput();
}

(Input::get('remember')==1 ? true : false)this line of code should work for remember me

(Input::get('remember')==1 ? true : false)这行代码应该可以记住我