kuzdo/laravel-wargaming-auth

Laravel Wargaming Auth

v2.2.0 2021-02-23 13:22 UTC

This package is auto-updated.

Last update: 2024-09-23 21:25:43 UTC


README

License Latest Stable Version Total Downloads

此包是一个 Laravel 8 服务提供者,它为 Wargaming OpenID 提供支持,并且非常易于与任何需要 Wargaming 身份验证的项目集成。

安装

使用 composer 安装此包。

composer require kuzdo/laravel-wargaming-auth

Laravel >=5.5 使用包自动发现,因此不需要您手动添加 ServiceProvider。

使用发布命令将包配置复制到您本地的配置中

php artisan vendor:publish --provider="Kuzdo\Laravel\WargamingAuth\Providers\WargamingAuthServiceProvider"

使用示例

routes/web.php

Route::get('auth/wargaming/{wargamingAuthRegion?}', 'AuthController@redirectToWargaming')->name('auth.wargaming');
Route::get('auth/wargaming/callback', 'AuthController@handleWargamingCallback')->name('auth.wargaming.handle');

AuthController

namespace App\Http\Controllers;

use Kuzdo\Laravel\WargamingAuth\WargamingAuth;
use Illuminate\Http\RedirectResponse;

class AuthController extends Controller
{
    /**
     * @var WargamingAuth
     */
    protected $wargamingAuth;

    /**
     * AuthController constructor.
     *
     * @param WargamingAuth $wargamingAuth
     */
    public function __construct(WargamingAuth $wargamingAuth)
    {
        $this->wargamingAuth = $wargamingAuth;
    }

    /**
     * Redirect the user to the authentication page.
     *
     * @param string|null $region
     *
     * @return RedirectResponse
     */
    public function redirectToWargaming(string $region = null): RedirectResponse
    {
        if ($region) {
            $this->wargamingAuth->setRegion($region);
        }

        return new RedirectResponse($this->wargamingAuth->redirectUrl());
    }

    /**
     * Get user info and log in (hypothetically).
     *
     * @return RedirectResponse
     */
    public function handleWargamingCallback(): RedirectResponse
    {
        if ($this->wargamingAuth->verify()) {
            $user = $this->wargamingAuth->user();

            //

            return new RedirectResponse('/');
        }

        return $this->redirectToWargaming();
    }
}