wp-kit/auth

A wp-kit component that handles authentication

2.0.2 2020-01-31 23:43 UTC

This package is not auto-updated.

Last update: 2024-11-30 22:12:58 UTC


README

This is a wp-kit component that handles authentication.

wp-kit/auth was built to work with Themosis as currently there are no authentication middlewares built into Themosis however with illuminate/routing built into Themosis, we are able to run Middleware on Routes and Route Groups.

wp-kit/auth achives compatibility with illuminate/auth by providing a UserProvider that integrates directly with WordPress to authenticate users.

wp-kit/auth comes aliased with four types of Middleware:

wp-kit/auth comes with a AuthenticatesUsers Trait just like wp-kit/foundation so you can use this trait inside Controllers so you can use traditional form authentication just like in Laravel.

Installation

Install via Composer in the root of your Themosis installation:

composer require "wp-kit/auth"

Setup

Add Service Provider(s)

Just register the service provider and facade in the providers config and theme config:

//inside theme/resources/config/providers.config.php

return [
    //
    WPKit\Auth\AuthServiceProvider::class
    //
];

Add Config File

Note: This will be changing to a traditional config file similar to that found in Laravel once the UserProvider Guard has been built

The recommended method of installing config files for wp-kit components is via wp kit vendor:publish command.

First, install WP CLI, and then install this component, wp kit vendor:publish will automatically be installed with wp-kit/utils, once installed you can run:

wp kit vendor:publish

For more information, please visit wp-kit/utils.

Alternatively, you can place the config file(s) in your theme/resources/config directory manually.

Allowing Headers

If using BasicAuth middleware, make sure you add the following line to your .htaccess file to allow Authorization headers:

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

Usage

You can activate Middleware on the route group or route itself:

Middleware on Group

// inside themosis-theme/resources/providers/RoutingService.php

namespace Theme\Providers;

use Themosis\Facades\Route;
use Themosis\Foundation\ServiceProvider;

class RoutingService extends ServiceProvider
{
    /**
     * Define theme routes namespace.
     */
    public function register()
    {
        Route::group([
	        'middleware' => [
			'start_session',
			'auth',
	        	//'auth.basic',
			//'auth.wp_login',
			//'auth.token'
		],
            	'namespace' => 'Theme\Controllers'
        ], function () {
        	require themosis_path('theme.resources').'routes.php';
        });
    }
}

Middleware on Route

// inside themosis-theme/resources/routes.php

Route::get('home', function(Input $request)
{
    return view('welcome');
    
})->middleware('auth.wp_login');

Using Traits in Controllers

The AuthenticatesUsers trait handles everything for logging the user in using a custom form.

namespace Theme\Controllers;

use Themosis\Route\BaseController;
use WPKit\Auth\Traits\AuthenticatesUsers;
use WPKit\Auth\Traits\ValidatesRequests;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */
    use AuthenticatesUsers, ValidatesRequests;
    
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';
    
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    
}

Make sure you add routes:

// an example in routes.php

Route::get('account', 'Example@showLoginForm');
Route::post('account', 'Example@login');
Route::get('logout', 'Example@logout');

Make sure you add a login form view:

<-- Inside resources/view/auth/login.php -->
<?php foreach( $errors as $field => $error ) : ?>
	<strong>There was an error with field: <?php echo $field; ?></strong>
<?php endforeach; ?>
<form method="post">
	<div>
		<label>Username</label>
		<input type="text" name="email" placeholder="Username" />
	</div>
	<div>
		<label>Password</label>
		<input type="password" name="password" placeholder="Password" />
	</div>
	<div>
		<input type="submit" value="Submit" />
	</div>
</form>

Config

Please install and study the default config file as described above to learn how to use this component.

Get Involved

To learn more about how to use wp-kit check out the docs:

View the Docs

Any help is appreciated. The project is open-source and we encourage you to participate. You can contribute to the project in multiple ways by:

  • Reporting a bug issue
  • Suggesting features
  • Sending a pull request with code fix or feature
  • Following the project on GitHub
  • Sharing the project around your community

For details about contributing to the framework, please check the contribution guide.

Requirements

Wordpress 4+

PHP 5.6+

License

wp-kit/auth is open-sourced software licensed under the MIT License.