Laravel 5.4 - 注册后自动登录

时间:2017-03-03 19:55:57

标签: php laravel laravel-5.4

我正在尝试在用户注册后自动登录。

我的用户注册方法:

public function register_user(Request $request)
{
    if (!$request->ajax()) return false;

    $form_rule = [
        'email'     => 'required|email|max:255|unique:tdshop_user_entity',
        'password'  => 'required|min:6',
    ];

    $validator  = Validator::make($request->all(), $form_rule);
    $status     = $validator->fails();

    $form_errors = [];
    foreach($form_rule as $field=>$rules){
        $form_errors[$field] = $validator->errors()->first($field);
    }

    if(!$validator->fails()){
        $user = User_entity::create([
            'email'     => $request['email'],
            'password'  => bcrypt($request['password']),
        ]);
        Auth::loginUsingId($user->user_id);
    }

    return json_encode([
        'status'    => ($status)? 'fail' : 'success',
        'result'    => json_encode($form_errors)
    ]);
}

我的User_entity模型:

    namespace App;

    //use Illuminate\Database\Eloquent\Model;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    use Zizaco\Entrust\Traits\EntrustUserTrait;
    use Eloquent;

    class User_entity extends Eloquent
    {
        use Notifiable;
        use EntrustUserTrait;

        protected $table    = 'tdshop_user_entity';
        protected $primaryKey = 'user_id';
        protected $fillable = ['email','password','status'];
        protected $hidden = ['password', 'remember_token'];

        public function Attribute_set()
        {
            return $this->hasOne('App\Attribute_set');   
        }

        public function Attribute_entity()
        {
            return $this->hasMany('App\Attribute_entity');
        } 

        public function User_address_entity()
        {
            return $this->hasMany('App\User_address_entity');
        } 
}

用户注册成功并记录在数据库中。 但是显示登录跟随错误:

  

SessionGuard.php第407行中的ErrorException:   传递给Illuminate \ Auth \ SessionGuard :: login()的参数1必须是Illuminate \ Contracts \ Auth \ Authenticatable的实例,App / User_entity的实例,在/ home / ali / www / brandmashhoor / vendor / laravel / framework中调用第392行/src/Illuminate/Auth/SessionGuard.php并定义

谁能告诉我什么是正确的问题。

2 个答案:

答案 0 :(得分:1)

User_entity必须延长Illuminate\Foundation\Auth\User才能使登录正常运行。在默认的User模型中,别名为Authenticatable

目前您正在扩展Eloquent,但Authenticatable会延长Illuminate\Database\Eloquent\Model,这将为您解决此问题。

User_entity模型中尝试此操作:

use Illuminate\Foundation\Auth\User as Authenticatable;

class User_entity extends Authenticatable
{
    ....

答案 1 :(得分:0)

在RegisterController中,您可以使用以下代码在注册后登录。

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Response;



if ($validator->passes()) {
// Store your user in database
event(new Registered($user = $this->create($request->all())));           
return response(['success' => true,'redirect' => 'dashboard'],    Response::HTTP_OK);
}

而不是 返回响应(['success'=> true]);

在下面使用。

Auth::login($user);
return response(['success' => true,'redirect' => 'dashboard'],     Response::HTTP_OK);