Steam аутентификация для Laravel - PullRequest
0 голосов
/ 29 февраля 2020

Я использовал Laravel 5.1, сейчас я обновляюсь до последней версии. Мне нужно обновить пакет laravel-steam-auth, я ввел команду composer require invisnik/laravel-steam-auth, все вроде нормально, ошибок не возникало, папка в / vendor создана, все так, как и должно быть. Затем я начинаю изменять файлы в соответствии с официальной инструкцией: https://github.com/invisnik/laravel-steam-auth, здесь показан пример AuthController, но я использую свой старый AuthController. И когда я пытаюсь войти на сайт через Steam, ничего не происходит. Сайт перезагружается, и попросите меня войти через Steam снова.

Мой AuthController:

<?php

namespace App\Http\Controllers;

use App\User;
use Auth;
use Illuminate\Http\Request;
use Carbon\Carbon;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Invisnik\LaravelSteamAuth\SteamAuth;

class SteamController extends Controller
{
    const API_KEY = 'XXXXX'; // for check tradeban.

    private $steamAuth;

    public function __construct(SteamAuth $auth)
    {
        parent::__construct();
        $this->steamAuth = $auth;
    }

    public function login()
    {
        $refCode = \Cookie::get('ref');
        if($refCode != NULL) {
            $result = \DB::table('users')->where('ref_code', $refCode)->count();
            $partner = User::where('ref_code', $refCode)->first();
        }

        if ($this->steamAuth->validate()) {
            $steamID = $this->steamAuth->getSteamId();
            $user = User::where('steamid64', $steamID)->first();

            $tradeban_info = 'https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key='.self::API_KEY.'&steamids='.$steamID;
            $tradeban_info = file_get_contents($tradeban_info);
            $tradeban_info = json_decode($tradeban_info, true);
            $tradeban = $tradeban_info['players'][0]['EconomyBan'];

            if (!is_null($user)) {
                if($refCode != NULL && $user->activate_code == '' && $result > 0 && $user->id != $partner->id) {
                    $steamInfo = $this->steamAuth->getUserInfo();
                    \DB::table('users')
                        ->where('steamid64', $steamID)
                        ->update(['username' => $this->replaceLogin($steamInfo->getNick()),
                            'avatar' => $steamInfo->getProfilePictureFull(),
                            'tradeban' => $tradeban,
                            'activate_code' => $refCode,
                            'activate_date' =>  Carbon::now(),
                            'money' => $user->money + 2
                        ]);
                    $user->save();
                    $refscore = \DB::table('users')->where('id', $partner->id)->value('ref_score');

                    \DB::table('users')->where('id', $partner->id)->update([
                        'money' => $partner->money + 2,
                        'ref_score' => $refscore + 1
                    ]);
                } else {
                    $steamInfo = $this->steamAuth->getUserInfo();
                    \DB::table('users')
                        ->where('steamid64', $steamID)
                        ->update([
                            'username' => $this->replaceLogin($steamInfo->getNick()),
                            'avatar' => $steamInfo->getProfilePictureFull(),
                            'vievsteam' => $steamInfo->getSteamID64(),
                        ]);
                }
            } else {
                if(isset($refCode)) {

                    $steamInfo = $this->steamAuth->getUserInfo();
                    $user = User::create([
                        'username' => $this->replaceLogin($steamInfo->getNick()),
                        'avatar' => $steamInfo->getProfilePictureFull(),
                        'steamid' => $steamInfo->getSteamID(),
                        'steamid64' => $steamInfo->getSteamID64(),
                        'tradeban' => $tradeban,
                        'activate_code' => $refCode,
                        'activate_date' =>  Carbon::now(),
                        'money' => 2,
                        'vievsteam' => $partner->id,
                        //'ip' => $this->getIP(),
                    ]);
                    $refscore = \DB::table('users')->where('id', $partner->id)->value('ref_score');

                    \DB::table('users')->where('id', $partner->id)->update([
                        'money' => $partner->money + 2,
                        'ref_score' => $refscore + 1
                    ]);
                } else {
                    $steamInfo = $this->steamAuth->getUserInfo();
                    $user = User::create([
                        'username' => $this->replaceLogin($steamInfo->getNick()),
                        'avatar' => $steamInfo->getProfilePictureFull(),
                        'steamid' => $steamInfo->getSteamID(),
                        'steamid64' => $steamInfo->getSteamID64(),
                        'tradeban' => $tradeban,
                    ]);
                }
            }

            Auth::login($user, true);
            return redirect('/');
        } else {
            return $this->steamAuth->redirect();
        }
    }


    public function logout()
    {
        Auth::logout();
        return redirect('/');
    }


}

А вот пример AuthController на официальной странице github:

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
        ]);
    }

}

Где моя ошибка? Почему я не могу авторизоваться при входе через Steam и постоянно просит войти снова?

...