Skip to content

Instantly share code, notes, and snippets.

@mortenege
Last active February 28, 2018 08:39
Show Gist options
  • Save mortenege/85c2732808aac75260061ab9e5eac62f to your computer and use it in GitHub Desktop.
Save mortenege/85c2732808aac75260061ab9e5eac62f to your computer and use it in GitHub Desktop.
Use a Wordpress User table and login logic within Laravel

Use WP user table and password control in Laravel

This is useful for anybody who is running Laravel Framework, but for some reason keep their User logic in Wordpress.

Note: This assumes any other Wordpress to Laravel migrations have already been taken care of (like adding remember_token to user table and setting up a User-model that extneds Illuminate\Contracts\Auth\Authenticatable)

requires either MikeMcLin\WpPassword or Hautelook\Phpass

PS. This can probably also be accomplished by swapping out the app-hasher for the entire project!

All that is needed is to

  • extend the User Provider in Laravel
  • modify the config to use the new provider
  • register the new provider in the AuthServiceProvider
  • edit (or extend the functionality) everywhere where 'email'/'username' should be 'user_email' and 'password' should be 'user_pass'

1. Create a UserProvider

Create a Class WPuserProvider as app/Extensions/WPUserProvider.php and extend Illuminate\Auth\EloquentUserProvider

use Illuminate\Auth\EloquentUserProvider;
use Hautelook\Phpass\PasswordHash;
class WPUserProvider extends EloquentUserProvider {

Override the method validateCredentials

public function validateCredentials(Authenticatable $user, array $credentials){
    $plain = $credentials['user_pass'];
    $hasher = new PasswordHash(8, true); // same settings as WP
    return $hasher->check($plain, $user->user_pass);
}

2. modify the config file

open and edit config/auth.php

'defaults' => [
    'guard' => 'wpguard',
    'passwords' => 'users'
],
...
'guards' => [
    'wpguard' => [
        'driver' => 'session',
        'provider' => 'wpusers'
    ],
    ...
...
'providers' => [
    'wpusers' => [
        'driver' => 'wpdriver',
        'model' => App\User::class
    ],
    ...
...

3. Register the new UserProvider

Open and edit App\Providers\AuthServiceProvider.php in method boot() add the following

Auth::provider('wpdriver', function ($app, array $config) {
    return new WPUserProvider($app['hash'], $config['model']);
});

4. Find and replace Laravel attributs with Wordpress attribues

Find in all necessary files (most of them probably listed below) where Laravel's 'email' and 'password' attributes are listed and replace them with 'user_email' and 'user_pass'

Interesting files\classes regarding this

  • Illuminate\Auth\EloquentUserProvider
  • Illuminate\Contracts\Auth\UserProvider
  • Illuminate\Contracts\Auth\Authenticatable
  • Illuminate\Auth\SessionGuard
  • Illuminate\Contracts\Auth\Guard
  • App\Http\Middleware\RedirectIfAuthenticated
  • App\Http\Controllers\Auth\LoginController
  • Illuminate\Foundation\Auth\AuthenticatesUsers
  • resources\views\auth*.*

References

https://code.tutsplus.com/tutorials/how-to-create-a-custom-authentication-guard-in-laravel--cms-29667

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment