FacebookRegistration.php
3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace FootyRoom\Services\Registration;
use FootyRoom\Config;
use FootyRoom\User\AvatarCreator;
use Illuminate\Contracts\Auth\Guard;
use FootyRoom\Services\Auth\FacebookLogin;
use FootyRoom\Repositories\IUserRepository;
use Illuminate\Contracts\Events\Dispatcher;
use Facebook\Exceptions\FacebookSDKException;
use FootyRoom\Services\Registration\ActivationKeyMailer;
class FacebookRegistration extends Registration
{
/**
* @var string
*/
public $fbtoken;
/**
* @var string
*/
public $facebookId;
/**
* @var \FootyRoom\Services\Auth\FacebookLogin
*/
protected $fbLogin;
/**
* @var \Facebook\GraphNodes\GraphUser
*/
protected $fbMe;
/**
* @var \FootyRoom\Repositories\UserRepository
*/
protected $userRepo;
/**
* @var \FootyRoom\Services\Registration\ActivationKeyMailer
*/
protected $activationKeyMailer;
/**
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $auth;
/**
* @var \Illuminate\Contracts\Events\Dispatcher;
*/
protected $events;
/**
* Constructor.
*
* @param array $userData
* @param \FootyRoom\Repositories\IUserRepository $userRepo
* @param \FootyRoom\Services\Registration\ActivationKeyMailer $activationKeyMailer
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \FootyRoom\Config $config
* @param \Illuminate\Contracts\Events\Dispatcher $events
*/
public function __construct(
array $userData,
IUserRepository $userRepo,
ActivationKeyMailer $activationKeyMailer,
Guard $auth,
Config $config,
Dispatcher $events
) {
$this->userRepo = $userRepo;
$this->activationKeyMailer = $activationKeyMailer;
$this->auth = $auth;
$this->config = $config;
$this->events = $events;
$this->fbtoken = $userData['fbtoken'];
$this->fbLogin = new FacebookLogin($config, $auth, $userRepo);
$this->fbLogin->setToken($this->fbtoken);
try {
$this->fbMe = $this->fbLogin->getMe();
} catch (FacebookSDKException $e) {
$this->errors = ['fbtoken' => ['auth']];
return;
}
$this->facebookId = $this->fbMe->getId();
$userData['email'] = $this->fbMe->getEmail();
$userData['fullName'] = $this->fbMe->getName();
$userData['status'] = 'newborn';
$this->init($userData);
}
public function validate(): bool
{
if (isset($this->error['fbtoken'])) {
return false;
}
$isValid = parent::validate();
if (isset($this->errors['email'])) {
$this->errors['fbtoken'] = $this->errors['email'];
unset($this->errors['email']);
}
return $isValid;
}
public function register()
{
parent::register();
$this->fbLogin->associateWith($this->userId);
// Fetch avatar from Facebook.
if (!$this->fbMe->getPicture()->isSilhouette()) {
AvatarCreator::create($this->fbMe->getPicture()->getUrl(), $this->userId);
}
// Automatically login person who has signed up.
$this->auth->loginUsingId($this->userId);
}
}