Skip to content

Commit b719a6a

Browse files
authored
feat(auth): add user onboarding screen and relocate AuthController (#204)
* refactor(auth): move AuthController from Admin to root Controllers namespace * feat(auth): implement user onboarding screen and profile setup flow after Google auth * test(auth): add feature tests for onboarding flow and redirection * style: apply prettier code formatting
1 parent 4be8f46 commit b719a6a

6 files changed

Lines changed: 631 additions & 120 deletions

File tree

‎app/Http/Controllers/Admin/AuthController.php‎

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}

‎resources/js/pages/auth/Login.vue‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ const googleAuthUrl = redirectParam
141141
>
142142
<span
143143
class="font-semibold text-slate-900 dark:text-gray-200"
144-
>Instant 1-Click Access:</span
144+
>Fast Google Access:</span
145145
>
146-
নতুন ইউজার হলে এক ক্লিকেই auto account create
147-
হবে, আর পুরাতন ইউজার হলে সরাসরি Login হয়ে যাবে।
146+
নতুন ইউজার হলে তথ্য সেটআপ করে অ্যাকাউন্ট তৈরি
147+
করতে পারবেন, আর পুরাতন ইউজার হলে সরাসরি Login
148+
হয়ে যাবে।
148149
</div>
149150
</div>
150151

0 commit comments

Comments
 (0)