0

Atm, I use a steamauth API to grab a users steamid and pass it into user->steamid, but I want to restrict it to, if the field named steamid in users is not null(has already a steamid) they cant enter the route and will get a redirect back. I have tried for several hours now, but i cant seem to get it to working. This is my AuthController atm:

use Invisnik\LaravelSteamAuth\SteamAuth;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

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;
        $this->middleware('auth');
    }

    /**
     * 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->getSteamId();

            if (!is_null($info)) {
                Auth::user()->update(['steamid' => $info]);
                return redirect($this->redirectURL); // redirect to site
            }
        }
        return $this->redirectToSteam();
    }
Amit Kumar PRO
  • 1,222
  • 2
  • 15
  • 27
Kh4zy
  • 105
  • 1
  • 3
  • 11
  • Have you tried using a middleware that checks if the field is empty or not? e.g. `if (is_null($user->steamid)) { return redirect('/somewhere'); }` – Raed Yakoubi Feb 01 '20 at 05:26

1 Answers1

0

Added this custom middleware and it works:

public function handle($request, \Closure $next)
    {
        /*$user = User::where('steamid', $request)->first();

        if (!is_null($user)) {
            return redirect('/profile');
        }*/

        if ($request->user()->steamid !== null){
            return redirect('/profile')->with('denied', 'Du kan kun tilføje én steamprofil');
        }
        return $next($request);
    }
Kh4zy
  • 105
  • 1
  • 3
  • 11