Package Data | |
---|---|
Maintainer Username: | risan |
Maintainer Contact: | risanbagja@gmail.com (Risan Bagja Pradana) |
Package Create Date: | 2016-01-03 |
Package Last Update: | 2016-03-09 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:03:44 |
Package Statistics | |
---|---|
Total Downloads: | 23 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Stateful authentication service provider for the latest Laravel 5.2 version.
Laravel already offers a handy way to provide user authentication functionality. Laravel framework ships with AuthenticatesUsers
trait which can be injected to your authentication controller. And actually, by default the provided AuthController
class are using this handy trait.
This package's intention is to replace the AuthenticatesUsers
trait from your authentication controller. By extracting the authentication logic into a seperate service provider, your authentication controller will be a lot more clean and readable.
This package is only for the latest Laravel 5.2 version and only supports the StatefulGuard
implementation.
This package relies on the following libraries:
To install this library using Composer, simply run the following command inside your Laravel project directory:
composer require risan/laravel-auth-service
Or you may also add risan/laravel-auth-service
package into your composer.json
file:
"require": {
"risan/laravel-auth-service": "~1.1"
}
Once the dependency is added, run the install command:
composer install
Once the package has been installed, you need to register package's service provider. Open your config/app.php
file, and add AuthService\AuthServiceProvider::class
into your providers
list like so:
'providers' => [
...
AuthService\AuthServiceProvider::class,
],
If you would like to use facade to access this package, you need to register the AuthService\Facades\AuthService::class
in aliases
directive. Open up your config/app.php
file, and update the aliases
directive like so:
'aliases' => [
...
'AuthService' => AuthService\Facades\AuthService::class,
],
This way, you may access the package functionality using AuthService
facade.
The last step to setup this package is to publish the configuration file. On your command prompt, run the following artisan command:
php artisan vendor:publish --provider="AuthService\AuthServiceProvider"
This command will copy a default package's configuration file in config/authservice.php
.
Once the package's configuration file is published, you may locate the file in config\authservice.php
. The default configuration file will look like this:
return [
'auth_event_listener_class' => AuthService\AuthEventListener::class,
'login_failed_message' => 'Credentials do not match.',
'after_login_success_path' => 'protected',
'after_logout_success_path' => 'login'
];
auth_event_listener_class
This configuration tells the service provider which authentication event listener class to use. By default it will use the provided AuthService\AuthEventListener
class. However you may also override it with your own event listener implementation as long as it confronts the AuthService\Contracts\AuthEventListenerInterface
contract.
login_failed_message
This is the error message that will be used when user's credentials is invalid. By default this error message will be flashed out to the session if login is failed.
after_login_success_path
This is the path where user will be redirected to if he/she successfully logged in.
after_logout_success_path
This is the path where user will be redirected to if he/she logged out from application.
To log the user in, simply call the login()
method:
AuthService::login(array $credentials, $remember = false);
This method will attempt to log the user in and will return an instance of Illuminate\Http\RedirectResponse
.
To log the user out, we may use the logout()
method:
AuthService::logout();
This method will also return an instance of Illuminate\Http\RedirectResponse
.
Here is some basic example to perform login and logout functionality, assuming that we are using the facade.
use AuthService;
// Log the user in.
$credentials = ['email' => 'john@example.com', 'password' => 'secret'];
AuthService::login($credentials);
// Log the user out.
AuthService::logout();
For a complete implementation, we will create a simple authentication controller that handles the login and logout request.
namespace App\Http\Controllers\Auth;
use AuthService;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
class AuthController extends Controller
{
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Show the login page.
*
* @return Illuminate\Http\Response
*/
public function getLogin()
{
return view('auth.login');
}
/**
* Handle login request.
*
* @param App\Http\Requests\Auth\LoginRequest $request
* @return Illuminate\Http\Response
*/
public function postLogin(LoginRequest $request)
{
$credentials = $request->only(['email', 'password']);
return AuthService::login($credentials, $request->has('remember'));
}
/**
* Log the user out.
*
* @return Illuminate\Http\Response
*/
public function getLogout()
{
return AuthService::logout();
}
}
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use AuthService\Contracts\AuthServiceInterface;
class AuthController extends Controller
{
protected $authService;
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct(AuthServiceInterface $authService)
{
$this->middleware('guest', ['except' => 'getLogout']);
$this->authService = $authService;
}
/**
* Show the login page.
*
* @return Illuminate\Http\Response
*/
public function getLogin()
{
return view('auth.login');
}
/**
* Handle login request.
*
* @param App\Http\Requests\Auth\LoginRequest $request
* @return Illuminate\Http\Response
*/
public function postLogin(LoginRequest $request)
{
$credentials = $request->only(['email', 'password']);
return $this->authService->login($credentials, $request->has('remember'));
}
/**
* Log the user out.
*
* @return Illuminate\Http\Response
*/
public function getLogout()
{
return $this->authService->logout();
}
}