-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathGitHubIntegrationController.php
More file actions
230 lines (178 loc) · 7.38 KB
/
GitHubIntegrationController.php
File metadata and controls
230 lines (178 loc) · 7.38 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use App\Models\User;
use App\Services\GitHubUserService;
use App\Support\GitHubOAuth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Laravel\Socialite\Facades\Socialite;
class GitHubIntegrationController extends Controller
{
public function __construct()
{
$this->middleware('auth')->except('handleCallback');
}
public function redirectToGitHub(): RedirectResponse
{
session(['github_auth_intent' => 'link']);
// Store the return URL if provided
if (request()->has('return')) {
session(['github_return_url' => request()->get('return')]);
}
return Socialite::driver('github')
->scopes(['read:user', 'repo'])
->redirect();
}
public function handleCallback(): RedirectResponse
{
try {
$githubUser = Socialite::driver('github')->user();
$intent = session()->pull('github_auth_intent', 'link');
if (Auth::check()) {
return $this->handleLinkAccount($githubUser);
}
if ($intent === 'login') {
return $this->handleLogin($githubUser);
}
return to_route('customer.login')
->with('error', 'Please log in first to connect your GitHub account.');
} catch (\Exception $e) {
Log::error('GitHub OAuth callback failed', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
$route = Auth::check() ? 'customer.licenses.list' : 'customer.login';
return to_route($route)
->with('error', 'GitHub authentication failed. Please try again.');
}
}
protected function handleLinkAccount($githubUser): RedirectResponse
{
$user = Auth::user();
$user->update([
'github_id' => $githubUser->id,
'github_username' => $githubUser->nickname,
'github_token' => encrypt($githubUser->token),
]);
$returnUrl = session()->pull('github_return_url');
if ($returnUrl) {
return redirect($returnUrl)
->with('success', 'GitHub account connected successfully!');
}
return to_route('dashboard')
->with('success', 'GitHub account connected successfully!');
}
protected function handleLogin($githubUser): RedirectResponse
{
$user = User::where('github_id', $githubUser->id)->first();
if ($user) {
$user->update([
'github_token' => encrypt($githubUser->token),
]);
Auth::login($user, remember: true);
return redirect()->intended(route('dashboard'))
->with('success', 'Welcome back!');
}
$user = User::where('email', $githubUser->email)->first();
if ($user) {
$user->update([
'github_id' => $githubUser->id,
'github_username' => $githubUser->nickname,
'github_token' => encrypt($githubUser->token),
]);
Auth::login($user, remember: true);
return redirect()->intended(route('dashboard'))
->with('success', 'GitHub account connected and logged in!');
}
$user = User::create([
'name' => $githubUser->name ?? $githubUser->nickname,
'email' => $githubUser->email,
'github_id' => $githubUser->id,
'github_username' => $githubUser->nickname,
'github_token' => encrypt($githubUser->token),
'password' => bcrypt(Str::random(24)),
'email_verified_at' => now(),
]);
Auth::login($user, remember: true);
return to_route('dashboard')
->with('success', 'Account created successfully!');
}
public function requestRepoAccess(): RedirectResponse
{
$user = Auth::user();
if (! $user->github_username) {
return back()->with('error', 'Please connect your GitHub account first.');
}
if (! $user->hasMobileRepoAccess()) {
return back()->with('error', 'You need an active Max license to access the mobile repository.');
}
$github = GitHubOAuth::make();
$success = $github->inviteToMobileRepo($user->github_username);
if ($success) {
$user->update([
'mobile_repo_access_granted_at' => now(),
]);
return back()->with('success', 'Repository invitation sent! Please check your GitHub notifications to accept the invitation.');
}
return back()->with('error', 'Failed to send repository invitation. Please try again or contact support.');
}
public function requestClaudePluginsAccess(): RedirectResponse
{
$user = Auth::user();
if (! $user->github_username) {
return back()->with('error', 'Please connect your GitHub account first.');
}
// Check if user has a Plugin Dev Kit license or is an Ultra team member
$pluginDevKit = Product::where('slug', 'plugin-dev-kit')->first();
if (! $user->hasActiveUltraSubscription() && ! $user->isUltraTeamMember() && (! $pluginDevKit || ! $user->hasProductLicense($pluginDevKit))) {
return back()->with('error', 'You need a Plugin Dev Kit license, Ultra subscription, or Ultra team membership to access the claude-code repository.');
}
$github = GitHubOAuth::make();
$success = $github->inviteToClaudePluginsRepo($user->github_username);
if ($success) {
$user->update([
'claude_plugins_repo_access_granted_at' => now(),
]);
return back()->with('success', 'Repository invitation sent! Please check your GitHub notifications to accept the invitation.');
}
return back()->with('error', 'Failed to send repository invitation. Please try again or contact support.');
}
public function disconnect(): RedirectResponse
{
$user = Auth::user();
$github = GitHubOAuth::make();
if ($user->mobile_repo_access_granted_at && $user->github_username) {
$github->removeFromMobileRepo($user->github_username);
}
if ($user->claude_plugins_repo_access_granted_at && $user->github_username) {
$github->removeFromClaudePluginsRepo($user->github_username);
}
$user->update([
'github_id' => null,
'github_username' => null,
'github_token' => null,
'mobile_repo_access_granted_at' => null,
'claude_plugins_repo_access_granted_at' => null,
]);
return back()->with('success', 'GitHub account disconnected successfully.');
}
public function repositories(): JsonResponse
{
$user = Auth::user();
if (! $user->hasGitHubToken()) {
return response()->json([
'error' => 'GitHub account not connected or token expired',
'repositories' => [],
], 401);
}
$service = GitHubUserService::for($user);
$repositories = $service->getRepositories(includePrivate: true);
return response()->json([
'repositories' => $repositories,
]);
}
}