Skip to content

Conversation

Copy link

Copilot AI commented Jan 24, 2026

The showLoginForm() method unconditionally saved the previous URL to session, causing redirect loops when users landed on /login from auth pages (login, register, password reset, GitHub OAuth).

Changes

  • Added auth route filtering before session storage in showLoginForm()
  • Exact URL comparison using in_array() to prevent false positives
  • Excluded routes: login, register, password.request, auth/github, auth/github/callback

Before:

public function showLoginForm()
{
    session(['redir' => url()->previous()]);
    return view('auth.login');
}

After:

public function showLoginForm()
{
    $previous = url()->previous();
    $authRoutes = [
        route('login'),
        route('register'),
        route('password.request'),
        url('auth/github'),
        url('auth/github/callback'),
    ];

    $isAuthRoute = in_array($previous, $authRoutes, true);

    if (!$isAuthRoute) {
        session(['redir' => $previous]);
    }

    return view('auth.login');
}
Original prompt

Problem

In app/Http/Controllers/Auth/LoginController.php around line 55, the code currently saves the previous URL to the session unconditionally:

session(['redir' => url()->previous()]);

This causes an issue when the previous page is the login page itself, potentially creating redirect loops.

Solution

Modify the code to check if the previous URL is the login page (or other authentication pages) before saving it to the session.

Implement one of these approaches:

Option 1: Simple check

$previous = url()->previous();
$loginUrl = route('login');

if ($previous !== $loginUrl && !str_contains($previous, '/login')) {
    session(['redir' => $previous]);
}

Option 2: Check against multiple auth routes (recommended)

$previous = url()->previous();
$authRoutes = [
    route('login'),
    // add route('register') if it exists
    // add other auth routes if needed
];

$isAuthRoute = collect($authRoutes)->contains(fn($route) => str_contains($previous, $route));

if (!$isAuthRoute) {
    session(['redir' => $previous]);
}

File to modify

  • app/Http/Controllers/Auth/LoginController.php (commit: 8113d94)

This will prevent circular redirects and improve the user authentication flow.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 24, 2026 20:27
Co-authored-by: vitoo <995694+vitoo@users.noreply.github.com>
…ives

Co-authored-by: vitoo <995694+vitoo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix redirect loop when saving previous URL in session Prevent redirect loops by filtering auth routes in LoginController Jan 24, 2026
Copilot AI requested a review from vitoo January 24, 2026 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants