kuzdo/laravel-wargaming-auth

Laravel Wargaming Auth

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

This package is auto-updated.

Last update: 2024-10-23 21:37:49 UTC


README

License Latest Stable Version Total Downloads

This package is a Laravel 8 service provider which provides support for Wargaming OpenID and is very easy to integrate with any project that requires Wargaming authentication.

Installation

Require this package with composer.

composer require kuzdo/laravel-wargaming-auth

Laravel >=5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

Copy the package config to your local config with the publish command:

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

Usage example

In routes/web.php:

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

In 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();
    }
}