From bcc68fb2a9e010541ca896ffa94265d5e303f3fb Mon Sep 17 00:00:00 2001 From: Dmitry Erofeev Date: Sun, 27 Mar 2016 21:09:02 +0300 Subject: [PATCH] Update docs --- docs/social-auth.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/social-auth.md b/docs/social-auth.md index 32215aed1..d802f6c84 100644 --- a/docs/social-auth.md +++ b/docs/social-auth.md @@ -140,4 +140,36 @@ The following config allows to log in using 3 networks (Twitter, Facebook and Go ], ], ], +``` + +## How to access social network attributes + +You can fill user's profile with data provided by social network, here is quick example of how to fill profile name +with the name provided via facebook: + +```php +// plase this code somewhere in your config files (bootstrap.php in case of using advanced app template, web.php in case +// of using basic app template + +use dektrium\user\controllers\SecurityController; + +Event::on(SecurityController::class, SecurityController::EVENT_AFTER_AUTHENTICATE, function (AuthEvent $e) { + // if user account was not created we should not continue + if ($e->account->user === null) { + return; + } + + // we are using switch here, because all networks provide different sets of data + switch ($e->client->getName()) { + case 'facebook': + $e->account->user->profile->updateAttributes([ + 'name' => $e->client->getUserAttributes()['name'], + ]); + case 'vkontakte': + // some different logic + } + + // after saving all user attributes will be stored under account model + // Yii::$app->identity->user->accounts['facebook']->decodedData +}); ``` \ No newline at end of file