• 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/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\Result;
21
use Daycry\Auth\Traits\BaseControllerTrait;
22
use Daycry\Auth\Traits\Viewable;
23

24
/**
25
 * 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
abstract class BaseAuthController extends BaseController implements AuthController
31
{
32
    use BaseControllerTrait;
33
    use Viewable;
34

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

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

NEW
51
            return redirect()->to($url);
×
52
        }
53

NEW
54
        return null;
×
55
    }
56

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

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

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

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

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

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

NEW
94
        return $redirect;
×
95
    }
96

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

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

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

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

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

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

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

NEW
145
        return $credentials;
×
146
    }
147

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

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

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

170
        // Redirect to success page
NEW
171
        return $this->handleSuccess(
×
NEW
172
            config('Auth')->loginRedirect(),
×
NEW
173
        )->withCookies();
×
174
    }
175
}
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