steamアカウントでよくあるTwitterなどのようにソーシャルログイン実装をしたい。
cakephp3
Steam Web API
OpenID(LightOpenID)
Steam Web API
https://steamcommunity.com/dev
このURLにOpenIDの紹介があるので、飛んでみると色々あるようだが
Googleで検索した結果、LightOpenIDがよさげなのでこれを使う。
cpmposerで導入するので、インストールしてない場合は、composer インストールしてください。紹介は省く。
"repositories":[ { "type": "vcs", "url": "https://github.com/iignatov/LightOpenID" } ], "require": { "php": ">=5.6", "iignatov/lightopenid": "*" },
composer update
public function index() { $this->autoRender = FALSE; $openid = new LightOpenID("https://". $_SERVER["HTTP_HOST"]. "/auth/steam"); $openid->identity = "http://steamcommunity.com/openid"; $openid->returnUrl = "https://". $_SERVER["HTTP_HOST"]. "/auth/steam/callback"; if($openid->mode === null) return $this->redirect($openid->authUrl()); }
$this->autoRender = FALSE;
$openid = new LightOpenID("https://". $_SERVER["HTTP_HOST"]. "/auth/steam");
$openid->identity = "http://steamcommunity.com/openid";
$openid->returnUrl = "https://". $_SERVER["HTTP_HOST"]. "/auth/steam/callback";
if($openid->mode === null) return $this->redirect($openid->authUrl());
public function callback() { $this->autoRender = FALSE; $openid = new LightOpenID("https://". $_SERVER["HTTP_HOST"]. "/auth/steam"); $openid->identity = "http://steamcommunity.com/openid"; $openid->returnUrl = "https://". $_SERVER["HTTP_HOST"]. "/auth/steam/callback"; $steam_id = str_replace('https://steamcommunity.com/openid/id/', '', $this->request->getQuery('openid_identity')); $user = $this->TUsers->find()->where(['steam_id' => $steam_id]); if(empty($this->Auth->user()) && $user->isEmpty()) return $this->registry($steam_id); if(!empty($this->Auth->user()) && $user->isEmpty()) return $this->cooperation($steam_id); if(empty($this->Auth->user()) && !$user->isEmpty()) return $this->login($user); if(!empty($this->Auth->user()) && !$user->isEmpty()) return $this->release(); $this->render('/Auth/complete'); }
if(empty($this->Auth->user()) && $user->isEmpty()) return $this->registry($steam_id);
protected function registry($steam_id) { $steam_user_json = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=[steam_web_api_key]&steamids={$steam_id}"); $steam_user = json_decode($steam_user_json); $steam_user = $steam_user->response->players[0]; $users_entity = $this->TUsers->newEntity([ "user_id" => uniqid(), "name" => $steam_user->personaname, "image" => $steam_user->avatar, "steam_id" => $steam_id, ]); if($this->TUsers->save($users_entity)) { $this->Auth->setUser($users_entity); return $this->redirect("/{$users_entity->user_id}"); } else { echo '失敗'; } }
$steam_user_json = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=[steam_web_api_key]&steamids={$steam_id}");
そのあとはcakephp3のデータ追加なので割愛。
違う部分などは好きなように変えてください。
のこりの連携と解除とログインに関しては特に難しいこともないので割愛。
必要な場合言ってもらえれば残りも載せるかも。
それじゃまた
コメントがありません。