master

laravel/framework

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

AuthenticationException.php

TLDR

The AuthenticationException.php file contains a class called AuthenticationException that extends the Exception class. It is part of the Illuminate\Auth namespace. This class is used to represent an authentication exception in the authentication process. It provides methods to get the guards that were checked and to get the redirect path for the unauthenticated user.

Methods

__construct($message = 'Unauthenticated.', array $guards = [], $redirectTo = null)

This method is the constructor of the AuthenticationException class. It creates a new authentication exception with the provided message, guards, and redirect path.

guards()

This method returns an array of the guards that were checked.

redirectTo(Request $request)

This method returns the path the user should be redirected to. If a redirect path is explicitly set, it will be returned. Otherwise, if a redirect callback is set, it will be called with the provided Request object to generate the redirect path.

redirectUsing(callable $redirectToCallback)

This static method is used to specify the callback that should be used to generate the redirect path. The provided callback will be called with the Request object to generate the redirect path.

<?php

namespace Illuminate\Auth;

use Exception;
use Illuminate\Http\Request;

class AuthenticationException extends Exception
{
    /**
     * All of the guards that were checked.
     *
     * @var array
     */
    protected $guards;

    /**
     * The path the user should be redirected to.
     *
     * @var string|null
     */
    protected $redirectTo;

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

    /**
     * Create a new authentication exception.
     *
     * @param  string  $message
     * @param  array  $guards
     * @param  string|null  $redirectTo
     * @return void
     */
    public function __construct($message = 'Unauthenticated.', array $guards = [], $redirectTo = null)
    {
        parent::__construct($message);

        $this->guards = $guards;
        $this->redirectTo = $redirectTo;
    }

    /**
     * Get the guards that were checked.
     *
     * @return array
     */
    public function guards()
    {
        return $this->guards;
    }

    /**
     * Get the path the user should be redirected to.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string|null
     */
    public function redirectTo(Request $request)
    {
        if ($this->redirectTo) {
            return $this->redirectTo;
        }

        if (static::$redirectToCallback) {
            return call_user_func(static::$redirectToCallback, $request);
        }
    }

    /**
     * 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;
    }
}