Laravel 6 - Как ограничить маршрут по значению поля пользователя? - PullRequest
0 голосов
/ 31 января 2020

Атм, я использую API Steamauth, чтобы захватить Steam пользователей и передать его в user-> steamid, но я хочу ограничить его, если поле с именем steamid в пользователях не нулевое (уже имеет steamid), они не могут введите маршрут и получите перенаправление назад. Я пытался в течение нескольких часов, но я не могу заставить его работать. Это мой AuthController банкомат:

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

1 Ответ

0 голосов
/ 01 февраля 2020

Добавлено это пользовательское промежуточное ПО, и оно работает:

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