• 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

0.0
/src/Controllers/BaseAuthController.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\Controllers;
15

16
use App\Controllers\BaseController;
17
use CodeIgniter\HTTP\RedirectResponse;
18
use Daycry\Auth\Authentication\Authenticators\Session;
19
use Daycry\Auth\Interfaces\AuthController;
20
use Daycry\Auth\Models\LoginModel;
21
use Daycry\Auth\Result;
22
use Daycry\Auth\Traits\BaseControllerTrait;
23
use Daycry\Auth\Traits\Viewable;
24

25
/**
26
 * Base Auth Controller that provides common functionality for all auth controllers
27
 *
28
 * Uses BaseControllerTrait for core functionality and implements
29
 * common patterns used across auth controllers.
30
 */
31
abstract class BaseAuthController extends BaseController implements AuthController
32
{
33
    use BaseControllerTrait;
34
    use Viewable;
35

36
    /**
37
     * Get CSRF token array for forms
38
     */
UNCOV
39
    protected function getTokenArray(): array
×
40
    {
UNCOV
41
        return $this->getToken();
×
42
    }
43

44
    /**
45
     * Check if user is already logged in and redirect if needed
46
     */
UNCOV
47
    protected function redirectIfLoggedIn(?string $redirectUrl = null): ?RedirectResponse
×
48
    {
49
        if (auth()->loggedIn()) {
×
UNCOV
50
            $url = $redirectUrl ?? config('Auth')->loginRedirect();
×
51

UNCOV
52
            return redirect()->to($url);
×
53
        }
54

UNCOV
55
        return null;
×
56
    }
57

58
    /**
59
     * Get validation rules for the specific controller
60
     * Must be implemented by child classes
61
     */
62
    abstract protected function getValidationRules(): array;
63

64
    /**
65
     * Validate request data with given rules
66
     */
UNCOV
67
    protected function validateRequest(array $data, array $rules): bool
×
68
    {
UNCOV
69
        return $this->validateData($data, $rules, [], config('Auth')->DBGroup);
×
70
    }
71

72
    /**
73
     * Handle validation errors with redirect
74
     */
UNCOV
75
    protected function handleValidationError(?string $route = null): RedirectResponse
×
76
    {
UNCOV
77
        $route ??= $this->request->getUri()->getPath();
×
78

79
        return redirect()->to($route)
×
80
            ->withInput()
×
UNCOV
81
            ->with('errors', $this->validator->getErrors());
×
82
    }
83

84
    /**
85
     * Handle successful action with redirect
86
     */
UNCOV
87
    protected function handleSuccess(string $redirectUrl, ?string $message = null): RedirectResponse
×
88
    {
UNCOV
89
        $redirect = redirect()->to($redirectUrl);
×
90

91
        if ($message) {
×
UNCOV
92
            $redirect = $redirect->with('message', $message);
×
93
        }
94

UNCOV
95
        return $redirect;
×
96
    }
97

98
    /**
99
     * Handle error with redirect
100
     */
UNCOV
101
    protected function handleError(string $route, string $error, bool $withInput = true): RedirectResponse
×
102
    {
UNCOV
103
        $redirect = redirect()->to($route);
×
104

105
        if ($withInput) {
×
UNCOV
106
            $redirect = $redirect->withInput();
×
107
        }
108

UNCOV
109
        return $redirect->with('error', $error);
×
110
    }
111

112
    /**
113
     * Get current session authenticator
114
     */
UNCOV
115
    protected function getSessionAuthenticator(): Session
×
116
    {
UNCOV
117
        return auth('session')->getAuthenticator();
×
118
    }
119

120
    /**
121
     * Check if current request has post-authentication action
122
     */
UNCOV
123
    protected function hasPostAuthAction(): bool
×
124
    {
UNCOV
125
        return $this->getSessionAuthenticator()->hasAction();
×
126
    }
127

128
    /**
129
     * Redirect to auth action if one exists
130
     */
UNCOV
131
    protected function redirectToAuthAction(): RedirectResponse
×
132
    {
UNCOV
133
        return redirect()->route('auth-action-show')->withCookies();
×
134
    }
135

136
    /**
137
     * Extract credentials from POST data for login
138
     */
UNCOV
139
    protected function extractLoginCredentials(): array
×
140
    {
141
        $credentials             = $this->request->getPost(setting('Auth.validFields')) ?? [];
×
142
        $credentials             = array_filter($credentials);
×
UNCOV
143
        $credentials['password'] = $this->request->getPost('password');
×
144

UNCOV
145
        return $credentials;
×
146
    }
147

148
    /**
149
     * Check if remember me is requested
150
     */
UNCOV
151
    protected function shouldRememberUser(): bool
×
152
    {
UNCOV
153
        return (bool) $this->request->getPost('remember');
×
154
    }
155

156
    /**
157
     * Handle authentication result
158
     */
UNCOV
159
    protected function handleAuthResult(Result $result, string $failureRoute): RedirectResponse
×
160
    {
161
        if (! $result->isOK()) {
×
UNCOV
162
            return $this->handleError($failureRoute, $result->reason());
×
163
        }
164

165
        // Handle post-authentication action if exists
166
        if ($this->hasPostAuthAction()) {
×
UNCOV
167
            return $this->redirectToAuthAction();
×
168
        }
169

170
        // Redirect to success page
171
        return $this->handleSuccess(
×
172
            config('Auth')->loginRedirect(),
×
UNCOV
173
        )->withCookies();
×
174
    }
175

176
    /**
177
     * Records a login/action attempt in the login log.
178
     *
179
     * @param string          $type       Identity type (e.g. Session::ID_TYPE_MAGIC_LINK)
180
     * @param string          $identifier The identifier used (email, token, etc.)
181
     * @param bool            $success    Whether the attempt was successful
182
     * @param int|string|null $userId     The user ID if known
183
     */
UNCOV
184
    protected function recordLoginAttempt(
×
185
        string $type,
186
        string $identifier,
187
        bool $success,
188
        $userId = null,
189
    ): void {
190
        /** @var LoginModel $loginModel */
UNCOV
191
        $loginModel = model(LoginModel::class);
×
192

UNCOV
193
        $loginModel->recordLoginAttempt(
×
UNCOV
194
            $type,
×
UNCOV
195
            $identifier,
×
UNCOV
196
            $success,
×
UNCOV
197
            $this->request->getIPAddress(),
×
UNCOV
198
            (string) $this->request->getUserAgent(),
×
UNCOV
199
            $userId,
×
UNCOV
200
        );
×
201
    }
202
}
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