Neuron-PHP

Unauthorized extends Exception
in package

Exception thrown when authentication is required but not provided or invalid.

This exception should be thrown when a user needs to authenticate to access a resource. It corresponds to HTTP 401 Unauthorized status code.

Tags
example
// Throw when no authentication is provided
if (!$request->hasAuthToken()) {
    throw new Unauthorized('Authentication required');
}

// Throw with realm for HTTP Basic Auth
if (!$this->isValidCredentials($credentials)) {
    throw new Unauthorized('Invalid credentials', 'Admin Area');
}

Table of Contents

Properties

$realm  : string|null

Methods

__construct()  : mixed
getRealm()  : string|null
Get the authentication realm if specified

Properties

Methods

__construct()

public __construct([string $message = 'Authentication required' ][, string|null $realm = null ][, int $code = 401 ][, Throwable|null $previous = null ]) : mixed
Parameters
$message : string = 'Authentication required'

The error message

$realm : string|null = null

Optional authentication realm (e.g., for WWW-Authenticate header)

$code : int = 401

The exception code (defaults to 401)

$previous : Throwable|null = null

Previous exception for chaining

getRealm()

Get the authentication realm if specified

public getRealm() : string|null
Return values
string|null

        
On this page

Search results