|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Bone\SocialAuth\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Bone\Http\Response\HtmlResponse; |
|
6
|
|
|
use Bone\SocialAuth\Service\SocialAuthService; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Laminas\Diactoros\Response\RedirectResponse; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
|
|
12
|
|
|
class SocialLoginController |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var SocialAuthService $service */ |
|
15
|
|
|
private $service; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string $loginRedirectRoute */ |
|
18
|
|
|
private $loginRedirectRoute; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* SocialLoginController constructor. |
|
22
|
|
|
* @param SocialAuthService $service |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function __construct(SocialAuthService $service, string $loginRedirectRoute) |
|
25
|
|
|
{ |
|
26
|
3 |
|
$this->service = $service; |
|
27
|
3 |
|
$this->loginRedirectRoute = $loginRedirectRoute; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ServerRequestInterface $request |
|
32
|
|
|
* @return ResponseInterface |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function login(ServerRequestInterface $request): ResponseInterface |
|
35
|
|
|
{ |
|
36
|
2 |
|
$provider = \ucfirst($request->getAttribute('provider')); |
|
37
|
2 |
|
$adapter = $this->service->getAuthAdapter($provider); |
|
38
|
|
|
|
|
39
|
2 |
|
if ($adapter->isConnected()) { |
|
40
|
1 |
|
$userProfile = $adapter->getUserProfile(); |
|
41
|
1 |
|
$adapter->disconnect(); |
|
42
|
1 |
|
$this->service->logInUser($userProfile); |
|
43
|
|
|
|
|
44
|
1 |
|
return new RedirectResponse($this->loginRedirectRoute); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
throw new Exception('Something went wrong', 500); |
|
48
|
|
|
} |
|
49
|
|
|
} |