Skip to content

Commit cb7030f

Browse files
committed
fix: couldn't read provider if user didn't exist
1 parent 4b07254 commit cb7030f

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

app/Http/Controllers/Auth/ProviderController.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers\Auth;
44

5+
use Illuminate\Support\Facades\Log;
56
use App\Http\Controllers\Controller;
67
use App\Models\User;
78
use Illuminate\Support\Facades\Auth;
@@ -19,18 +20,20 @@ public function callback($provider)
1920
try {
2021

2122
$socialuser = Socialite::driver($provider)->user();
22-
if (User::where('email', $socialuser->email)->first()->provider != $provider) {
23+
$potentialUser = User::where('email', $socialuser->email)->first();
24+
// check if user is already registered
25+
if ($potentialUser && $potentialUser->provider != $provider) {
2326
return redirect('/login')->withErrors(['email' => 'Email uses another provider. Please login with ' . User::where('email', $socialuser->email)->first()->provider . ' instead.']);
2427
}
25-
28+
2629
$name = $socialuser->name;
27-
if($name == null){
30+
if ($name == null) {
2831
$name = $socialuser->nickname;
29-
}
30-
if($name == null){
32+
}
33+
if ($name == null) {
3134
$name = $socialuser->username;
3235
}
33-
if($name == null){
36+
if ($name == null) {
3437
$name = explode('@', $socialuser->email)[0];
3538
}
3639
$user = User::updateOrCreate([
@@ -49,8 +52,9 @@ public function callback($provider)
4952

5053
return redirect('/dashboard');
5154
} catch (\Exception $e) {
52-
// log error on console
53-
return redirect('/login');
55+
// log error on logs
56+
Log::error($e->getMessage());
57+
return redirect('/login');
5458
}
5559

5660

0 commit comments

Comments
 (0)