2017 © Pedro Peláez
 

library auth

Kolay Auth

image

kolayik/auth

Kolay Auth

  • Thursday, July 19, 2018
  • by kolay-hakan
  • Repository
  • 8 Watchers
  • 12 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 6 Versions
  • 20 % Grown

The README.md

Documentation

For version dev-master See the below for documentation., (*1)


Installation

To install this package you will need:, (*2)

  • Laravel 4 or 5
  • PHP 5.4 +

Install package via composer require, (*3)

composer require kolayik/auth:dev-master

or edit your composer.json, (*4)

"require": {

    "kolayik/auth": "dev-master"
}

Then run composer update in your terminal to pull it in., (*5)

Once this has finished, you will need to add the service provider to the providers array in your app.php config as follows:, (*6)

KolayIK\Auth\Providers\LaravelServiceProvider::class

Finally, you will want to publish the config and migration file using the following command:, (*7)

Laravel 5:, (*8)

$ php artisan vendor:publish --provider="KolayIK\Auth\Providers\LaravelServiceProvider"

Configuration

Open .env file and change according to your request., (*9)

Token time to live - KOLAY_AUTH_TTL, (*10)

Storage - KOLAY_AUTH_DRIVER, (*11)

KOLAY_AUTH_DRIVER:"database" or "cache"

KOLAY_AUTH_TTL:1440

Quick Start

How do generate a custom token ?, (*12)

use KolayIK\Auth\Facades\KolayAuth;

class AuthenticateController extends Controller
{
    public function authenticate(Request $request)
    {
        // your add custom login code
        $userId = "kolayik";

        return response()->json(KolayAuth::generate($userId));
    }
}

How do authenticate via token in custom middleware ?, (*13)

    namespace App\Http\Middleware;
    use KolayIK\Auth\Facades\KolayAuth;

    class CustomAuth
    {
        public function handle($request, Closure $next)
        {   
            $token = KolayAuth::authenticate();

            if ($token->isExpired()) {
                throw new \Exception('Session expired!');
            }

            return $next($request);
        }
    }

Authentication

To make authenticated requests via http using the built in methods, you will need to set an authorization header as follows:, (*14)

Authorization: Bearer {yourtoken}

Note to Apache users, (*15)

Apache seems to discard the Authorization header if it is not a base64 encoded user/pass combo. So to fix this you can add the following to your apache config, (*16)

RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

Alternatively you can include the token via a query string, (*17)

http://api.mysite.com/me?authorization_key={yourtoken}

To get the token from the request you can do:, (*18)

$token = KolayAuth::getToken();

Middleware, (*19)

You can use kolay.auth middleware:, (*20)

Route::group(['prefix' => '/api/v1', 'middleware' => 'kolay.auth'], function () {
    //your code
});

License

The MIT License (MIT), (*21)

The Versions