|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Mail\WelcomeUserMail; |
| 7 | +use App\Models\User; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Support\Facades\Auth; |
| 10 | +use Illuminate\Support\Facades\Mail; |
| 11 | +use Illuminate\Support\Str; |
| 12 | +use Inertia\Inertia; |
| 13 | +use Laravel\Socialite\Facades\Socialite; |
| 14 | + |
| 15 | +class AuthController extends Controller |
| 16 | +{ |
| 17 | + // |
| 18 | + public function index() |
| 19 | + { |
| 20 | + if (Auth::check()) { |
| 21 | + return redirect()->route('admin.index'); |
| 22 | + } |
| 23 | + |
| 24 | + return Inertia::render('auth/Login'); |
| 25 | + } |
| 26 | + |
| 27 | + public function redirectToGoogle(Request $request) |
| 28 | + { |
| 29 | + if (Auth::check()) { |
| 30 | + return redirect()->route('admin.index'); |
| 31 | + } |
| 32 | + |
| 33 | + if ($request->filled('redirect')) { |
| 34 | + $redirectUrl = $request->query('redirect'); |
| 35 | + |
| 36 | + if (str_starts_with($redirectUrl, '/') && ! str_starts_with($redirectUrl, '//')) { |
| 37 | + session(['url.intended' => url($redirectUrl)]); |
| 38 | + } else { |
| 39 | + $host = parse_url($redirectUrl, PHP_URL_HOST); |
| 40 | + $appHost = parse_url(config('app.url'), PHP_URL_HOST); |
| 41 | + |
| 42 | + if ($host && $appHost && ($host === $appHost || str_ends_with($host, '.'.$appHost) || $host === 'localhost')) { |
| 43 | + session(['url.intended' => $redirectUrl]); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return Socialite::driver('google')->redirect(); |
| 49 | + } |
| 50 | + |
| 51 | + public function handleGoogleCallback(Request $request) |
| 52 | + { |
| 53 | + try { |
| 54 | + $googleUser = Socialite::driver('google')->user(); |
| 55 | + } catch (\Throwable $e) { |
| 56 | + return redirect()->route('login')->with('error', 'Failed to authenticate with Google. Please try again.'); |
| 57 | + } |
| 58 | + |
| 59 | + $user = User::where('google_id', $googleUser->getId()) |
| 60 | + ->orWhere('email', $googleUser->getEmail()) |
| 61 | + ->first(); |
| 62 | + |
| 63 | + if ($user) { |
| 64 | + $user->update([ |
| 65 | + 'google_id' => $googleUser->getId(), |
| 66 | + 'email_verified_at' => $user->email_verified_at ?? now(), |
| 67 | + ]); |
| 68 | + |
| 69 | + Auth::login($user, remember: true); |
| 70 | + $request->session()->regenerate(); |
| 71 | + |
| 72 | + $defaultUrl = $user->username |
| 73 | + ? route('user.profile', $user->username) |
| 74 | + : route('profile.edit'); |
| 75 | + |
| 76 | + return redirect()->intended($defaultUrl); |
| 77 | + } |
| 78 | + |
| 79 | + $request->session()->put('onboarding_user', [ |
| 80 | + 'google_id' => $googleUser->getId(), |
| 81 | + 'email' => $googleUser->getEmail(), |
| 82 | + 'name' => $googleUser->getName() ?? $googleUser->getNickname() ?? '', |
| 83 | + 'avatar' => $googleUser->getAvatar() ?? null, |
| 84 | + ]); |
| 85 | + |
| 86 | + return redirect()->route('onboarding'); |
| 87 | + } |
| 88 | + |
| 89 | + public function showOnboarding(Request $request) |
| 90 | + { |
| 91 | + if (Auth::check()) { |
| 92 | + return redirect()->route('index'); |
| 93 | + } |
| 94 | + |
| 95 | + if (! $request->session()->has('onboarding_user')) { |
| 96 | + return redirect()->route('login')->with('error', 'Please continue with Google to create an account.'); |
| 97 | + } |
| 98 | + |
| 99 | + $onboardingUser = $request->session()->get('onboarding_user'); |
| 100 | + |
| 101 | + return Inertia::render('auth/Onboarding', [ |
| 102 | + 'user' => $onboardingUser, |
| 103 | + ]); |
| 104 | + } |
| 105 | + |
| 106 | + public function completeOnboarding(Request $request) |
| 107 | + { |
| 108 | + if (Auth::check()) { |
| 109 | + return redirect()->route('index'); |
| 110 | + } |
| 111 | + |
| 112 | + if (! $request->session()->has('onboarding_user')) { |
| 113 | + return redirect()->route('login')->with('error', 'Session expired. Please continue with Google again.'); |
| 114 | + } |
| 115 | + |
| 116 | + $onboardingData = $request->session()->get('onboarding_user'); |
| 117 | + |
| 118 | + $validated = $request->validate([ |
| 119 | + 'name' => ['required', 'string', 'max:255'], |
| 120 | + 'username' => [ |
| 121 | + 'required', |
| 122 | + 'string', |
| 123 | + 'min:3', |
| 124 | + 'max:30', |
| 125 | + 'regex:/^[a-zA-Z0-9_]+$/', |
| 126 | + 'unique:users,username', |
| 127 | + ], |
| 128 | + 'school' => ['required', 'string', 'max:255'], |
| 129 | + ], [ |
| 130 | + 'school.required' => 'Please enter your school, college, or institution name.', |
| 131 | + 'username.regex' => 'Username can only contain letters, numbers, and underscores.', |
| 132 | + 'username.unique' => 'This username is already taken. Please choose another one.', |
| 133 | + ]); |
| 134 | + |
| 135 | + $user = User::where('google_id', $onboardingData['google_id']) |
| 136 | + ->orWhere('email', $onboardingData['email']) |
| 137 | + ->first(); |
| 138 | + |
| 139 | + if (! $user) { |
| 140 | + $user = User::create([ |
| 141 | + 'name' => $validated['name'], |
| 142 | + 'username' => $validated['username'], |
| 143 | + 'email' => $onboardingData['email'], |
| 144 | + 'google_id' => $onboardingData['google_id'], |
| 145 | + 'institution' => $validated['school'], |
| 146 | + 'email_verified_at' => now(), |
| 147 | + ]); |
| 148 | + |
| 149 | + Mail::to($user->email)->queue(new WelcomeUserMail($user)); |
| 150 | + } |
| 151 | + |
| 152 | + $request->session()->forget('onboarding_user'); |
| 153 | + |
| 154 | + Auth::login($user, remember: true); |
| 155 | + $request->session()->regenerate(); |
| 156 | + |
| 157 | + $defaultUrl = $user->username |
| 158 | + ? route('user.profile', $user->username) |
| 159 | + : route('profile.edit'); |
| 160 | + |
| 161 | + return redirect()->intended($defaultUrl)->with('success', 'Account created successfully! Welcome to HSCStack.'); |
| 162 | + } |
| 163 | + |
| 164 | + public function logout(Request $request) |
| 165 | + { |
| 166 | + Auth::logout(); |
| 167 | + $request->session()->invalidate(); |
| 168 | + $request->session()->regenerateToken(); |
| 169 | + |
| 170 | + return redirect()->route('index'); |
| 171 | + } |
| 172 | +} |
0 commit comments