Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User authentication in Laravel from xenforo database with using several tables

I have 2 databases: one from Laravel and other from XenForo 2. On Laravel, registration should not take place, but only on the forum in order to reduce data duplication and eliminate the out of sync if it happens for some reason.

There are 2 tables in XF: xf_users - stores basic user information, and xf_user_authenticate - stores a serialized array string with a password hash.

It is necessary to authenticate through these 2 tables. User entered username/password correctly - logged into Laravel CMS.

Connecting to a third-party database is simple: manually register the connection in the User model as follows:

protected $ connection = 'forum';
protected $ table = 'xf_users';
public $ timestamps = false;

I also created the Password model for obtaining passwords from the xf_user_authenticate table:

class Password extends Model
{
     protected $ fillable = ['data'];
     protected $ connection = 'forum';
     protected $ table = 'xf_user_authenticate';
     public $ timestamps = false;
}

Further, as I understand it, I need to make a custom guard and provider, and here I can no longer understand what to do next...

How can I use these 2 tables for authentication in the laravel engine?

like image 906
Vladimir Gonchar Avatar asked Aug 29 '19 09:08

Vladimir Gonchar


1 Answers

I solved this problem by means changing User model:

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = ['username', 'email', 'password', 'user_state'];
    protected $connection = 'forum';
    protected $table = 'xf_user';
    protected $primaryKey = 'user_id';
    public $timestamps = false;

    public function getAuthPassword(){
        $password = Password::where('user_id', '=', $this->user_id)->select('data')->first();
        return unserialize($password->data)['hash'];
    }
}

Using this model, user from XF logins fine

like image 178
Vladimir Gonchar Avatar answered Nov 16 '22 06:11

Vladimir Gonchar