Skip to content

Commit ca2b0a5

Browse files
author
Anton
authored
Merge pull request #4 from bluzphp/develop
Migrate Auth module to new version of HybridAuth
2 parents c0c1808 + abcbe79 commit ca2b0a5

File tree

7 files changed

+74
-337
lines changed

7 files changed

+74
-337
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ install:
1212
- composer create-project bluzphp/skeleton --stability=dev
1313
# Require current module
1414
- cd skeleton
15-
- composer require bluzphp/module-$BLUZ_MODULE:dev-$TRAVIS_BRANCH
15+
- composer require bluzphp/module-$BLUZ_MODULE:dev-$TRAVIS_BRANCH --dev
1616
before_script:
1717
# Database
1818
- mysql -e 'CREATE DATABASE bluz;'

application/models/Auth/AuthInterface.php

-46
This file was deleted.

application/models/Auth/AuthProvider.php

-224
This file was deleted.
+71-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,91 @@
11
<?php
22
/**
33
* Auth controller
4-
*
5-
* @author yuklia
6-
* @created 05.05.15 17:30
74
*/
85
namespace Application;
96

10-
use Application\Auth\AuthProvider;
117
use Bluz\Controller\Controller;
8+
use Bluz\Proxy\Config;
129
use Bluz\Proxy\Messages;
10+
use Bluz\Proxy\Response;
11+
use Bluz\Proxy\Router;
1312

1413
/**
1514
* @param string $provider
16-
* @return false
15+
*
16+
* @return void
17+
* @throws Exception
1718
*/
1819
return function ($provider = '') {
1920
/**
2021
* @var Controller $this
2122
*/
2223
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) {
2788
Messages::addError($e->getMessage());
2889
}
29-
30-
return false;
90+
Response::redirectTo('index', 'index');
3191
};

application/modules/auth/controllers/endpoint.php

-15
This file was deleted.

0 commit comments

Comments
 (0)