diepfeiffe/laravel-steam-auth

Laravel 的 Steam 身份验证

1.0.0 2023-03-05 18:15 UTC

This package is auto-updated.

Last update: 2024-09-05 21:47:03 UTC


README

Latest Stable Version Total Downloads License

此软件包允许您在 Laravel 项目中实现 Steam 身份验证。

要求

  • Laravel 7+

安装

安装软件包

composer require ilzrv/laravel-steam-auth

发布配置文件

php artisan vendor:publish --provider="Ilzrv\LaravelSteamAuth\ServiceProvider"

设置 Steam API 密钥

将您的 Steam API 密钥添加到 .env 文件中。您可以在 此处 找到它。

如果您想使用多个 API 密钥,请用逗号分隔它们

STEAM_AUTH_API_KEYS=YourSteamApiKey1,YourSteamApiKey2

提示

PendingRequest 设置

您可以使用任何 Laravel PendingRequest 设置。例如代理和重试

public function __construct(Request $request)
{
    $pendingRequest = \Illuminate\Support\Facades\Http::withOptions([
        'proxy' => 'http://username:password@ip',
        'connect_timeout' => 10,
    ])->retry(3);

    $this->steamAuth = new SteamAuth($request, $pendingRequest);
}

代理域名

如果您想设置代理域名,更新 redirect_url 中的 steam-auth.php 为您的绝对地址,例如 https://auth.test/login。您可以为本地环境和生产环境使用不同的域名,如下所示

<?php

return [
    'redirect_url' => env('APP_ENV', 'production') == 'production'
        ? 'https://auth.test/login'
        : null,
];

在 NGINX 的代理域名设置中,您可以指定以下内容

server {
    listen 443 ssl http2;
    server_name auth.test;
    return 301 https://general.test$uri$is_args$args;
}

示例

routes/web.php

Route::get('login', [\App\Http\Controllers\Auth\SteamAuthController::class, 'login']);

创建控制器 SteamAuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Support\Facades\Auth;
use Ilzrv\LaravelSteamAuth\SteamAuth;
use Ilzrv\LaravelSteamAuth\SteamData;

class SteamAuthController extends Controller
{
    /**
     * The SteamAuth instance.
     *
     * @var SteamAuth
     */
    protected $steamAuth;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * SteamAuthController constructor.
     *
     * @param SteamAuth $steamAuth
     */
    public function __construct(SteamAuth $steamAuth)
    {
        $this->steamAuth = $steamAuth;
    }

    /**
     * Get user data and login
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function login()
    {
        if (!$this->steamAuth->validate()) {
            return $this->steamAuth->redirect();
        }

        $data = $this->steamAuth->getUserData();

        if (is_null($data)) {
            return $this->steamAuth->redirect();
        }

        Auth::login(
            $this->firstOrCreate($data),
            true
        );

        return redirect($this->redirectTo);
    }

    /**
     * Get the first user by SteamID or create new
     *
     * @param SteamData $data
     * @return User|\Illuminate\Database\Eloquent\Model
     */
    protected function firstOrCreate(SteamData $data)
    {
        return User::firstOrCreate([
            'steam_id' => $data->getSteamId(),
        ], [
            'name' => $data->getPersonaName(),
            'avatar' => $data->getAvatarFull(),
            'player_level' => $data->getPlayerLevel(),
            // ...and other what you need
        ]);
    }
}