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

daycry / auth / 16343465380

17 Jul 2025 11:07AM UTC coverage: 59.224% (-0.6%) from 59.854%
16343465380

push

github

web-flow
Merge pull request #23 from daycry/development

Improvements

57 of 292 new or added lines in 16 files covered. (19.52%)

6 existing lines in 4 files now uncovered.

1939 of 3274 relevant lines covered (59.22%)

22.81 hits per line

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

0.0
/src/Controllers/ImprovedBaseAuthController.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\Result;
21
use Daycry\Auth\Traits\BaseControllerTrait;
22
use Daycry\Auth\Traits\Viewable;
23

24
/**
25
 * Improved Base Auth Controller that provides common functionality for all auth controllers
26
 *
27
 * Uses BaseControllerTrait for core functionality and implements
28
 * common patterns used across auth controllers.
29
 *
30
 * This is an alternative to the existing controllers that provides:
31
 * - Cleaner code structure
32
 * - Better separation of concerns
33
 * - Reusable helper methods
34
 * - Consistent error handling
35
 */
36
abstract class ImprovedBaseAuthController extends BaseController implements AuthController
37
{
38
    use BaseControllerTrait;
39
    use Viewable;
40

41
    /**
42
     * Get CSRF token array for forms
43
     */
44
    protected function getTokenArray(): array
45
    {
NEW
46
        return $this->getToken();
×
47
    }
48

49
    /**
50
     * Check if user is already logged in and redirect if needed
51
     */
52
    protected function redirectIfLoggedIn(?string $redirectUrl = null): ?RedirectResponse
53
    {
NEW
54
        if (auth()->loggedIn()) {
×
NEW
55
            $url = $redirectUrl ?? config('Auth')->loginRedirect();
×
56

NEW
57
            return redirect()->to($url);
×
58
        }
59

NEW
60
        return null;
×
61
    }
62

63
    /**
64
     * Get validation rules for the specific controller
65
     * Must be implemented by child classes
66
     */
67
    abstract protected function getValidationRules(): array;
68

69
    /**
70
     * Validate request data with given rules
71
     */
72
    protected function validateRequest(array $data, array $rules): bool
73
    {
NEW
74
        return $this->validateData($data, $rules, [], config('Auth')->DBGroup);
×
75
    }
76

77
    /**
78
     * Handle validation errors with redirect
79
     */
80
    protected function handleValidationError(?string $route = null): RedirectResponse
81
    {
NEW
82
        $route ??= $this->request->getUri()->getPath();
×
83

NEW
84
        return redirect()->to($route)
×
NEW
85
            ->withInput()
×
NEW
86
            ->with('errors', $this->validator->getErrors());
×
87
    }
88

89
    /**
90
     * Handle successful action with redirect
91
     */
92
    protected function handleSuccess(string $redirectUrl, ?string $message = null): RedirectResponse
93
    {
NEW
94
        $redirect = redirect()->to($redirectUrl);
×
95

NEW
96
        if ($message) {
×
NEW
97
            $redirect = $redirect->with('message', $message);
×
98
        }
99

NEW
100
        return $redirect;
×
101
    }
102

103
    /**
104
     * Handle error with redirect
105
     */
106
    protected function handleError(string $route, string $error, bool $withInput = true): RedirectResponse
107
    {
NEW
108
        $redirect = redirect()->to($route);
×
109

NEW
110
        if ($withInput) {
×
NEW
111
            $redirect = $redirect->withInput();
×
112
        }
113

NEW
114
        return $redirect->with('error', $error);
×
115
    }
116

117
    /**
118
     * Get current session authenticator
119
     */
120
    protected function getSessionAuthenticator(): Session
121
    {
NEW
122
        return auth('session')->getAuthenticator();
×
123
    }
124

125
    /**
126
     * Check if current request has post-authentication action
127
     */
128
    protected function hasPostAuthAction(): bool
129
    {
NEW
130
        return $this->getSessionAuthenticator()->hasAction();
×
131
    }
132

133
    /**
134
     * Redirect to auth action if one exists
135
     */
136
    protected function redirectToAuthAction(): RedirectResponse
137
    {
NEW
138
        return redirect()->route('auth-action-show')->withCookies();
×
139
    }
140

141
    /**
142
     * Extract credentials from POST data for login
143
     */
144
    protected function extractLoginCredentials(): array
145
    {
NEW
146
        $credentials             = $this->request->getPost(setting('Auth.validFields')) ?? [];
×
NEW
147
        $credentials             = array_filter($credentials);
×
NEW
148
        $credentials['password'] = $this->request->getPost('password');
×
149

NEW
150
        return $credentials;
×
151
    }
152

153
    /**
154
     * Check if remember me is requested
155
     */
156
    protected function shouldRememberUser(): bool
157
    {
NEW
158
        return (bool) $this->request->getPost('remember');
×
159
    }
160

161
    /**
162
     * Handle authentication result
163
     */
164
    protected function handleAuthResult(Result $result, string $failureRoute): RedirectResponse
165
    {
NEW
166
        if (! $result->isOK()) {
×
NEW
167
            return $this->handleError($failureRoute, $result->reason());
×
168
        }
169

170
        // Handle post-authentication action if exists
NEW
171
        if ($this->hasPostAuthAction()) {
×
NEW
172
            return $this->redirectToAuthAction();
×
173
        }
174

175
        // Redirect to success page
NEW
176
        return $this->handleSuccess(
×
NEW
177
            config('Auth')->loginRedirect(),
×
NEW
178
        )->withCookies();
×
179
    }
180
}
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