|
1 | 1 | <?php
|
2 | 2 | /**
|
3 | 3 | * Auth controller
|
4 |
| - * |
5 |
| - * @author yuklia |
6 |
| - * @created 05.05.15 17:30 |
7 | 4 | */
|
8 | 5 | namespace Application;
|
9 | 6 |
|
10 |
| -use Application\Auth\AuthProvider; |
11 | 7 | use Bluz\Controller\Controller;
|
| 8 | +use Bluz\Proxy\Config; |
12 | 9 | use Bluz\Proxy\Messages;
|
| 10 | +use Bluz\Proxy\Response; |
| 11 | +use Bluz\Proxy\Router; |
13 | 12 |
|
14 | 13 | /**
|
15 | 14 | * @param string $provider
|
16 |
| - * @return false |
| 15 | + * |
| 16 | + * @return void |
| 17 | + * @throws Exception |
17 | 18 | */
|
18 | 19 | return function ($provider = '') {
|
19 | 20 | /**
|
20 | 21 | * @var Controller $this
|
21 | 22 | */
|
22 | 23 | try {
|
23 |
| - $auth = new AuthProvider($provider); |
24 |
| - $auth->setIdentity($this->user()); |
25 |
| - $auth->authProcess(); |
26 |
| - } catch (Exception $e) { |
| 24 | + // Get configuration |
| 25 | + $config = Config::getData('auth', 'hybrid'); |
| 26 | + $provider = strtolower($provider); |
| 27 | + |
| 28 | + // Check provider name |
| 29 | + if (!array_key_exists($provider, $config['providers'])) { |
| 30 | + throw new Exception('Invalid provider name'); |
| 31 | + } |
| 32 | + |
| 33 | + $config['callback'] = Router::getFullUrl('auth', 'auth', ['provider' => $provider]); |
| 34 | + |
| 35 | + // Feed configuration array to Hybridauth |
| 36 | + $hybridauth = new \Hybridauth\Hybridauth($config); |
| 37 | + |
| 38 | + // Attempt to authenticate users with a provider by name |
| 39 | + $adapter = $hybridauth->authenticate(ucfirst($provider)); |
| 40 | + |
| 41 | + // Returns a boolean of whether the user is connected with provider |
| 42 | + if ($adapter->isConnected()) { |
| 43 | + // Retrieve the user's profile |
| 44 | + $profile = $adapter->getUserProfile(); |
| 45 | + |
| 46 | + // Access token from provider |
| 47 | + $accessToken = $adapter->getAccessToken(); |
| 48 | + |
| 49 | + // Check authRow |
| 50 | + $authRow = Auth\Table::getAuthRow($provider, $profile->identifier); |
| 51 | + |
| 52 | + // Inspect profile's public attributes |
| 53 | + if ($this->user()) { |
| 54 | + if ($authRow) { |
| 55 | + Messages::addNotice('You have already linked to `%s`', $provider); |
| 56 | + } else { |
| 57 | + // Create token and link it with user profile |
| 58 | + $authRow = new Auth\Row(); |
| 59 | + $authRow->userId = $this->user()->id; |
| 60 | + $authRow->provider = $provider; |
| 61 | + $authRow->foreignKey = $profile->identifier; |
| 62 | + $authRow->tokenSecret = $accessToken['access_token_secret'] ?? ''; |
| 63 | + $authRow->tokenType = $accessToken['token_type'] ?? Auth\Table::TYPE_ACCESS; |
| 64 | + Messages::addNotice('Your account was linked to `%s` successfully!', $provider); |
| 65 | + } |
| 66 | + // Update access token |
| 67 | + $authRow->token = $accessToken['access_token']; |
| 68 | + $authRow->save(); |
| 69 | + Response::redirectTo('users', 'profile'); |
| 70 | + } else { |
| 71 | + // Authenticate in the application |
| 72 | + if ($authRow) { |
| 73 | + // Try to login |
| 74 | + $user = Users\Table::findRow($authRow->userId); |
| 75 | + Auth\Table::tryLogin($user); |
| 76 | + Messages::addNotice('You are signed'); |
| 77 | + } else { |
| 78 | + // User not found |
| 79 | + Messages::addError('Not found linked profile'); |
| 80 | + Response::redirectTo('users', 'signin'); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Disconnect the adapter |
| 85 | + $adapter->disconnect(); |
| 86 | + } |
| 87 | + } catch (\Hybridauth\Exception\Exception $e) { |
27 | 88 | Messages::addError($e->getMessage());
|
28 | 89 | }
|
29 |
| - |
30 |
| - return false; |
| 90 | + Response::redirectTo('index', 'index'); |
31 | 91 | };
|
0 commit comments