src/Auth/functions.php
source
| 1 | <?php |
| 2 | |
| 3 | namespace mini; |
| 4 | |
| 5 | use mini\Auth\Auth; |
| 6 | use mini\Auth\AuthInterface; |
| 7 | |
| 8 | /** |
| 9 | * Auth Feature - Global Helper Functions |
| 10 | * |
| 11 | * These functions provide the public API for the mini\Auth feature. |
| 12 | */ |
| 13 | |
| 14 | // Register Auth facade service |
| 15 | Mini::$mini->addService(Auth::class, Lifetime::Singleton, fn() => new Auth()); |
| 16 | |
| 17 | // Register AuthInterface - apps must provide implementation via config |
| 18 | Mini::$mini->addService(AuthInterface::class, Lifetime::Singleton, fn() => Mini::$mini->loadServiceConfig(AuthInterface::class)); |
| 19 | |
| 20 | /** |
| 21 | * Get the Auth facade instance |
| 22 | * |
| 23 | * Returns the Auth facade with convenience methods. |
| 24 | * The facade delegates to the configured AuthInterface implementation. |
| 25 | * |
| 26 | * Usage: |
| 27 | * auth()->requireLogin(); |
| 28 | * auth()->requireRole('admin'); |
| 29 | * if (auth()->isAuthenticated()) { ... } |
| 30 | * $userId = auth()->getUserId(); |
| 31 | * |
| 32 | * @return Auth Auth facade instance |
| 33 | */ |
| 34 | function auth(): Auth |
| 35 | { |
| 36 | return Mini::$mini->get(Auth::class); |
| 37 | } |
| 38 | |
| 39 |