1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWeb\SocialAuth; |
4
|
|
|
|
5
|
|
|
use MadWeb\SocialAuth\Models\SocialProvider; |
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
7
|
|
|
use MadWeb\SocialAuth\Events\SocialUserCreated; |
8
|
|
|
use MadWeb\SocialAuth\Events\SocialUserAttached; |
9
|
|
|
use Laravel\Socialite\Contracts\User as SocialUser; |
10
|
|
|
use MadWeb\SocialAuth\Contracts\SocialAuthenticatable; |
11
|
|
|
|
12
|
|
|
class SocialProviderManager |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var SocialProvider |
16
|
|
|
*/ |
17
|
|
|
protected $social; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* SocialProviderManager constructor. |
21
|
|
|
* @param SocialProvider $social |
22
|
|
|
*/ |
23
|
51 |
|
public function __construct(SocialProvider $social) |
24
|
|
|
{ |
25
|
51 |
|
$this->social = $social; |
26
|
51 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $key |
30
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
31
|
|
|
*/ |
32
|
24 |
|
public function socialUserQuery(string $key) |
33
|
|
|
{ |
34
|
24 |
|
return $this->social->users()->wherePivot(config('social-auth.foreign_keys.socials'), $key); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Gets user by unique social identifier. |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
18 |
|
public function getUserByKey(string $key) |
44
|
|
|
{ |
45
|
18 |
|
return $this->socialUserQuery($key)->first(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param SocialAuthenticatable $user |
50
|
|
|
* @param SocialUser $socialUser |
51
|
|
|
*/ |
52
|
12 |
|
public function attach(SocialAuthenticatable $user, SocialUser $socialUser) |
53
|
|
|
{ |
54
|
12 |
|
$user->attachSocial( |
55
|
12 |
|
$this->social, |
56
|
12 |
|
$socialUser->getId(), |
57
|
12 |
|
$socialUser->token, |
|
|
|
|
58
|
12 |
|
$socialUser->expiresIn ?? null |
|
|
|
|
59
|
|
|
); |
60
|
|
|
|
61
|
12 |
|
event(new SocialUserAttached($user, $this->social, $socialUser)); |
62
|
12 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create new system user by social user data. |
66
|
|
|
* |
67
|
|
|
* @param Authenticatable $userModel |
68
|
|
|
* @param SocialProvider $social |
69
|
|
|
* @param SocialUser $socialUser |
70
|
|
|
* @return Authenticatable |
71
|
|
|
*/ |
72
|
6 |
|
public function createNewUser( |
73
|
|
|
Authenticatable $userModel, |
74
|
|
|
SocialProvider $social, |
75
|
|
|
SocialUser $socialUser |
76
|
|
|
): Authenticatable { |
77
|
6 |
|
$NewUser = $userModel->create( |
|
|
|
|
78
|
6 |
|
$userModel->mapSocialData($socialUser) |
|
|
|
|
79
|
|
|
); |
80
|
|
|
|
81
|
6 |
|
$NewUser->attachSocial( |
82
|
6 |
|
$social, |
83
|
6 |
|
$socialUser->getId(), |
84
|
6 |
|
$socialUser->token, |
|
|
|
|
85
|
6 |
|
$socialUser->expiresIn ?? null |
|
|
|
|
86
|
|
|
); |
87
|
|
|
|
88
|
6 |
|
event(new SocialUserCreated($NewUser)); |
89
|
|
|
|
90
|
6 |
|
return $NewUser; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: