alexevladgabriel / laravel-steam-auth
Laravel Steam Auth
v4.2.0
2020-03-06 11:14 UTC
Requires
- php: ^7.2
- guzzlehttp/guzzle: ^6.3
- illuminate/http: ^5.8|^6.0|^7.0
- illuminate/routing: ^5.8|^6.0|^7.0
- illuminate/support: ^5.8|^6.0|^7.0
Requires (Dev)
- phpunit/phpunit: ^7.0
This package is auto-updated.
Last update: 2024-09-29 05:42:55 UTC
README
此包是一个 Laravel 5 服务提供者,它提供了 Steam OpenID 的支持,并且非常容易与需要 Steam 认证的任何项目集成。
要求
- PHP 7.2+
- Laravel 5.8+
安装
通过 Composer
composer require invisnik/laravel-steam-auth
Steam API 密钥
将您的 Steam API 密钥添加到您的 .env
文件中。您可以在 这里 获取您的 API 密钥。
STEAM_API_KEY=SomeKindOfAPIKey
配置文件
发布配置文件。
php artisan vendor:publish --provider="Invisnik\LaravelSteamAuth\SteamServiceProvider"
用法示例
在 config/steam-auth.php
return [ /* * Redirect URL after login */ 'redirect_url' => '/auth/steam/handle', /* * Realm override. Bypass domain ban by Valve. * Use alternative domain with redirection to main for authentication (banned by valve). */ // 'realm' => 'redirected.com', /* * API Key (set in .env file) [http://steamcommunity.com/dev/apikey] */ 'api_key' => env('STEAM_API_KEY', ''), /* * Is using https? */ 'https' => false, ];
在 routes/web.php
Route::get('auth/steam', 'AuthController@redirectToSteam')->name('auth.steam'); Route::get('auth/steam/handle', 'AuthController@handle')->name('auth.steam.handle');
注意:如果您想继续使用 Laravel 的默认登出路由,还需要添加以下内容
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
在 AuthController
namespace App\Http\Controllers; use Invisnik\LaravelSteamAuth\SteamAuth; use App\User; use Auth; class AuthController extends Controller { /** * The SteamAuth instance. * * @var SteamAuth */ protected $steam; /** * The redirect URL. * * @var string */ protected $redirectURL = '/'; /** * AuthController constructor. * * @param SteamAuth $steam */ public function __construct(SteamAuth $steam) { $this->steam = $steam; } /** * Redirect the user to the authentication page * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function redirectToSteam() { return $this->steam->redirect(); } /** * Get user info and log in * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function handle() { if ($this->steam->validate()) { $info = $this->steam->getUserInfo(); if (!is_null($info)) { $user = $this->findOrNewUser($info); Auth::login($user, true); return redirect($this->redirectURL); // redirect to site } } return $this->redirectToSteam(); } /** * Getting user by info or created if not exists * * @param $info * @return User */ protected function findOrNewUser($info) { $user = User::where('steamid', $info->steamID64)->first(); if (!is_null($user)) { return $user; } return User::create([ 'username' => $info->personaname, 'avatar' => $info->avatarfull, 'steamid' => $info->steamID64 ]); } }
如果您希望使用与配置中指定的不同的登录重定向 URL
// Inside your controller login method $this->steam->setRedirectUrl(route('login.route')); ... return $this->steam->redirect();
如果您需要另一个 steamID,可以使用另一个包将给定的 steamID64 转换为其他类型,例如 xPaw/SteamID。