ollyxar/laravel-auth

Laravel auth helper

1.1.2 2018-08-11 17:10 UTC

This package is not auto-updated.

Last update: 2024-04-25 17:07:47 UTC


README

Version Downloads License

Get user's id from session identified by cookie

Why auth helper?

Auth helper designed for external PHP application authentication. It helps developers to get user_id from their session without any auth-token or any authorize process.

How it's work?

Suppose you have some PHP server application that need to know who is making request. It can be some artisan command requiring authorization or WebSocket server in another port that is listening connections.

In the browser user go to some address (obviously it must have the same domain). Thus browser sends current headers to your application. And then you can use Cookie from the headers to define client.

License

Example

Here is a simple example how to determine user_id

// if the sessions are in files by default
use Ollyxar\LaravelAuth\FileAuth;

// or if the sessions in the Redis
// use Ollyxar\LaravelAuth\RedisAuth;

// The same code for Redis except you have to call RedisAuth instead
if ($userId = FileAuth::getUserIdByHeaders($headers)) {
    // we got it!
}

You have to provide correct $headers array that must content Cookie item. Otherwise $user_id will return false

The headers should be like:

$headers = [
    'Cookie' => 'someotherkey=someothervalue;laravel_session=somerandomstring'
];

Please notice that auth helper use the same native methods / functions to find session by cookie.