S
S
Stanislav Khmelevsky2016-05-13 17:42:54
Yii
Stanislav Khmelevsky, 2016-05-13 17:42:54

How to add multilingual support to Yii2 - authClient returnUrl?

Good day!
There is a project in which multilingualism is implemented, according to this article
Those . links look like https://site.io/ru/profile | https://site.io/en/profile | etc.
There is a Yii2 extension - authClient .
In the settings for the extension, there is a returnUrl parameter that stores the link
to which the user is redirected after successful authorization/registration.
But here's the problem, when I redirect to https://site.io/auth?authclient=social_network_name
, I get an error:

Exception – yii\authclient\InvalidResponseException

Request failed with code: 401, 
message: {"error":"invalid_grant","error_description":"Code is invalid or expired."}

And if you specify some language for returnUrl :
https://site.io/en/auth?authclient=social_network_name
, then everything is OK, and there are no errors. But languages ​​can be a little less than dofiga!
I tried to attach a "crutch" to the action that receives the returnUrl,
splits it, adds the current language, and saves it back,
but it didn't help and the error still appears. Here is an example of this crutch:
public function actions() {
    return [
        'auth' => [
            'class' => 'yii\authclient\AuthAction',
            'successCallback' => [$this, 'onAuthSuccess'],
        ],
        ...
    ];
}

public function onAuthSuccess($client) {
    $exploded_url = explode(Yii::$app->request->hostInfo . '/', $client->getReturnUrl());
    $current_lang = Lang::getCurrent();
    $new_return_url = Yii::$app->request->hostInfo . '/' . $current_lang->url . '/' . $exploded_url[1];
    $client->setReturnUrl($new_return_url);

    if ( (new AuthHandler($client))->handle() ) {
        $this->redirect('profile');
    }
}

The funny thing is, in general, everything works, i.e. only redirect does not happen.
If you go back and refresh the page, you can see that authorization / registration has been
completed.
In this regard, the question is: How to beautifully implement multilingual support, with the correct redirect?
PS Or am I missing something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Arutyunov, 2016-05-13
@socode

I "translated" all urls with this extension: https://github.com/codemix/yii2-localeurls
Any request goes through urlManager which is overridden in this extension. The language for the user is automatically determined and redirected to this language version. You can also add to "ignore" those url patterns that do not need to be processed and redirected. For example, /api etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question