PHPackages                             joke2k/tinker-auth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. joke2k/tinker-auth

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

joke2k/tinker-auth
==================

Authenticated sessions for Laravel Tinker with strict/optional modes and command-level auth trait.

01PHPCI passing

Since Mar 4Pushed 2mo agoCompare

[ Source](https://github.com/joke2k/tinker-auth)[ Packagist](https://packagist.org/packages/joke2k/tinker-auth)[ RSS](/packages/joke2k-tinker-auth/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Tinker Auth
===========

[](#tinker-auth)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e5b75a9dba7a6badd1a40f779d8aa1f0683202fe60b10a2abc1bfdcc7edbea2d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f6b65326b2f74696e6b65722d617574682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/joke2k/tinker-auth)[![Total Downloads](https://camo.githubusercontent.com/7eed6397d818b44bdc8f1c3f220ce5dc47625d6b45342dfa58855c425b935846/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f6b65326b2f74696e6b65722d617574682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/joke2k/tinker-auth)[![License](https://camo.githubusercontent.com/5088b6c574f77723cba3fbb594ee0b476c55583304a0ac7ef57f4deb5754c9a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a6f6b65326b2f74696e6b65722d617574682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/joke2k/tinker-auth)

Tinker Auth is a Laravel package that enforces or enables user authentication for `php artisan tinker` sessions and provides a reusable command trait for command-level user context.

Features
--------

[](#features)

- Tinker session auth modes:
    - `strict`: authentication is required.
    - `optional`: authentication is available but can be skipped.
    - `disabled`: no authentication is performed.
- Tinker command supports `--user|-u` to prefill the login username.
- Interactive auth prompts use `laravel/prompts` when available for improved terminal UX.
- Per-environment behavior through `.env` values.
- Authenticated Tinker session sets the active Laravel user (`Auth::user()`).
- Reusable command trait that adds:
    - `--user|-u`: prefill login username and require password.
    - Per-command auth mode (`strict|optional`) via class attribute.

Requirements
------------

[](#requirements)

- PHP 8.2+
- Laravel 11 or 12
- `illuminate/support`
- `laravel/tinker`
- `laravel/prompts` (optional)

Installation
------------

[](#installation)

1. Install the package:

```
composer require joke2k/tinker-auth
```

2. Publish package configuration:

```
php artisan tinker-auth:install
```

Manual alternative:

```
php artisan vendor:publish --provider="Joke2k\TinkerAuth\TinkerAuthServiceProvider" --tag="tinker-auth-config"
```

3. Set your `.env` values (example):

```
TINKER_AUTH_MODE=optional
TINKER_AUTH_USERNAME_COLUMN=email
TINKER_AUTH_GUARD=web
TINKER_AUTH_MAX_ATTEMPTS=3
```

4. Run Tinker with authentication support:

```
# normal tinker flow (uses TINKER_AUTH_MODE)
php artisan tinker

# prefill login with --user / -u and prompt for password
php artisan tinker --user=admin@example.com
php artisan tinker -u admin@example.com
```

Optional (recommended) for better interactive prompts:

```
composer require laravel/prompts
```

Configuration
-------------

[](#configuration)

Published config: `config/tinker-auth.php`

```
return [
    'mode' => env('TINKER_AUTH_MODE', 'optional'),
    'username_column' => env('TINKER_AUTH_USERNAME_COLUMN', 'email'),
    'guard' => env('TINKER_AUTH_GUARD'),
    'max_attempts' => (int) env('TINKER_AUTH_MAX_ATTEMPTS', 3),
    'prompt' => [
        'login_label' => 'Login',
        'password_label' => 'Password',
        'strict_message' => 'Authentication is required to start Tinker in strict mode.',
        'optional_message' => 'Press enter to continue without authentication.',
        'autocomplete_users' => (bool) env('TINKER_AUTH_AUTOCOMPLETE_USERS', false),
        'autocomplete_limit' => (int) env('TINKER_AUTH_AUTOCOMPLETE_LIMIT', 5),
    ],
    'command_trait' => [
        'default_mode' => env('TINKER_AUTH_COMMAND_DEFAULT_MODE', 'strict'),
    ],
];
```

Example per-environment overrides:

```
# .env.local
TINKER_AUTH_MODE=optional
TINKER_AUTH_AUTOCOMPLETE_USERS=true

# .env.production
TINKER_AUTH_MODE=strict
TINKER_AUTH_AUTOCOMPLETE_USERS=false
```

Tinker Behavior
---------------

[](#tinker-behavior)

- `TINKER_AUTH_MODE=strict`
    - Interactive: prompts for login + password.
    - Non-interactive: exits with failure.
- `TINKER_AUTH_MODE=optional`
    - Interactive: allows authentication or skip.
    - Non-interactive: continues without auth.
- `TINKER_AUTH_MODE=disabled`
    - Always continues without auth.
- `--user|-u ` (on `php artisan tinker`)
    - Uses the identifier as login username.
    - Always prompts for password.
    - Behaves as strict authentication for that run.
- `$_u` context variable
    - Contains the authenticated user for the current Tinker session.
    - Is `null` when no user is authenticated.

Command Trait Usage
-------------------

[](#command-trait-usage)

Use the trait in any custom Artisan command:

```
