Skip to content

Commit 5124893

Browse files
committed
Refactor social authentication routes and methods for consistency
1 parent 6dacd0a commit 5124893

5 files changed

Lines changed: 19 additions & 25 deletions

File tree

app/Http/Controllers/SocialProvider.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,18 @@
1515

1616
class SocialProvider extends Controller
1717
{
18-
public mixed $user;
18+
private ?User $user;
1919

2020
public function __construct()
2121
{
2222
$this->user = Auth::user();
2323
}
2424

25-
public function google(Request $request): RedirectResponse
25+
public function redirect(Request $request, string $provider): RedirectResponse
2626
{
27-
$this->createSession($request, 'google');
27+
$this->createSession($request);
2828

29-
return Socialite::driver('google')->redirect();
30-
}
31-
32-
public function github(Request $request): RedirectResponse
33-
{
34-
$this->createSession($request, 'github');
35-
36-
return Socialite::driver('github')->redirect();
29+
return Socialite::driver($provider)->redirect();
3730
}
3831

3932
public function callback(string $provider): RedirectResponse
@@ -56,15 +49,15 @@ public function callback(string $provider): RedirectResponse
5649
return $this->redirectBack();
5750
}
5851

59-
$socialAccountByEmail = Social::getUserBySocialAccoutEmail($provider, $socialite->email);
52+
$socialAccountByEmail = Social::getUserBySocialAccountEmail($provider, $socialite->email);
6053

6154
if ($socialAccountByEmail->first()) {
6255
throw new Exception('Can\'t unlink social account with different email address.');
6356
}
6457

65-
$socialAccontsByUserId = Social::getUserBySocialUserId($provider, $this->user->id);
58+
$socialAccountsByUserId = Social::getUserBySocialUserId($provider, $this->user->id);
6659

67-
if ($socialAccontsByUserId->first()) {
60+
if ($socialAccountsByUserId->first()) {
6861
throw new Exception('Can\'t unlink social account with different email address.');
6962
}
7063

@@ -82,7 +75,7 @@ public function callback(string $provider): RedirectResponse
8275
return $this->redirectBack();
8376
}
8477

85-
$loginUsingSocialAccount = Social::getUserBySocialAccountsId($provider, $socialite->id)->first();
78+
$loginUsingSocialAccount = Social::getUserBySocialAccountId($provider, $socialite->id)->first();
8679

8780
if ($loginUsingSocialAccount) {
8881
$user = User::where('id', $loginUsingSocialAccount->user_id)->first();
@@ -178,10 +171,10 @@ private function redirectBack(): RedirectResponse
178171
return Redirect::to($intended);
179172
}
180173

181-
$socialite = Session::get('url.socialite');
174+
$socialiteUrl = Session::get('url.socialite');
182175

183-
if ($socialite) {
184-
return Redirect::to($socialite);
176+
if ($socialiteUrl) {
177+
return Redirect::to($socialiteUrl);
185178
}
186179

187180
return Redirect::route('profile');

app/Models/Social.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function getUserBySocialAccountId(Builder $query, string $provider, st
2323
}
2424

2525
#[Scope]
26-
protected function getUserBySocialAccoutEmail(Builder $query, string $provider, string $email): void
26+
protected function getUserBySocialAccountEmail(Builder $query, string $provider, string $email): void
2727
{
2828
$query->where([
2929
'provider' => $provider,

resources/views/livewire/auth/login.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class="text-[0.9375rem]/4 text-[#8b7355] hover:underline"
7171
<x-separator />
7272

7373
<div class="mb-0.5 flex flex-col gap-2">
74-
<x-social-link :href="route('socials.google')">
74+
<x-social-link :href="route('socials.redirect', ['provider' => 'google'])">
7575
<img
7676
src="{{ asset('assets/icon/google.svg') }}"
7777
class="size-4"
@@ -80,7 +80,7 @@ class="size-4"
8080
Continue with Google
8181
</x-social-link>
8282

83-
<x-social-link :href="route('socials.github')">
83+
<x-social-link :href="route('socials.redirect', ['provider' => 'github'])">
8484
<img
8585
src="{{ asset('assets/icon/github.svg') }}"
8686
class="size-4"

resources/views/livewire/profile.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@ class="xs:max-w-30 ml-auto w-full text-xs/3"
114114
@else
115115
<x-social-account
116116
name="Google"
117-
:url="route('socials.google')"
117+
:url="route('socials.redirect', ['provider' => 'google'])"
118118
email="{{ $socials_google ? $socials_google->email : 'Not Linked' }}"
119119
>
120120
{{ $socials_google ? 'Unlink' : 'Link' }}
121121
</x-social-account>
122122

123123
<x-social-account
124124
name="GitHub"
125-
:url="route('socials.github')"
125+
:url="route('socials.redirect', ['provider' => 'github'])"
126126
email="{{ $socials_github ? $socials_github->email : 'Not Linked' }}"
127127
>
128128
{{ $socials_github ? 'Unlink' : 'Link' }}

routes/web.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@
4646
Route::controller(SocialProvider::class)
4747
->prefix('/socials')
4848
->group(function () {
49-
Route::get('/google', 'google')->name('socials.google');
50-
Route::get('/github', 'github')->name('socials.github');
49+
Route::get('/{provider}', 'redirect')
50+
->name('socials.redirect')
51+
->whereIn('provider', ['google', 'github']);
5152

5253
Route::get('/{provider}/callback', 'callback');
5354
});

0 commit comments

Comments
 (0)