First,you need to do some changes in setting.
Share customer accounts between multiple Website
You should configure this feature here: System -> Configuration -> Customer Configuration -> Share Customer Accounts
.
Make this setting to Global for sharing every customer to all websites.
Share login between websites
To keep the session when switching between stores on different website, enable "Use SID on Frontend" in System > Configuration > General > Web:
Force users to redirect to the same website they have registered
Forcefully customer login to the same website they have registered when we have tried to login from another website.
Use customer_login
Define event to config.xml
<?xml version="1.0"?>
<config>
<modules>
<Stackexchange_Magento165528>
<version>1.0.0</version>
</Stackexchange_Magento165528>
</modules>
<global>
<models>
<magento165528>
<class>Stackexchange_Magento165528_Model</class>
</magento165528>
</models>
<events>
<customer_login> <!-- identifier of the event we want to catch -->
<observers>
<customer_login_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>magento165528/observer</class> <!-- observers class alias -->
<method>redirectoSourceDomain</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</customer_login_handler>
</observers>
</customer_login>
</events>
</global>
</config>
Observer class:
<?php
class Stackexchange_Magento165528_Model_Observer
{
public function redirectoSourceDomain(Varien_Event_Observer $observer)
{
$_customer = $observer->getEvent()->getCustomer();
/*
* Store of website from which website Customer have registered
*/
$_customer_resgister_store_id= $_customer->getStoreId();
if($_customer_resgister_store_id != Mage::app()->getStore()->getStoreId()){
$allStores=Mage::app()->getStores(); //get list of all stores,websites
foreach ($allStores as $_eachStoreId => $val){
$_storeId = Mage::app()->getStore($_eachStoreId)->getId();
//get url using store id
if($_customer_resgister_store_id == $_eachStoreId ){
$Websiteurl= Mage::app()->getStore($_storeId)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$_redirecUrl = $Websiteurl."customer/account/login?SID=".Mage::getModel("core/session")->getEncryptedSessionId();
/* Force redirect to repective Website */
Mage::app()->getFrontController()->getResponse()
->setRedirect($_redirecUrl)
->sendResponse();
exit;
}
}
}
return;
}
}
NOTE:
I HAVE TEST THIS CODE on MY MAGENTO DEMO STORE WEBSITES.
This two website is running from same magento instance using website concept .