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

daycry / auth / 23341393117

20 Mar 2026 11:45AM UTC coverage: 64.989% (+1.2%) from 63.745%
23341393117

push

github

daycry
Merge branch 'development' of https://github.com/daycry/auth into development

4 of 4 new or added lines in 2 files covered. (100.0%)

315 existing lines in 13 files now uncovered.

3306 of 5087 relevant lines covered (64.99%)

47.03 hits per line

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

90.2
/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\Entities\UserIdentity;
21
use Daycry\Auth\Exceptions\AuthenticationException;
22
use Daycry\Auth\Interfaces\ActionInterface;
23
use Daycry\Auth\Interfaces\AuthenticatorInterface;
24
use Daycry\Auth\Interfaces\UserProviderInterface;
25

26
/**
27
 * Facade for Authentication
28
 *
29
 * Common methods (all authenticators):
30
 *
31
 * @method Result               attempt(array $credentials)
32
 * @method Result               check(array $credentials)
33
 * @method bool                 checkAction(UserIdentity $identity, string $token)
34
 * @method void                 completeLogin(User $user)
35
 * @method void                 forget(?User $user = null)
36
 * @method ActionInterface|null getAction()
37
 * @method mixed                getLogCredentials(array $credentials)
38
 *
39
 * Session-only methods:
40
 * @method string    getPendingMessage()
41
 * @method User|null getPendingUser()
42
 * @method User|null getUser()
43
 * @method bool      hasAction(int|string|null $userId = null)
44
 * @method bool      isAnonymous()
45
 * @method bool      isPending()
46
 * @method bool      loggedIn()
47
 * @method void      login(User $user, bool $actions = true)
48
 * @method void      loginById(int|string $userId)
49
 * @method void      logout()
50
 * @method void      recordActiveDate()
51
 * @method self      remember(bool $shouldRemember = true)
52
 * @method void      startLogin(User $user)
53
 * @method bool      startUpAction(string $type, User $user)
54
 */
55
class Auth
56
{
57
    /**
58
     * The current version of CodeIgniter Shield
59
     */
60
    public const SHIELD_VERSION = '5.0.0';
61

62
    protected ?UserProviderInterface $userProvider = null;
63
    protected ?Authentication $authenticate        = null;
64

65
    /**
66
     * The Authenticator alias to use for this request.
67
     */
68
    protected ?string $alias = null;
69

70
    public function __construct(protected AuthConfig $config)
86✔
71
    {
72
    }
86✔
73

74
    protected function ensureAuthentication(): void
70✔
75
    {
76
        if ($this->authenticate !== null) {
70✔
77
            return;
32✔
78
        }
79

80
        $authenticate = new Authentication($this->config);
70✔
81
        $authenticate->setProvider($this->getProvider());
70✔
82

83
        $this->authenticate = $authenticate;
70✔
84
    }
85

86
    /**
87
     * Sets the Authenticator alias that should be used for this request.
88
     *
89
     * @return $this
90
     */
91
    public function setAuthenticator(?string $alias = null): self
69✔
92
    {
93
        if ($alias !== null) {
69✔
94
            $this->alias = $alias;
20✔
95
        }
96

97
        return $this;
69✔
98
    }
99

100
    /**
101
     * Returns the current authentication class.
102
     */
103
    public function getAuthenticator(): AuthenticatorInterface
52✔
104
    {
105
        $this->ensureAuthentication();
52✔
106

107
        return $this->authenticate
52✔
108
            ->factory($this->alias);
52✔
109
    }
110

111
    /**
112
     * Returns the current user, if logged in.
113
     */
114
    public function user(): ?User
40✔
115
    {
116
        return $this->getAuthenticator()->loggedIn()
40✔
117
            ? $this->getAuthenticator()->getUser()
14✔
118
            : null;
40✔
119
    }
120

121
    /**
122
     * Returns the current user's id, if logged in.
123
     *
124
     * @return int|string|null
125
     */
126
    public function id()
26✔
127
    {
128
        $user = $this->user();
26✔
129

130
        return ($user !== null) ? $user->id : null;
26✔
131
    }
132

UNCOV
133
    public function authenticate(array $credentials): Result
×
134
    {
UNCOV
135
        $this->ensureAuthentication();
×
136

UNCOV
137
        return $this->authenticate
×
UNCOV
138
            ->factory($this->alias)
×
UNCOV
139
            ->attempt($credentials);
×
140
    }
141

142
    /**
143
     * Will set the routes in your application to use
144
     * the Shield auth routes.
145
     *
146
     * Usage (in Config/Routes.php):
147
     *      - auth()->routes($routes);
148
     *      - auth()->routes($routes, ['except' => ['login', 'register']])
149
     */
150
    public function routes(RouteCollection &$routes, array $config = []): void
6✔
151
    {
152
        $authRoutes = config('Auth')->routes;
6✔
153

154
        $namespace = $config['namespace'] ?? 'Daycry\Auth\Controllers';
6✔
155

156
        $routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void {
6✔
157
            foreach ($authRoutes as $name => $row) {
6✔
158
                if (! isset($config['except']) || ! in_array($name, $config['except'], true)) {
6✔
159
                    foreach ($row as $params) {
6✔
160
                        $options = isset($params[3])
6✔
161
                            ? ['as' => $params[3]]
6✔
162
                            : null;
6✔
163
                        $routes->{$params[0]}($params[1], $params[2], $options);
6✔
164
                    }
165
                }
166
            }
167
        });
6✔
168
    }
169

170
    /**
171
     * Returns the Model that is responsible for getting users.
172
     *
173
     * @throws AuthenticationException
174
     */
175
    public function getProvider(): UserProviderInterface
73✔
176
    {
177
        if ($this->userProvider !== null) {
73✔
178
            return $this->userProvider;
14✔
179
        }
180

181
        $className          = $this->config->userProvider;
73✔
182
        $this->userProvider = new $className();
73✔
183

184
        return $this->userProvider;
73✔
185
    }
186

187
    /**
188
     * Provide magic function-access to Authenticators to save use
189
     * from repeating code here, and to allow them to have their
190
     * own, additional, features on top of the required ones,
191
     * like "remember-me" functionality.
192
     *
193
     * @param list<string> $args
194
     *
195
     * @throws AuthenticationException
196
     */
197
    public function __call(string $method, array $args)
38✔
198
    {
199
        $this->ensureAuthentication();
38✔
200

201
        $authenticator = $this->authenticate->factory($this->alias);
38✔
202

203
        if (method_exists($authenticator, $method)) {
38✔
204
            return $authenticator->{$method}(...$args);
38✔
205
        }
206
    }
207
}
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