GithubHelp home page GithubHelp logo

ahsankhatri / wordpress-auth-driver-laravel Goto Github PK

View Code? Open in Web Editor NEW
52.0 4.0 36.0 25 KB

A package to provide wordpress users in laravel authentication system.

License: MIT License

PHP 100.00%
laravel wordpress laravel-package laravel-authorization php laravel-guard

wordpress-auth-driver-laravel's Introduction

Wordpress Auth Driver for Laravel

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Intelligence Status License

Laravel wordpress-auth-driver-laravel
5.2 to 5.5 ^1.0
5.6 to 8.x ^2.0

Installation

To install this package you will need

The best way to install this package is with the help of composer. Run

composer require ahsankhatri/wordpress-auth-provider

or install it by adding it to composer.json then run composer update

"require": {
    "ahsankhatri/wordpress-auth-provider": "^2.0",
}

Setup

Once you have installed this package from the composer, make sure to follow the below steps to configure.

To register authentication guard.

config/auth.php
'guards' => [
    ...,
    'wordpress' => [
        'driver' => 'session',
        'provider' => 'wordpress',
    ],
'providers' => [
    ...,
    'wordpress' => [
        'driver' => 'eloquent.wordpress',
        'model' => MrShan0\WordpressAuth\Models\WordpressUser::class,
    ],
'passwords' => [
    ...,
    'wordpress' => [
        'provider' => 'wordpress',
        'table' => 'password_resets',
        'expire' => 60,
    ],

Publish config file (optional)

php artisan vendor:publish --provider="MrShan0\WordpressAuth\WordpressAuthServiceProvider"

It will publish config file (config/wordpress-auth.php) where you can define your own connection type e.g wp-mysql. Make sure to fill prefix in config/database.php for wp_ prefix in your tables if you're using prefix in wordpress tabels.

For example:

'wp-mysql' => [
    'driver' => 'mysql',
    'host' => env('WP_DB_HOST', '127.0.0.1'),
    'port' => env('WP_DB_PORT', '3306'),
    'database' => env('WP_DB_DATABASE', 'forge'),
    'username' => env('WP_DB_USERNAME', 'forge'),
    'password' => env('WP_DB_PASSWORD', ''),
    'unix_socket' => env('WP_DB_SOCKET', ''),
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => 'wp_',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
],

Add following option along if using Laravel v7 (optional)

    // ...
    'url' => env('DATABASE_URL'),
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],

Configuration

password_resets table (from Laravel default auth mechanism) is required to hold reset password token. If you do not have password_resets table then use this migration instead

<?php

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

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->string('email')->index();
            $table->string('token');
            $table->timestamp('created_at')->nullable();
        });
    }

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

Extension

Alternatively, if you want to use a custom user model, you should have it extend MrShan0\WordpressAuth\Models\WordpressUser and specify the name of your model in config/auth.php under providers -> wordpress -> model.

Customization

If you've renamed your user_email column of wordpress database, you need to first publish configurations of this package if you've not already, extend the model as mentioned above and make sure you've override your changes in your $fillable property and config/wordpress-auth.php config file which is being used for authentication scaffolding and sending notifications.

Usage

You need to define wordpress guard explicitly to load the driver.

Examples

Auth::guard('wordpress')->loginUsingId(5);

// or login using email and password
Auth::guard('wordpress')->attempt([
    'user_email' => '[email protected]',
    'user_pass' => 'quickbrownfox'
]);

// get user object
Auth::guard('wordpress')->user();

// Update wordpress compatible password
$user->user_pass = app('wordpress-auth')->make('new_password');
$user->save();

// logout
Auth::guard('wordpress')->logout();

You may also change default guard in config/auth.php then your code will look like

Auth::loginUsingId(5);

If you haven't set default guard and wanted to take advantage of Password Resets (Auth Scaffolding) in laravel. You may need to define guard and broker explicitly in Auth/ForgotPasswordController.php and Auth/ResetPasswordController.php as

/**
 * Get the broker to be used during password reset.
 *
 * @return \Illuminate\Contracts\Auth\PasswordBroker
 */
public function broker()
{
    return \Password::broker('wordpress');
}

/**
 * Get the guard to be used during password reset.
 *
 * @return \Illuminate\Contracts\Auth\StatefulGuard
 */
protected function guard()
{
    return \Auth::guard('wordpress');
}

Changelog

CHANGELOG

Credits

Thanks to the community of Laravel.

Copyright and License

Copyright (c) 2016 Ahsaan Muhammad Yousuf, MIT License

wordpress-auth-driver-laravel's People

Contributors

ahsankhatri avatar chrisrollins65 avatar jeffreyvr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

wordpress-auth-driver-laravel's Issues

Support for Laravel 5.8.*

Thanks for the update to Laravel 5.8, but I can't install it because I'm on Laravel 5.8.14.

Would it be possible to update the composer.json to accept 5.8.* or do you need to fix anything before you can do that?

Here's the error I receive when running composer require ahsankhatri/wordpress-auth-provider


Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: don't install ahsankhatri/wordpress-auth-provider v2.1.1
    - Conclusion: don't install ahsankhatri/wordpress-auth-provider v2.1.0
    - Conclusion: remove laravel/framework v5.8.14
    - Installation request for ahsankhatri/wordpress-auth-provider ^2.1 -> satisfiable by ahsankhatri/wordpress-auth-provider[v2.1.0, v2.1.1, v2.x-dev].
    - Conclusion: don't install laravel/framework v5.8.14
    - ahsankhatri/wordpress-auth-provider v2.x-dev requires laravel/framework >=5.6 <=5.8 -> satisfiable by laravel/framework[5.6.x-dev, 5.7.x-dev, v5.8.0].
    - Can only install one of: laravel/framework[5.6.x-dev, v5.8.14].
    - Can only install one of: laravel/framework[5.7.x-dev, v5.8.14].
    - Can only install one of: laravel/framework[v5.8.0, v5.8.14].
    - Installation request for laravel/framework (locked at v5.8.14, required as 5.8.*) -> satisfiable by laravel/framework[v5.8.14].

How can i set `user` model to `JWTSubject `

I code this:

\Config::set('jwt.user' , "MrShan0\WordpressAuth\Models\WordpressUser");
\Config::set('auth.providers.users.model', \MrShan0\WordpressAuth\Models\WordpressUser::class);

if (\Auth::guard('wordpress')->attempt($request->all())) 
{
        $user = \Auth::guard('wordpress')->user();
        $token = JWTAuth::fromUser($user);
}
 else 
{
        return response()->json(['result' => 'error.']);
}

return response()->json(['result' => $token]);

request api output this:

"Argument 1 passed to Tymon\JWTAuth\JWT::fromUser() must be an instance of Tymon\JWTAuth\Contracts\JWTSubject, instance of MrShan0\WordpressAuth\Models\WordpressUser given, called in /Library/WebServer/Documents/cn.51baopen/api/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 237"

getNameAttribute

Add this function to the model to show the WP username in the default Auth::user()->name function.


    /**
     * Usage to return user name
     * 
     * @return string
     */
    public function getNameAttribute(){
        return $this->user_login;
    }

Separate WP database

In my case, I want to use a separate DB for the WP tables and the Laravel tables.

Because this provider uses an Eloquent model, this can be specified in the Model by:
protected $connection = 'mysql-wp';

In config/database.php you can set-up an separate DB connection.

You can copy the Model from the vendor to your app directory and inject the line above in the model.

Maybe this is a nice feature to make configurable or add this to the readme?

L8 looking for email not user_email

Good afternoon,

I just finished setting up the package on a fresh Laravel 8 install and adjusted the .env to connect to the mysql connection of the wordpress database and made the changes specified in the read me. When I try logging in, it is still looking for email in the wp_ tables.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'email' in 'where clause' (SQL: select * from wp_users where email = [email protected] limit 1)

I've even tried changing the users $fillable to the standard wp_ requirements. user_login, user_email, user_pass

the config/database and config/app has also been changed to what the readme says. Any assistance would be great :).

Support 5.8?

can you update package to support laravel 5.8.

Username instead?

Is it possible for us to easily use the default wordpress logins instead? Im a bit confused why it says this is for using with wordpress auth, but then doesnt even use the wordpress username, which is not typically an email address.

Support for Laravel 6

This is what I'm looking for! I've tried installing this on Laravel 6 but it caused unexpected behavior.
I would like to see updates with support for Laravel 6. Thank you.

How to upgrade laravel 6.0

If upgrade laravel 6.0,Error this

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Conclusion: remove ahsankhatri/wordpress-auth-provider v2.1.2
    - Conclusion: don't install ahsankhatri/wordpress-auth-provider v2.1.2
    - Conclusion: don't install ahsankhatri/wordpress-auth-provider v2.1.1
    - Conclusion: don't install ahsankhatri/wordpress-auth-provider v2.1.0
    - Conclusion: don't install ahsankhatri/wordpress-auth-provider v2.0.0

Laravel 7.9+

There are quite a few minor releases of laravel 7 that this package seems to work fine in. Any reason why it's limited to only 7.0 - 7.8?

Support for PHP8

use by compose update

Problem 1
    - ahsankhatri/wordpress-auth-provider[v2.0.0, ..., v2.x-dev] require php ^7.1 -> your php version (8.0.3) does not satisfy that requirement.
    - Root composer.json requires ahsankhatri/wordpress-auth-provider ^2.0 -> satisfiable by ahsankhatri/wordpress-auth-provider[v2.0.0, ..., v2.x-dev].

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.