-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathCustomerAuthController.php
More file actions
185 lines (148 loc) · 5.65 KB
/
CustomerAuthController.php
File metadata and controls
185 lines (148 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace App\Http\Controllers\Auth;
use App\Enums\TeamUserStatus;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Models\Plugin;
use App\Models\TeamUser;
use App\Models\User;
use App\Services\CartService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\View\View;
class CustomerAuthController extends Controller
{
public function __construct(protected CartService $cartService) {}
public function showLogin(): View
{
return view('auth.login');
}
public function showRegister(): View
{
return view('auth.register');
}
public function register(Request $request): RedirectResponse
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email:rfc,dns', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
Auth::login($user);
// Transfer guest cart to user
$this->cartService->transferGuestCartToUser($user);
// Check for pending team invitation
$this->acceptPendingTeamInvitation($user);
// Check for pending add-to-cart action
$pendingPluginId = session()->pull('pending_add_to_cart');
if ($pendingPluginId) {
$plugin = Plugin::find($pendingPluginId);
if ($plugin && $plugin->isPaid() && $plugin->activePrice) {
$cart = $this->cartService->getCart($user);
try {
$this->cartService->addPlugin($cart, $plugin);
return to_route('cart.show')
->with('success', "{$plugin->name} has been added to your cart!");
} catch (\Exception $e) {
// Plugin couldn't be added, continue to normal flow
}
}
}
return redirect()->intended(route('dashboard'));
}
public function login(LoginRequest $request): RedirectResponse
{
$request->authenticate();
$request->session()->regenerate();
$user = Auth::user();
// Transfer guest cart to user
$this->cartService->transferGuestCartToUser($user);
// Check for pending team invitation
$this->acceptPendingTeamInvitation($user);
// Check for pending add-to-cart action
$pendingPluginId = session()->pull('pending_add_to_cart');
if ($pendingPluginId) {
$plugin = Plugin::find($pendingPluginId);
if ($plugin && $plugin->isPaid() && $plugin->activePrice) {
$cart = $this->cartService->getCart($user);
try {
$this->cartService->addPlugin($cart, $plugin);
return to_route('cart.show')
->with('success', "{$plugin->name} has been added to your cart!");
} catch (\Exception $e) {
// Plugin couldn't be added, continue to normal flow
}
}
}
return redirect()->intended(route('dashboard'));
}
public function logout(Request $request): RedirectResponse
{
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return to_route('customer.login');
}
public function showForgotPassword(): View
{
return view('auth.forgot-password');
}
public function sendPasswordResetLink(Request $request): RedirectResponse
{
$request->validate([
'email' => ['required', 'email:rfc,dns'],
]);
$status = \Illuminate\Support\Facades\Password::sendResetLink(
$request->only('email')
);
return $status === \Illuminate\Auth\Passwords\PasswordBroker::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}
public function showResetPassword(string $token): View
{
return view('auth.reset-password', ['token' => $token]);
}
public function resetPassword(Request $request): RedirectResponse
{
$request->validate([
'token' => ['required'],
'email' => ['required', 'email:rfc,dns'],
'password' => ['required', 'min:8', 'confirmed'],
]);
$status = \Illuminate\Support\Facades\Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password): void {
$user->forceFill([
'password' => $password,
]);
$user->save();
}
);
return $status === \Illuminate\Auth\Passwords\PasswordBroker::PASSWORD_RESET
? to_route('customer.login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
private function acceptPendingTeamInvitation(User $user): void
{
$token = session()->pull('pending_team_invitation_token');
if (! $token) {
return;
}
$teamUser = TeamUser::where('invitation_token', $token)
->where('email', $user->email)
->where('status', TeamUserStatus::Pending)
->first();
if ($teamUser) {
$teamUser->accept($user);
session()->flash('success', "You've joined {$teamUser->team->name}!");
}
}
}