A
A
Alex Wells2015-08-21 03:25:27
Open ID
Alex Wells, 2015-08-21 03:25:27

How to work with Laravel Steam Auth?

Hello. I'm trying to authorize users through steam. Registration should not be, and authorization is exclusively through Steam. Now I did everything as in the example-guide, well, approximately. Only now I, for the life of me, do not understand how it should work. I created a get route on [email protected], if you go to this route, authorization on the Steam website will come out. When you confirm everything, it returns back to the site with the following contents of the address bar:

?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=id_res&openid.op_endpoint=https%3A%2F%2Fsteamcommunity.com%2Fopenid%2Flogin&openid.claimed_id=http%3A%2F%2Fsteamcommunity.com%2Fopenid%2Fid%2F76561198126709110&openid.identity=http%3A%2F%2Fsteamcommunity.com%2Fopenid%2Fid%2F

But the user is still guest. What to do? Here is the SteamController itself:
<?php
  
namespace App\Http\Controllers;

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

class SteamController extends Controller {

    /**
     * @var SteamAuth
     */
    private $steam;

    public function __construct(SteamAuth $steam)
    {
        $this->steam = $steam;
    }

    public function getLogin()
    {
        if($this->steam->validate()){
            $info = $this->steam->getUserInfo();
      echo $info->getNick();
            if(!is_null($info)) {
        try {
          $user = User::where('steamid', $info->getSteamID())->first();
          if($user->name != $info->getNick() || $user->profileURL != $info->getProfileURL()) {
            $user->name = $info->getNick();
            $user->profileURL = $info->getProfileURL();
            $user->save();
          }
          
          Auth::login($user);
          return $user;
          
        } catch(Exception $ex) {
          $user = User::create([
            'name' => $info->getNick(),
            'steamid' => $info->getSteamID(),
            'profileURL' => $info->getProfileURL(),
          ]);
          
          Auth::login($user);
          return $user;
        }
            }
        } else {
            return  $this->steam->redirect(); //redirect to steam login page
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2015-08-21
@Alex_Wells

And so, guys, I figured it out. Right now we will fix the redneck code, but it works! It turned out that after authorization, Steam must get to the same controller and the same method, and then the validate () method will return true, and everything will work!
Here is the code:

<?php
  
namespace App\Http\Controllers;

use Illuminate\Auth\GenericUser;
use App\User;
use Debugbar;
use Auth;
use Redirect;
use App\Http\Controllers\Controller;
use Invisnik\LaravelSteamAuth\SteamAuth;

class SteamController extends Controller {

    /**
     * @var SteamAuth
     */
    private $steam;

    public function __construct(SteamAuth $steam)
    {
        $this->steam = $steam;
    }

    public function getLogin()
    {
        if($this->steam->validate()) {
      Debugbar::warning('OK');
            $info = $this->steam->getUserInfo();
            if(!is_null($info)) {
        try {
          $user = User::where('steamid', $info->getSteamID())->first();
          if(is_null($user)) {
            $user = User::create([
              'name' => $info->getNick(),
              'steamid' => $info->getSteamID(),
              'profileURL' => $info->getProfileURL(),
            ]);
          }
          if($user->name != $info->getNick() || $user->profileURL != $info->getProfileURL()) {
            $user->name = $info->getNick();
            $user->profileURL = $info->getProfileURL();
            $user->save();
          }
          
          Auth::login($user);
          return Redirect::to('/');
          
        } catch(Illuminate\Database\Eloquent\ModelNotFoundException $ex) {
        }
            }
        } else {
            return  $this->steam->redirect(); //redirect to steam login page
        }
    }
  
  public function getLogout()
  {
    Auth::logout();
    return Redirect::to('/');
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question