Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.72% |
36 / 43 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AuthController | |
83.72% |
36 / 43 |
|
40.00% |
2 / 5 |
11.52 | |
0.00% |
0 / 1 |
| showLogin | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| login | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
2.01 | |||
| showRegister | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| register | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
4 | |||
| logout | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Customer; |
| 4 | |
| 5 | use App\Http\Controllers\Controller; |
| 6 | use App\Models\admin\Customer; |
| 7 | use Illuminate\Http\RedirectResponse; |
| 8 | use Illuminate\Http\Request; |
| 9 | use Illuminate\Support\Facades\Auth; |
| 10 | use Illuminate\Support\Facades\Hash; |
| 11 | use Illuminate\Validation\Rules\Password; |
| 12 | use Illuminate\View\View; |
| 13 | |
| 14 | /** |
| 15 | * Customer accounts (guard "customer") — separate from the admin login. |
| 16 | * A guest checkout still creates a plain `customers` row with no password; |
| 17 | * registering later on the same email adopts that row (and its past |
| 18 | * orders) instead of creating a duplicate. |
| 19 | */ |
| 20 | class AuthController extends Controller |
| 21 | { |
| 22 | public function showLogin(): View|RedirectResponse |
| 23 | { |
| 24 | return Auth::guard('customer')->check() |
| 25 | ? redirect()->route('account.orders') |
| 26 | : view('website.account.login'); |
| 27 | } |
| 28 | |
| 29 | public function login(Request $request): RedirectResponse |
| 30 | { |
| 31 | $credentials = $request->validate([ |
| 32 | 'email' => ['required', 'email'], |
| 33 | 'password' => ['required', 'string'], |
| 34 | ]); |
| 35 | |
| 36 | if (! Auth::guard('customer')->attempt($credentials, $request->boolean('remember'))) { |
| 37 | return back()->withInput($request->only('email'))->with('error', 'Those details don’t match our records.'); |
| 38 | } |
| 39 | |
| 40 | $request->session()->regenerate(); |
| 41 | |
| 42 | return redirect()->intended(route('account.orders'))->with('success', 'Welcome back!'); |
| 43 | } |
| 44 | |
| 45 | public function showRegister(): View|RedirectResponse |
| 46 | { |
| 47 | return Auth::guard('customer')->check() |
| 48 | ? redirect()->route('account.orders') |
| 49 | : view('website.account.register'); |
| 50 | } |
| 51 | |
| 52 | public function register(Request $request): RedirectResponse |
| 53 | { |
| 54 | $data = $request->validate([ |
| 55 | 'name' => ['required', 'string', 'max:255'], |
| 56 | 'email' => ['required', 'email', 'max:255'], |
| 57 | 'phone' => ['nullable', 'string', 'max:40'], |
| 58 | 'password' => ['required', 'confirmed', Password::defaults()], |
| 59 | ]); |
| 60 | |
| 61 | // A guest checkout may already have created this email as a plain |
| 62 | // customer row (no password) — adopt it instead of duplicating, so |
| 63 | // their past orders show up in the new account. |
| 64 | $customer = Customer::query()->where('email', $data['email'])->first(); |
| 65 | |
| 66 | if ($customer && $customer->hasAccount()) { |
| 67 | return back()->withInput($request->except('password')) |
| 68 | ->with('error', 'An account with that email already exists — try logging in.'); |
| 69 | } |
| 70 | |
| 71 | if ($customer) { |
| 72 | $customer->update([ |
| 73 | 'name' => $data['name'], |
| 74 | 'phone' => $data['phone'] ?? $customer->phone, |
| 75 | 'password' => Hash::make($data['password']), |
| 76 | ]); |
| 77 | } else { |
| 78 | $customer = Customer::create([ |
| 79 | 'name' => $data['name'], |
| 80 | 'email' => $data['email'], |
| 81 | 'phone' => $data['phone'] ?? null, |
| 82 | 'password' => Hash::make($data['password']), |
| 83 | ]); |
| 84 | } |
| 85 | |
| 86 | Auth::guard('customer')->login($customer); |
| 87 | $request->session()->regenerate(); |
| 88 | |
| 89 | return redirect()->route('account.orders')->with('success', 'Account created — welcome!'); |
| 90 | } |
| 91 | |
| 92 | public function logout(Request $request): RedirectResponse |
| 93 | { |
| 94 | Auth::guard('customer')->logout(); |
| 95 | $request->session()->invalidate(); |
| 96 | $request->session()->regenerateToken(); |
| 97 | |
| 98 | return redirect()->route('account.login')->with('success', 'You have been logged out.'); |
| 99 | } |
| 100 | } |