<commit_before><?php

namespace Northstar\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Contracts\Auth\Factory as Auth;

class RedirectIfAuthenticated
{
    /**
     * The authentication factory.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
    protected $auth;

    /**
     * Create a new middleware instance.
     *
     * @param \Illuminate\Contracts\Auth\Factory $auth
     */
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = 'api')
    {
        if ($this->auth->guard($guard)->check()) {
            return $this->handleAnonymous($request);
        }

        return $next($request);
    }

    /**
     * Handle an anonymous request.
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function handleAnonymous(Request $request)
    {
        if ($request->wantsJson() || $request->ajax()) {
            throw new HttpException(401, 'You cannot do this with an active authentication token.');
        }

        return redirect('/');
    }
}<commit_msg>Redirect to login page if not authenticated.<commit_after><?php

namespace Northstar\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Illuminate\Contracts\Auth\Factory as Auth;

class RedirectIfAuthenticated
{
    /**
     * The authentication factory.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
    protected $auth;

    /**
     * Create a new middleware instance.
     *
     * @param \Illuminate\Contracts\Auth\Factory $auth
     */
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = 'api')
    {
        if ($this->auth->guard($guard)->check()) {
            return $this->handleAnonymous($request);
        }

        return $next($request);
    }

    /**
     * Handle an anonymous request.
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function handleAnonymous(Request $request)
    {
        if ($request->wantsJson() || $request->ajax()) {
            throw new HttpException(401, 'You cannot do this with an active authentication token.');
        }

        return redirect('login');
    }
}
{'fraction_non_alphanumeric': 0.124956369982548, 'fraction_numerical': 0.0020942408376963353, 'mean_word_length': 2.313294797687861, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3933', 'n_tokens_mistral': 826, 'n_tokens_neox': 829, 'n_words': 196}