• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / auth / 21031260116

15 Jan 2026 11:02AM UTC coverage: 66.786% (+0.2%) from 66.555%
21031260116

push

github

daycry
Refactor Auth config for improved organization

Reorganized the Auth configuration file by grouping related settings into logical sections such as logging, rate limiting, authentication, user/registration, password/security, OAuth, views/URLs, and discovery/cron. This improves readability and maintainability without changing any functional behavior.

2246 of 3363 relevant lines covered (66.79%)

33.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.24
/src/Auth.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth;
15

16
use CodeIgniter\Router\RouteCollection;
17
use Daycry\Auth\Authentication\Authentication;
18
use Daycry\Auth\Config\Auth as AuthConfig;
19
use Daycry\Auth\Entities\User;
20
use Daycry\Auth\Exceptions\AuthenticationException;
21
use Daycry\Auth\Interfaces\AuthenticatorInterface;
22
use Daycry\Auth\Interfaces\UserProviderInterface;
23

24
/**
25
 * Facade for Authentication
26
 *
27
 * @method Result    attempt(array $credentials)
28
 * @method Result    check(array $credentials)
29
 * @method bool      checkAction(string $token, string $type) [Session]
30
 * @method void      forget(?User $user = null)               [Session]
31
 * @method User|null getUser()
32
 * @method bool      loggedIn()
33
 * @method bool      login(User $user)
34
 * @method void      loginById($userId)
35
 * @method bool      logout()
36
 * @method void      recordActiveDate()
37
 * @method $this     remember(bool $shouldRemember = true)    [Session]
38
 */
39
class Auth
40
{
41
    /**
42
     * The current version of CodeIgniter Shield
43
     */
44
    public const SHIELD_VERSION = '1.0.5';
45

46
    protected ?UserProviderInterface $userProvider = null;
47
    protected ?Authentication $authenticate        = null;
48

49
    /**
50
     * The Authenticator alias to use for this request.
51
     */
52
    protected ?string $alias = null;
53

54
    public function __construct(protected AuthConfig $config)
55
    {
56
    }
81✔
57

58
    protected function ensureAuthentication(): void
59
    {
60
        if ($this->authenticate !== null) {
58✔
61
            return;
30✔
62
        }
63

64
        $authenticate = new Authentication($this->config);
58✔
65
        $authenticate->setProvider($this->getProvider());
58✔
66

67
        $this->authenticate = $authenticate;
58✔
68
    }
69

70
    /**
71
     * Sets the Authenticator alias that should be used for this request.
72
     *
73
     * @return $this
74
     */
75
    public function setAuthenticator(?string $alias = null): self
76
    {
77
        if ($alias !== null) {
65✔
78
            $this->alias = $alias;
20✔
79
        }
80

81
        return $this;
65✔
82
    }
83

84
    /**
85
     * Returns the current authentication class.
86
     */
87
    public function getAuthenticator(): AuthenticatorInterface
88
    {
89
        $this->ensureAuthentication();
48✔
90

91
        return $this->authenticate
48✔
92
            ->factory($this->alias);
48✔
93
    }
94

95
    /**
96
     * Returns the current user, if logged in.
97
     */
98
    public function user(): ?User
99
    {
100
        return $this->getAuthenticator()->loggedIn()
36✔
101
            ? $this->getAuthenticator()->getUser()
14✔
102
            : null;
36✔
103
    }
104

105
    /**
106
     * Returns the current user's id, if logged in.
107
     *
108
     * @return int|string|null
109
     */
110
    public function id()
111
    {
112
        $user = $this->user();
22✔
113

114
        return ($user !== null) ? $user->id : null;
22✔
115
    }
116

117
    public function authenticate(array $credentials): Result
118
    {
119
        $this->ensureAuthentication();
×
120

121
        return $this->authenticate
×
122
            ->factory($this->alias)
×
123
            ->attempt($credentials);
×
124
    }
125

126
    /**
127
     * Will set the routes in your application to use
128
     * the Shield auth routes.
129
     *
130
     * Usage (in Config/Routes.php):
131
     *      - auth()->routes($routes);
132
     *      - auth()->routes($routes, ['except' => ['login', 'register']])
133
     */
134
    public function routes(RouteCollection &$routes, array $config = []): void
135
    {
136
        $authRoutes = config('Auth')->routes;
6✔
137

138
        $namespace = $config['namespace'] ?? 'Daycry\Auth\Controllers';
6✔
139

140
        $routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void {
6✔
141
            foreach ($authRoutes as $name => $row) {
6✔
142
                if (! isset($config['except']) || ! in_array($name, $config['except'], true)) {
6✔
143
                    foreach ($row as $params) {
6✔
144
                        $options = isset($params[3])
6✔
145
                            ? ['as' => $params[3]]
6✔
146
                            : null;
6✔
147
                        $routes->{$params[0]}($params[1], $params[2], $options);
6✔
148
                    }
149
                }
150
            }
151
        });
6✔
152
    }
153

154
    /**
155
     * Returns the Model that is responsible for getting users.
156
     *
157
     * @throws AuthenticationException
158
     */
159
    public function getProvider(): UserProviderInterface
160
    {
161
        if ($this->userProvider !== null) {
69✔
162
            return $this->userProvider;
10✔
163
        }
164

165
        $className          = $this->config->userProvider;
69✔
166
        $this->userProvider = new $className();
69✔
167

168
        return $this->userProvider;
69✔
169
    }
170

171
    /**
172
     * Provide magic function-access to Authenticators to save use
173
     * from repeating code here, and to allow them to have their
174
     * own, additional, features on top of the required ones,
175
     * like "remember-me" functionality.
176
     *
177
     * @param list<string> $args
178
     *
179
     * @throws AuthenticationException
180
     */
181
    public function __call(string $method, array $args)
182
    {
183
        $this->ensureAuthentication();
30✔
184

185
        $authenticator = $this->authenticate->factory($this->alias);
30✔
186

187
        if (method_exists($authenticator, $method)) {
30✔
188
            return $authenticator->{$method}(...$args);
30✔
189
        }
190
    }
191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc