wp-kit/auth
This is a wp-kit component that handles authentication., (*1)
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
., (*2)
wp-kit/auth
achives compatibility with illuminate/auth
by providing a UserProvider that integrates directly with WordPress to authenticate users., (*3)
wp-kit/auth
comes aliased with four types of Middleware
:, (*4)
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., (*5)
Installation
Install via Composer
in the root of your Themosis
installation:, (*6)
composer require "wp-kit/auth"
Setup
Add Service Provider(s)
Just register the service provider and facade in the providers config and theme config:, (*7)
//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, (*8)
The recommended method of installing config files for wp-kit
components is via wp kit vendor:publish
command., (*9)
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:, (*10)
wp kit vendor:publish
, (*11)
For more information, please visit wp-kit/utils
., (*12)
Alternatively, you can place the config file(s) in your theme/resources/config
directory manually., (*13)
If using BasicAuth
middleware, make sure you add the following line to your .htaccess
file to allow Authorization
headers:, (*14)
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
, (*15)
Usage
You can activate Middleware
on the route group or route itself:, (*16)
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., (*17)
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:, (*18)
// 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:, (*19)
<-- 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., (*20)
Get Involved
To learn more about how to use wp-kit
check out the docs:, (*21)
View the Docs, (*22)
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:, (*23)
- 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., (*24)
Requirements
Wordpress 4+, (*25)
PHP 5.6+, (*26)
License
wp-kit/auth is open-sourced software licensed under the MIT License., (*27)