This package is a Laravel 5 service provider which provides support for Wargaming OpenID and is very easy to integrate with any project that requires Wargaming authentication.
Add this to your composer.json
file, in the require object:
"rguedes/laravel-wargaming-auth": "dev-master"
and this:
"repositories": [
{
"type":"vcs",
"url": "https://github.com/rguedes/laravel-wargaming-auth.git"
}
]
After that, run composer install
to install the package.
Add the service provider to app/config/app.php
, within the providers
array.
'providers' => [
// ...
Rguedes\LaravelWargamingAuth\WargamingServiceProvider::class,
]
Lastly, publish the config file.
php artisan vendor:publish
In config/wargaming-auth.php
return [
/*
* Redirect URL after login
*/
'redirect_url' => '/login',
/*
* API Key (https://developers.wargaming.net/applications)
*/
'api_key' => 'Your API Key',
/*
* Is using https?
*/
'https' => false
];
In routes/web.php
Route::get('login', 'AuthController@login')->name('login');
Note: if you want to keep using Laravel's default logout route, add the following as well:
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
In AuthController
namespace App\Http\Controllers;
use Rguedes\LaravelWargamingAuth\WargamingAuth;
use App\User;
use Auth;
class AuthController extends Controller
{
/**
* @var WargamingAuth
*/
private $wargaming;
public function __construct(WargamingAuth $wargaming)
{
$this->wargaming = $wargaming;
}
public function login()
{
if ($this->wargaming->validate()) {
$info = $this->wargaming->getUserInfo();
if (!is_null($info)) {
$user = User::where('wargamingid', $info->account_id)->first();
if (is_null($user)) {
$user = User::create([
'username' => $info->nickname,
'wargamingid' => $info->account_id
]);
}
Auth::login($user, true);
return redirect('/'); // redirect to site
}
}
return $this->wargaming->redirect(); // redirect to Wargaming login page
}
}