2016-01-06 102 views
-1

這是我的控制器。如何防止Facebook登錄後在彈出窗口中重定向網址?

public function actions() 
{ 
    return [   

     'auth' => [ 
      'class' => 'yii\authclient\AuthAction', 
      'successCallback' => [$this, 'oAuthSuccess'], 
     ], 
    ]; 
} 


public function oAuthSuccess($client) { 

    $name = explode(" ",$userAttributes['name']); 
    $existing_customer = Customer::find() 
       ->where(['email' => $userAttributes['email']]) 
       ->orWhere(['id_facebook' => $userAttributes['id']]) 
       ->one();   

    if(empty($existing_customer)){ 

     $customer = new Customer(); 
     $customer->firstname = $name[0]; 
     $customer->lastname = $name[1]; 
     $customer->id_default_group = 3; 
     $customer->username = $userAttributes['id'].'@facebook.com'; 
     $customer->id_facebook = $userAttributes['id']; 
     $customer->email = $userAttributes['email']; 
     $password = rand(0000, 9999); 
     $auth_key = Yii::$app->getSecurity()->generateRandomString(); 
     $customer->auth_key = $auth_key; 
     $hash = Yii::$app->getSecurity()->generatePasswordHash($password); 
     $customer->password_hash = $hash; 
     $customer->activation_code = $password; 
     $customer->active =1; 

     if ($customer->save(false)) {  

      $customergroup = new CustomerGroup();  
      $customergroup->id_customer = $customer->id_customer; 
      $customergroup->id_group = $customer->id_default_group; 
      $customergroup->save(false);     

      Yii::$app->response->redirect(['advanced','email' => $customer->email]); 
     }   
    } 

這是我的main.php文件。

'authClientCollection' => [ 
     'class' => 'yii\authclient\Collection', 
     'clients' => [ 
      'facebook' => [     
       'class' => 'yii\authclient\clients\Facebook', 
       'authUrl' => 'https://www.facebook.com/dialog/oauth?display=popup', 
       'clientId' => '', 
       'clientSecret' => '',      
       'scope' => [ 
        'email', 
        'user_birthday', 
        'user_location', 
        'user_hometown',       
       ], 
      ],     
     ], 
    ], 

其實我在做註冊的2個過程。 用戶點擊Facebook按鈕後,它返回到我的第二步彈出,但我需要它在我的網站。這怎麼可能?

回答

1

改變你與你的願望的行動對行動功能auth successCallBack ..

樣本,其中successCallback檢查,如果用戶如果爲真呼叫動作身份驗證,否則動作連接

/** @inheritdoc */ 
public function actions() 
{ 
    return [ 
     'auth' => [ 
      'class' => AuthAction::className(), 
      'successCallback' => \Yii::$app->user->isGuest 
       ? [$this, 'authenticate'] 
       : [$this, 'connect'], 
     ] 
    ]; 
} 

客人下方在facebook登錄後,您將返回oAuthSuccess函數內的代碼。我認爲您正在尋找的彈出窗口位於此函數內部,並且從此重定向中調用。如果你想讓其他人改變這個功能的引導部分。

Yii::$app->response->redirect(['advanced','email' => $customer->email]); 
+0

你能介紹關於驗證和連接功能嗎? –

+0

這些函數名稱是樣本..你必須用你想要執行的動作的名稱替換這些名稱....在描述的情況下 – scaisEdge

+0

在你的問題你使用$ this'oAuthSuccess'你的控制器的動作..但似乎這顯示你不想要的模式.. – scaisEdge