-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Problem with authorization through social network new user. Return 404 #383
Comments
I tried all events. The problem is solved only this way, otherwise redirecting to 404Config. Event beforeAuthenticate handler 'user' => [
'class' => Da\User\Module::class,
'controllerMap' => [
'security' => [
'class' => 'Da\User\Controller\SecurityController',
'on beforeAuthenticate' => ['app\components\SocialNetworkHandler', 'beforeAuthenticate']
],
],
], file components/SocialNetworkHandler.php<?php
namespace app\components;
use Da\User\Event\SocialNetworkAuthEvent;
use Da\User\Factory\MailFactory;
use Da\User\Model\User;
use Da\User\Service\UserCreateService;
use Yii;
use yii\base\BaseObject;
class SocialNetworkHandler extends BaseObject
{
/**
* @param SocialNetworkAuthEvent $event
*/
public static function beforeAuthenticate($event)
{
/** @var User $model */
$user = Yii::$container->get(User::class);
if($user::find()->where(['email' => $event->client->email])->exists()) return;
$user = new $user([
'scenario' => 'create',
'email' => $event->client->email,
'username' => $event->client->username,
'password' => null
]);
/** @var MailFactory $mailFactory */
$mailFactory = Yii::$container->get(MailFactory::class);
$mailService = $mailFactory::makeWelcomeMailerService($user);
/** @var UserCreateService $userCreateService */
$userCreateService = Yii::$container->get(UserCreateService::class, [$user, $mailService]);
$userCreateService->run();
$event->account->user_id = $user->id;
$event->account->save();
Yii::$app->user->login($user);
}
} |
I also noticed this bug. BEFORE if (($user = $this->getUser($account)) instanceof User) {
$account->user_id = $user->id;
$account->save(false);
} AFTER if (($user = $this->getUser($account)) instanceof User) {
$account->user_id = $user->id;
}
$account->save(false); |
@grozzzny from your code I made an even simpler fix. Thanks for the insight on the beforeAuthenticate hook. My final SocialNetworkHandler.php file is only the code below. Rest is exactly the same as you pointed out. class SocialNetworkHandler extends BaseObject
{
/**
* @param SocialNetworkAuthEvent $event
*/
public static function beforeAuthenticate($event)
{
return $event->account->save(false);
}
} Saving the social account like above already unlocks the entire user registration/welcome email etc. stuff existing, in my project the code below is redundant ( of course, unless you are customizing it) $user = new $user([
'scenario' => 'create',
'email' => $event->client->email,
'username' => $event->client->username,
'password' => null
]);
/** @var MailFactory $mailFactory */
$mailFactory = Yii::$container->get(MailFactory::class);
$mailService = $mailFactory::makeWelcomeMailerService($user);
/** @var UserCreateService $userCreateService */
$userCreateService = Yii::$container->get(UserCreateService::class, [$user, $mailService]);
$userCreateService->run();
``` |
Thanks, @mkodummy. |
@mkodummy and @grozzzny Thanks, the last change works! But I get this warning when login with github: Please use the Authorization HTTP header instead as using the Depending on your API usage, we'll be sending you this email reminder on a monthly basis. Visit https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param for more information about suggested workarounds and removal dates. |
this happened to me also ... and I reached this page ... why is not fixed in the official repo ? |
For PHP 7.4+ 'user' => [
'class' => Da\User\Module::class,
'controllerMap' => [
'security' => [
'class' => Da\User\Controller\SecurityController::class,
'on beforeAuthenticate' =>
static fn (Da\User\Event\SocialNetworkAuthEvent $event) => $event->account->save(false),
],
],
// ...
], |
This solution worked like a charm!
|
I added this in my |
@grozzzny i tried your steps, after i login with google, it redirects me to |
I don't know if this is related to your issue, but I also had to apply the fix described in this issue #397 |
@Jonyx4 unfortunately adding the |
This works for me:
frontend\controllers\SocialNetworkHandler:
|
@whisere does yours redirect newly registered users to the |
It is normal that's just a step for user to confirm/edit their user name and email in the system (they may not want to use the same user name and email used in the social media account), it should automatically fill in the email address from social account. |
@whisere cool, thanks. if i was to skip that step, and auto fill firstname and last name from social account, know which file i can edit or extend by any chance? |
There may not be first and last name from social account, you can see: overwriting controllers in the documentation: |
yii2-usuario/src/User/Controller/RegistrationController.php
Line 158 in 8adbffe
Pls, help me. The problem with authorization through social network new user. Return 404 /user/registration/connect?code=FMwHPLCI*****************DJC
The text was updated successfully, but these errors were encountered: