phpNT - OAuth 2.0
, (*1)
ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅:
ΠΠ²ΡΠΎΡΠΈΠ·Π°ΡΠΈΡ ΡΠ΅ΡΠ΅Π· ΡΡΠΎΡΠΎΠ½Π½ΠΈΠ΅ ΡΠ΅ΡΠ²ΠΈΡΡ.
Π‘ΠΎΡΠΈΠ°Π»ΡΠ½ΡΠ΅ ΡΠ΅ΡΠΈ:
Π£ΡΡΠ°Π½ΠΎΠ²ΠΊΠ°:, (*2)
php composer.phar require "phpnt/yii2-oauth" "*"
ΠΈΠ»ΠΈ, (*3)
composer require phpnt/yii2-oauth "*"
ΠΈΠ»ΠΈ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ Π² composer.json ΡΠ°ΠΉΠ», (*4)
"phpnt/yii2-oauth": "*"
ΠΏΠΎΡΠ»Π΅ Π·Π°Π³ΡΡΠ·ΠΊΠΈ, Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΌΠΈΠ³ΡΠ°ΡΠΈΡ, (*5)
yii migrate --migrationPath=@vendor/phpnt/yii2-oauth/migrations
ΠΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅:
### ΠΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΠ΅:
// Π² ΡΠ°ΠΉΠ»Π΅ Π½Π°ΡΡΡΠΎΠΉΠΊΠΈ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ (main.php - Advanced ΠΈΠ»ΠΈ web.php - Basic)
// Π² controllerMap
...
'controllerMap' => [
'auth' => [
'class' => 'phpnt\oAuth\controllers\AuthController',
'modelUser' => 'app\models\User' // ΠΏΡΡΡ ΠΊ ΠΌΠΎΠ΄Π΅Π»ΠΈ User
],
],
/**
* Π components Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ authClientCollection
* Π΅ΡΠ»ΠΈ Π² ΠΌΠΎΠ΄Π΅Π»ΠΈ app\models\User ΠΈΠΌΠ΅ΡΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠ΅ ΠΏΠΎΠ»Ρ:
* email - ΡΠ». ΠΏΠΎΡΡΠ°
* first_name - ΠΈΠΌΡ
* last_name - ΡΠ°ΠΌΠΈΠ»ΠΈΡ
* avatar - ΠΏΡΡΡ ΠΊ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ
* gender - ΠΏΠΎΠ» (ΠΆΠ΅Π½ΡΠΊΠΈΠΉ - 1, ΠΌΡΠΆΡΠΊΠΎΠΉ - 2)
* status - ΡΡΠ°ΡΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ (0 - Π½Π΅ Π°ΠΊΡΠΈΠ²ΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ, 1 - Π°ΠΊΡΠΈΠ²ΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ (ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΡΠΎΠ»ΡΠΊΠΎ ΡΡΠΎΡ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ, 2 - Π·Π°Π±Π»ΠΎΠΊΠΈΡΠΎΠ²Π°Π½Π½ΡΠΉ))
* ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π²Π°ΡΡ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠ΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΡ
*/
'components' => [
...
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'google' => [
// https://console.developers.google.com/project
'class' => 'phpnt\oAuth\oauth\Google',
'clientId' => '---',
'clientSecret' => '---',
],
'yandex' => [
// https://oauth.yandex.ru/client/new
'class' => 'phpnt\oAuth\oauth\Yandex',
'clientId' => '---',
'clientSecret' => '---',
],
'facebook' => [
// https://developers.facebook.com/apps
'class' => 'phpnt\oAuth\oauth\Facebook',
'clientId' => '---',
'clientSecret' => '---',
],
'vkontakte' => [
// https://vk.com/editapp?act=create
'class' => 'phpnt\oAuth\oauth\VKontakte',
'clientId' => '---',
'clientSecret' => '---',
],
'twitter' => [
// https://dev.twitter.com/apps/new
'class' => 'phpnt\oAuth\oauth\Twitter',
'consumerKey' => '---',
'consumerSecret' => '---',
],
'linkedin' => [
// https://www.linkedin.com/developer/apps/
'class' => 'phpnt\oAuth\oauth\LinkedIn',
'clientId' => '---',
'clientSecret' => '---',
],
'github' => [
// https://github.com/settings/applications/new
'class' => 'phpnt\oAuth\oauth\GitHub',
'clientId' => '---',
'clientSecret' => '---',
'scope' => 'user:email, user'
],
]
],
],
ΠΈΠ»ΠΈ, (*6)
// Π² ΡΠ°ΠΉΠ»Π΅ Π½Π°ΡΡΡΠΎΠΉΠΊΠΈ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ (main.php - Advanced ΠΈΠ»ΠΈ web.php - Basic)
// Π² controllerMap
...
'controllerMap' => [
'auth' => [
'class' => 'phpnt\oAuth\controllers\AuthController',
'modelUser' => 'app\models\User' // ΠΏΡΡΡ ΠΊ ΠΌΠΎΠ΄Π΅Π»ΠΈ User
],
],
/**
* Π components Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΊΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ authClientCollection
* Π΅ΡΠ»ΠΈ Π² ΠΌΠΎΠ΄Π΅Π»ΠΈ app\models\User ΠΏΠΎΠ»Ρ Π½Π΅ ΡΠΎΠ²ΠΏΠ°Π΄Π°ΡΡ Ρ ΠΏΠΎΠ»ΡΠΌΠΈ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ, ΡΠΊΠ°Π·ΡΠ²Π°Π΅ΠΌ ΠΈΡ
Π²ΡΡΡΠ½ΡΡ Ρ Π΄ΠΎΠΏ. ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ:
*/
'components' => [
...
'authClientCollection' => [
'class' => 'yii\authclient\Collection',
'clients' => [
'google' => [
// https://console.developers.google.com/project
'class' => 'phpnt\oAuth\oauth\Google',
'email' => 'email_field_in_User',
'first_name' => 'first_name_field_in_User',
'last_name' => 'last_name_field_in_User',
/* ΠΠΎΠ»Π΅ Π΄Π»Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ */
'avatar' => 'avatar_field_in_User',
/* ΠΠΎΠ»Π΅ ΠΏΠΎΠ» ΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π/Π */
'gender' => 'gender_field_in_User',
'female' => 2, // Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π΄Π»Ρ ΠΆΠ΅Π½ΡΠΊΠΎΠ³ΠΎ ΠΏΠΎΠ»Π°
'male' => 1, // Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π΄Π»Ρ ΠΌΡΠΆΡΠΊΠΎΠ³ΠΎ ΠΏΠΎΠ»Π°
/* ΠΠΎΠ»Π΅ ΡΡΠ°ΡΡΡ ΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π°ΠΊΡΠΈΠ²Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ */
'status' => 'status_field_in_User',
'statusActive' => 1, // Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π΄Π»Ρ Π°ΠΊΡΠΈΠ²Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ
'clientId' => '---',
'clientSecret' => '---',
],
...
]
],
],
### Π ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»Π΅Π½ΠΈΠΈ, Π³Π΄Π΅ Π½ΡΠΆΠ½Π° Π°Π²ΡΠΎΡΠΈΠ·Π°ΡΠΈΡ OAuth 2.0 Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ:
use phpnt\oAuth\AuthChoice;
// Π²ΠΈΠ΄ΠΆΠ΅Ρ, Π²ΡΠ²ΠΎΠ΄ΠΈΡ ΡΠΏΠΈΡΠΎΠΊ ΡΠ΅ΡΠ²ΠΈΡΠΎΠ², Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠΎΡΠΎΡΡΡ
Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ Π°Π²ΡΠΎΡΠΈΠ·ΠΎΠ²Π°ΡΡΡΡ
echo AuthChoice::widget(['baseAuthUrl' => ['/auth/index']]);
ΠΠΎΠΊΡΠΌΠ΅Π½ΡΠ°ΡΠΈΡ (ΠΏΡΠΈΠΌΠ΅ΡΡ):
ΠΠ΅ΡΡΠΈΡ:
### 0.0.1
ΠΠΈΡΠ΅Π½Π·ΠΈΡ: