|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace App\Src\UseCases\Infra\Gateway; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use App\Src\UseCases\Domain\Auth\SocialiteUser; |
|
8
|
|
|
use App\Src\UseCases\Domain\Shared\Gateway\SocialiteGateway; |
|
9
|
|
|
use Laravel\Socialite\Facades\Socialite; |
|
10
|
|
|
|
|
11
|
|
|
class SocialiteGatewayImpl implements SocialiteGateway |
|
12
|
|
|
{ |
|
13
|
|
|
public function user(string $provider): SocialiteUser |
|
14
|
|
|
{ |
|
15
|
|
|
if($provider === 'twitter'){ |
|
16
|
|
|
$user = Socialite::driver($provider)->user(); |
|
17
|
|
|
}elseif($provider === 'facebook'){ |
|
18
|
|
|
$user = Socialite::driver($provider)->fields(['name', 'first_name', 'last_name', 'email'])->user(); |
|
|
|
|
|
|
19
|
|
|
}else { |
|
20
|
|
|
$user = Socialite::driver($provider)->stateless()->user(); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
$email = $user->getEmail(); |
|
24
|
|
|
|
|
25
|
|
|
// For twitter, we prefer to use the name. If empty we'll use the twitter account (nickname): |
|
26
|
|
|
$firstname = $user->getName() !== null ? $user->getName() : $user->getNickname(); |
|
27
|
|
|
if(!empty($user->user['given_name'])){ |
|
28
|
|
|
// Google |
|
29
|
|
|
$firstname = $user->user['given_name']; |
|
30
|
|
|
}elseif (!empty($user->user['first_name'])){ |
|
31
|
|
|
// Facebook |
|
32
|
|
|
$firstname = $user->user['first_name']; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$lastname = $user->getName(); |
|
36
|
|
|
if(!empty($user->user['family_name'])){ |
|
37
|
|
|
// Google |
|
38
|
|
|
$lastname = $user->user['family_name']; |
|
39
|
|
|
}elseif (!empty($user->user['last_name'])){ |
|
40
|
|
|
// Facebook |
|
41
|
|
|
$lastname = $user->user['last_name']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$id = $user->getId(); |
|
45
|
|
|
$picture = $user->getAvatar(); |
|
46
|
|
|
|
|
47
|
|
|
return new SocialiteUser($id, $email, $firstname, $lastname, $picture); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|