ollyxar/laravel-auth

Laravel 身份验证助手

1.1.2 2018-08-11 17:10 UTC

This package is not auto-updated.

Last update: 2024-09-26 18:58:47 UTC


README

Version Downloads License

从通过cookie标识的session中获取用户id

为什么需要身份验证助手?

身份验证助手是为外部PHP应用程序的认证而设计的。它帮助开发者从会话中获取user_id,无需任何auth-token或授权过程。

它是如何工作的?

假设你有一些PHP服务器应用程序需要知道谁在发起请求。这可能是一些需要授权的 artisan 命令,或者另一个端口的 WebSocket 服务器,它在监听连接。

在浏览器中,用户访问某个地址(显然它必须在同一域名下)。因此,浏览器将当前头部发送到你的应用程序。然后你可以使用头部的Cookie来定义客户端。

License

示例

以下是一个确定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!
}

你必须提供正确的$headers数组,该数组必须包含Cookie项。否则,$user_id将返回false

头部应该像这样

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

请注意,身份验证助手使用相同的原生方法/函数通过cookie查找会话。