master

laravel/framework

Last updated at: 29/12/2023 09:20

RedirectIfAuthenticated.php

TLDR

This file defines a middleware class called RedirectIfAuthenticated that is used to redirect authenticated users to a specific path. The class contains a handle method to handle incoming requests and determine whether the user is authenticated. It also has a redirectTo method to determine the path the user should be redirected to, and a defaultRedirectUri method to determine the default URI if no specific path is provided.

Methods

handle

This method is used to handle an incoming request and check if the user is authenticated. If the user is authenticated, it redirects them to the specified path. Otherwise, it passes the request to the next middleware.

redirectTo

This method determines the path the user should be redirected to when they are authenticated. It first checks if a custom callback function is provided to generate the redirect path. If not, it falls back to the defaultRedirectUri method.

defaultRedirectUri

This method determines the default URI the user should be redirected to when they are authenticated. It first checks if there is a route defined for 'dashboard' or 'home', and if so, it returns the route URI. If no route is found, it checks for the existence of a 'GET' route for 'dashboard' or 'home', and returns '/dashboard' or '/home' respectively. If no suitable route is found, it returns '/'.

Classes

There are no classes defined in this file.

<?php

namespace Illuminate\Auth\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Symfony\Component\HttpFoundation\Response;

class RedirectIfAuthenticated
{
    /**
     * The callback that should be used to generate the authentication redirect path.
     *
     * @var callable|null
     */
    protected static $redirectToCallback;

    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next, string ...$guards): Response
    {
        $guards = empty($guards) ? [null] : $guards;

        foreach ($guards as $guard) {
            if (Auth::guard($guard)->check()) {
                return redirect($this->redirectTo($request));
            }
        }

        return $next($request);
    }

    /**
     * Get the path the user should be redirected to when they are authenticated.
     */
    protected function redirectTo(Request $request): ?string
    {
        return static::$redirectToCallback
            ? call_user_func(static::$redirectToCallback, $request)
            : $this->defaultRedirectUri();
    }

    /**
     * Get the default URI the user should be redirected to when they are authenticated.
     */
    protected function defaultRedirectUri(): string
    {
        foreach (['dashboard', 'home'] as $uri) {
            if (Route::has($uri)) {
                return route($uri);
            }
        }

        $routes = Route::getRoutes()->get('GET');

        foreach (['dashboard', 'home'] as $uri) {
            if (isset($routes[$uri])) {
                return '/'.$uri;
            }
        }

        return '/';
    }

    /**
     * Specify the callback that should be used to generate the redirect path.
     *
     * @param  callable  $redirectToCallback
     * @return void
     */
    public static function redirectUsing(callable $redirectToCallback)
    {
        static::$redirectToCallback = $redirectToCallback;
    }
}