PHP code example of risan / laravel-auth-service

1. Go to this page and download the library: Download risan/laravel-auth-service library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

risan / laravel-auth-service example snippets


'providers' => [
    ...
    AuthService\AuthServiceProvider::class,

],

'aliases' => [
    ...
    'AuthService' => AuthService\Facades\AuthService::class,

],

php artisan vendor:publish --provider="AuthService\AuthServiceProvider"

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'
];

AuthService::login(array $credentials, $remember = false);

AuthService::logout();

use AuthService;

// Log the user in.
$credentials = ['email' => '[email protected]', 'password' => 'secret'];
AuthService::login($credentials);

// Log the user out.
AuthService::logout();

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();
    }
}