简体   繁体   中英

Laravel 5.4 - Auto login after registration

I'm trying to automatically login after the user registers.

My user registration method:

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)
    ]);
}

My User_entity Model:

    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');
        } 
}

User registration is successful and is recorded in the database. But to login following error is displayed:

ErrorException in SessionGuard.php line 407: Argument 1 passed to Illuminate\\Auth\\SessionGuard::login() must be an instance of Illuminate\\Contracts\\Auth\\Authenticatable, instance of App\\User_entity given, called in /home/ali/www/brandmashhoor/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 392 and defined

Can anyone tell me what is the correct problem.

You User_entity must extend Illuminate\\Foundation\\Auth\\User for the login to work. In the default User model this is aliased as Authenticatable .

Currently you're extending Eloquent , but Authenticatable extends Illuminate\\Database\\Eloquent\\Model which will take care of that for you.

Try this in your User_entity model:

use Illuminate\Foundation\Auth\User as Authenticatable;

class User_entity extends Authenticatable
{
    ....

In RegisterController you can use following code to login after the registration.

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);
}

instead of return response(['success' => true]);

use below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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